This is a Hibernate/JPA question.
I have a set of Schedule
objects, each including several Step
s of various StepType
s. Each StepType
is unique within a schedule, so the steps are stored as a Map<StepType, Step>
. The code is annotated as:
@Entity
public class Schedule implements Serializable {
@MapKey(name="type")
@OneToMany(cascade=CascadeType.ALL, mappedBy="schedule")
private Map<StepType, Step> steps;
}
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"schedule", "type"})})
public class Step implements Serializable {
@ManyToOne
private StepType type;
@ManyToOne
private Schedule schedule;
}
Unfortunately, this is apparently not allowed. A MapKey is required to have a uniqueness constraint associated with it, which type
does not, since step type is only unique within a particular schedule.
Is there a better way to annotate this structure, or am I going to have to rethink the object model for Hibernate's sake? Or should this violation be harmless? (The Map is refusing to load properly, but I can't confirm for sure that this is why.)