开发者

Using a Hibernate MapKey that's not unique within a table

开发者 https://www.devze.com 2022-12-22 07:33 出处:网络
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<St

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开发者_StackOverflow中文版? (The Map is refusing to load properly, but I can't confirm for sure that this is why.)


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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号