Hi,
I would like to include all fields of an embeddable class in a table-level unique constraint on an entity using annotations (JPA or Hibernate). Does anyone know if it is possible to reference the embeddable in the @UniqueConstaint definition without having to list all its column names explicitly?
e.g. I have a simple embeddable with only integer fields such as:
Code:
@Embeddable
public class TravellerNumbers {
@Column
Integer numAdults = 0;
@Column
Integer numInfants = 0;
@Column
Integer numStudents = 0
...
}
And an entity which includes it as a field:
Code:
@Entity
public class TripDetails {
@Id
Long tripId;
@Column
String modeOfTravel;
@Column
TravellerNumbers travellers;
...
}
The only way I can see to include the embeddable fields in the unique constraint (covering other entity fields as well) is to list the column names of the embeddable individually:
Code:
@Table(
uniqueConstraints = {
@UniqueConstraint(
columnNames = {"modeOfTravel", "numAdults", "numInfants", "numStudents", ...})
}
)
While this gives the correct effect it seems a bit unsafe - e.g. if anyone changes the embeddable class they'll need to remember to change the column names of all entities that use it.
Does anyone know if there's a "nicer" way to do it with the annotations syntax, e.g. referencing the "travellers" field as a whole?
(A pretty minor problem admittedly, but I'm just curious because it's cropped up a few times recently in my work)
Thanks,
Stephen