2014-02-22 35 views

回答

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」})}。