tags:

views:

50

answers:

1

This is a Hibernate/JPA question.

I have a set of Schedule objects, each including several Steps of various StepTypes. 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.)

A: 

According to the spec, you're right, unicity is required. But according to me (and you), it would seem logical to only require "localized unicity". I've juste found that the "Pro JPA 2" Book states the same: "not required to be unique accross the entire domain of this entity type. It only need to be uniwue within the scaope of the relationship".

I need the exact same behaviour as you. I'll test code soon.

Here's a link to the exerpt of the book: http://books.google.com/books?id=j84hdeHH2PYC&amp;pg=PA119&amp;lpg=PA119&amp;dq=jpa+mapkey+uniqueness&amp;source=bl&amp;ots=C_TluOiJxZ&amp;sig=EibqwD-3slqk7pnEJDK38k4T6Zc&amp;hl=fr&amp;ei=147MTJLTHNC6jAecuanYBw&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=9&amp;ved=0CEgQ6AEwCA#v=onepage&amp;q=t%20should%20also%20be%20unique%2C%20although%20it%20is%20not%20&amp;f=false

FMarot