如何在Hibernate中创建表的唯一约束?

时间:2014-02-22 21:10:34

标签: hibernate

如何在休眠中创建唯一约束?

我有办公室课程和员工课程,我想在每个办公室创建唯一员工ID。

请建议我......

1 个答案:

答案 0 :(得分:0)

我得到了如何在休眠中创建唯一约束的答案....

Office.java类

@Entity
@Table(name="OFFICE")
public class Office {

    @Id
    @Column(name = "ID", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "OFFICE_NAME", length=30)
    private String officeName;

    @Column(name = "DESCRPTION", length=5000)
    private String description;

    @OneToMany(mappedBy="office", targetEntity=Emplyee.class, cascade=CascadeType.ALL)
    private Set<Emplyee> emplyees = new HashSet<Emplyee>();

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the officeName
     */
    public String getOfficeName() {
        return officeName;
    }

    /**
     * @param officeName the officeName to set
     */
    public void setOfficeName(String officeName) {
        this.officeName = officeName;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the emplyees
     */
    public Set<Emplyee> getEmplyees() {
        return emplyees;
    }

    /**
     * @param emplyees the emplyees to set
     */
    public void setEmplyees(Set<Emplyee> emplyees) {
        this.emplyees = emplyees;
    }
}

Employee.java类

@Entity
@Table(name="EMPLOYEE", uniqueConstraints={@UniqueConstraint(columnNames={"OFFICE_ID","EMPLOYEE_NUMBER"})})
public class Emplyee {

    @Id
    @Column(name = "ID", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "FIRST_NAME", nullable=false, length=30)
    private String firstName;

    @Column(name = "MIDDLE_NAME",length=30)
    private String middleName;

    @Column(name = "LAST_NAME", nullable=false, length=30)
    private String lastName;

    @Column(name = "EMPLOYEE_NUMBER", nullable=false, length=20)    
    private String employeeNumber;

    @ManyToOne
    @JoinColumn(name="OFFICE_ID")
    private Office office;

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the middleName
     */
    public String getMiddleName() {
        return middleName;
    }

    /**
     * @param middleName the middleName to set
     */
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the employeeNumber
     */
    public String getEmployeeNumber() {
        return employeeNumber;
    }

    /**
     * @param employeeNumber the employeeNumber to set
     */
    public void setEmployeeNumber(String employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    /**
     * @return the office
     */
    public Office getOffice() {
        return office;
    }

    /**
     * @param office the office to set
     */
    public void setOffice(Office office) {
        this.office = office;
    }
}

但我可以在表级别上使用此注释.... uniqueConstraints = {@的UniqueConstraint(COLUMNNAMES = { “FIELD1”, “FIELD2”})}。