2009-12-03 31 views
4

校園裏有@OneToMany Buildings Building有@OneToMany Rooms。 的房間名字必須是一個校園 中是唯一的(例如校園-A,塊A,房間-A和校園-B,塊A,房間-A應儘可能保存)@UniqueConstraint檢查JPA中的多個表格

有沒有可能在Room實體上定義這樣一個唯一的約束?

回答

4

我可能在這裏錯過了一些東西,但是沒有在校園內建立獨特的名字?所以,你需要確保房間的名字是給建築物內唯一的:

@Entity 
@Table(name="rooms", 
    uniqueConstraints = {@UniqueConstraint(columnNames={"building_id","name"})} 
) 
public class Room { 
    ... 
    @ManyToOne 
    @JoinColumn(name = "building_id") 
    private Building building; 
    ... 
} 

如果你現在做內CampusBuilding的名字一樣,你要善於去。

+0

也幫了我!歡呼+1。 – monojohnny 2010-12-10 20:58:37