Hibernate – @uniqueConstraint Checks multiple tables in JPA

The @OneToMany building on campus has @OneToMany guest rooms.
Room names must be unique on campus
(e.g. campus-A, block-A, room-A and Campus-B, block-A, room-A should be able to store)

Is it possible to define such a unique constraint on the Room entity?

I may be missing something here, but there is no unique name established on campus? Therefore, you need to ensure that the room name is unique within a given building:

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

If you do the same for the building name on campus now Thing, then you should go well.

@OneToMany建筑 has @OneToMany guest rooms on campus.
The room name must be unique on campus
(e.g. Campus-A, block-A, room-A and campus-B, block-A, room-A should be able to store)

Is it possible to define such a unique constraint on the Room entity?

I may have missed something here, but there is no unique name established on campus? Therefore, you need to ensure that the room name is unique within a given building:

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

If you do the same for the building name on campus now Thing, then you should go well.

Leave a Comment

Your email address will not be published.