@UniqueConstraint check multiple tables in JPA

On campus, the @OneToMany Building Building has @OneToMany Rooms. Room names must be unique on campus (e.g., campus-A, block-A, room-A and campus-B, block-A, room-A must be saved)

Is it possible to define such a unique constraint for the Room object?

+4
source share
1 answer

Maybe I'm missing something, but they don’t build names that are unique on campus? Therefore, you need to make sure that the room names are unique within the given building:

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

If you are now doing the same for the Building names inside Campus , you should be good to go.

+4
source

All Articles