2009-12-03 55 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