同一实体上的OneToMany复合主键

时间:2013-04-16 23:52:41

标签: hibernate jpa annotations one-to-many

我想做以下事情:

@Entity
@Table(name="things")
public class Thing
{
    @Id
    private String id;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private Set<Thing> alreadyMatchedThings;

    //getters and setters
}

正如预期的那样,这会创建table things_things,并且因为两列有时会重复相同的值,所以我真正想要的是将这两列(id和外键)视为复合主键。

不出意外,我收到错误重复条目'1165f4a1-c369-41ea-888c-0e3798deb04f'用于键'alreadyMatchedThings_id'

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

好吧,我想我可以简单地选择:

@Entity
@Table(name="things")
public class Thing
{
    @Id
    private String id;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn(name="sourcethingid")
    private Set<MatchedThing> matchedThings;

    //getters and setters
}

,其中

@Entity
public class MatchedThings
{
    @Id
    private String id;

    private Thing matchedThing;

    //getters and setters
}

干得好,我。