[英]How to map embeddable that contains many-to-many
I have the following DB schema:我有以下数据库架构:
And the following model:以及以下 model:
I'm struggling to map the RootChildren
as an @Embedded
within Root
.我正在努力将 map
RootChildren
作为@Embedded
内的Root
。 I've tried using a @ManyToMany
within RootChildren
to map the set of Child
, but I couldn't make it work.我尝试在
RootChildren
@ManyToMany
map 集合Child
,但我无法使其工作。 It doesn't seem like there's a lot of documentation online on the subject or at least I couldn't find anything that works so hopefully someone will be able to help.似乎没有很多关于这个主题的在线文档,或者至少我找不到任何有用的东西,所以希望有人能够提供帮助。
Note that I do not want to model bi-directional relationships if possible.请注意,如果可能,我不想 model 双向关系。 Thanks!
谢谢!
Erratum: RootChildren#children
should be a SortedSet
.勘误:
RootChildren#children
应该是一个SortedSet
。
Child
cannot be an entity if you want to use embeddables.如果您想使用可嵌入对象,则
Child
不能是实体。
You can map RootChildren
as separate entity with a composite key containing Root
and Child
as a @ManyToOne
association:您可以将 map
RootChildren
作为单独的实体,使用包含Root
和Child
作为@ManyToOne
关联的复合键:
@Entity
@IdClass( PK.class )
public static class RootChildren {
@Id
@ManyToOne(fetch = FetchType.LAZY)
Root root;
@Id
@ManyToOne(fetch = FetchType.LAZY)
Child child;
int ordering;
}
public class PK implements Serializable {
private Root root;
private Child child;
public PK(Root root, Child child) {
this.root = root;
this.child = child;
}
private PK() {
}
//Getters, setters, hashcode and equals are omitted for brevity
}
Alternatively, if you create a separate class like EmbeddedChild
, you could use this mapping:或者,如果您创建一个单独的 class (如
EmbeddedChild
),则可以使用此映射:
@Entity
class Root {
@Embedded
RootChildren children;
}
@Embeddable
class RootChildren {
@ElementCollection
Set<EmbeddedChild> children;
}
@Embeddable
class EmbeddedChild {
}
I did the following mapping in a scratch project I have (running on embedded Derby, but that should not matter) and it seems to work.我在一个临时项目中做了以下映射(在嵌入式 Derby 上运行,但这不重要),它似乎工作。 I did have problem defining the collection as
SortedSet
, hibernate complained that "A sorted collection must define and ordering or sorting";我确实在将集合定义为
SortedSet
时遇到了问题,hibernate 抱怨说“排序的集合必须定义和排序或排序”; it would go away if I added an ordering property (visible to the state of the entity), which is not what you want.如果我添加了一个排序属性(对实体的 state 可见),它将 go 消失,这不是你想要的。
@Entity
@Table(name="SO_ROOT")
public class Root {
@Id
@GeneratedValue(strategy=SEQUENCE)
private long id;
@Embedded
private RootChildren children;
// getters and setters
}
@Embeddable
public class RootChildren {
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "SO_ROOT_CHILDREN",
joinColumns = @JoinColumn(name = "root_id"),
inverseJoinColumns = @JoinColumn(name = "child_id")
)
@OrderColumn(name = "xorder")
private List<Child> children;
// getters and setters
}
@Entity
@Table(name="SO_CHILD")
public class Child {
@Id
@GeneratedValue(strategy=SEQUENCE)
private long id;
// getters and setters
}
Test code:测试代码:
EntityManager em = null;
try {
em = ...
em.getTransaction().begin();
Root root1 = new Root();
Root root2 = new Root();
Child r1c1 = new Child();
Child r1c2 = new Child();
Child r2c1 = new Child();
Child r2c2 = new Child();
RootChildren rc1 = new RootChildren();
rc1.setChildren(Arrays.asList(r1c1,r1c2));
RootChildren rc2 = new RootChildren();
rc2.setChildren(Arrays.asList(r2c1,r2c2));
root1.setChildren(rc1);
root2.setChildren(rc2);
em.persist(root1);
em.persist(root2);
em.getTransaction().commit();
}
finally {
EntityManagerUtil.safeClose(em);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.