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

如何在Hibernate中创建表上的唯一约束?,hibernate,Hibernate,如何在hibernate中创建唯一约束 我有office类和Employee类,我想在每个办公室中创建唯一的员工Id 请建议我……我得到了如何在hibernate中创建唯一约束的答案 Office.java类 @Entity @Table(name="OFFICE") public class Office { @Id @Column(name = "ID", unique = true, nullable = false) @GeneratedValue(strate

如何在hibernate中创建唯一约束

我有office类和Employee类,我想在每个办公室中创建唯一的员工Id


请建议我……

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

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;
    }
}
但我可以在类级别上为表使用此注释。。。。 uniqueConstraints={@UniqueConstraint(columnNames={“FIELD1”,“FIELD2”})

@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;
    }
}