@Embeddable在Hibernate中生成类

时间:2014-11-15 22:14:25

标签: java mysql hibernate

我有一个名为Employee的非常简单的MySQl表,它有id,firstname,lastname,age -

但是当我生成Hibernate类时,它看起来像下面的

问题 -

这是一个示例独立表,为什么它会使用@Embeddable ..

生成一个类

员工

package com.hb;

// Generated Nov 15, 2014 5:00:28 PM by Hibernate Tools 3.4.0.CR1

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * Employees generated by hbm2java
 */
@Entity
@Table(name = "Employees", catalog = "javadb")
public class Employees implements java.io.Serializable {

    private EmployeesId id;

    public Employees() {
    }

    public Employees(EmployeesId id) {
        this.id = id;
    }

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)),
            @AttributeOverride(name = "age", column = @Column(name = "age", nullable = false)),
            @AttributeOverride(name = "first", column = @Column(name = "first")),
            @AttributeOverride(name = "last", column = @Column(name = "last")) })
    public EmployeesId getId() {
        return this.id;
    }

    public void setId(EmployeesId id) {
        this.id = id;
    }

}

EmployeesId

package com.hb;

// Generated Nov 15, 2014 5:00:28 PM by Hibernate Tools 3.4.0.CR1

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * EmployeesId generated by hbm2java
 */
@Embeddable
public class EmployeesId implements java.io.Serializable {

    private int id;
    private int age;
    private String first;
    private String last;

    public EmployeesId() {
    }

    public EmployeesId(int id, int age) {
        this.id = id;
        this.age = age;
    }

    public EmployeesId(int id, int age, String first, String last) {
        this.id = id;
        this.age = age;
        this.first = first;
        this.last = last;
    }

    @Column(name = "id", nullable = false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "age", nullable = false)
    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Column(name = "first")
    public String getFirst() {
        return this.first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    @Column(name = "last")
    public String getLast() {
        return this.last;
    }

    public void setLast(String last) {
        this.last = last;
    }

    public boolean equals(Object other) {
        if ((this == other))
            return true;
        if ((other == null))
            return false;
        if (!(other instanceof EmployeesId))
            return false;
        EmployeesId castOther = (EmployeesId) other;

        return (this.getId() == castOther.getId())
                && (this.getAge() == castOther.getAge())
                && ((this.getFirst() == castOther.getFirst()) || (this
                        .getFirst() != null && castOther.getFirst() != null && this
                        .getFirst().equals(castOther.getFirst())))
                && ((this.getLast() == castOther.getLast()) || (this.getLast() != null
                        && castOther.getLast() != null && this.getLast()
                        .equals(castOther.getLast())));
    }

    public int hashCode() {
        int result = 17;

        result = 37 * result + this.getId();
        result = 37 * result + this.getAge();
        result = 37 * result
                + (getFirst() == null ? 0 : this.getFirst().hashCode());
        result = 37 * result
                + (getLast() == null ? 0 : this.getLast().hashCode());
        return result;
    }

}

Hibernate代码生成hbm文件。

<hibernate-mapping>
    <class name="com.hb.Employees" table="Employees" catalog="javadb">
        <composite-id name="id" class="com.hb.EmployeesId">
            <key-property name="id" type="int">
                <column name="id" />
            </key-property>
            <key-property name="age" type="int">
                <column name="age" />
            </key-property>
            <key-property name="first" type="string">
                <column name="first" />
            </key-property>
            <key-property name="last" type="string">
                <column name="last" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

0 个答案:

没有答案