Java ElementCollection中是否不支持将可嵌入引用与可嵌入引用一起使用?

Java ElementCollection中是否不支持将可嵌入引用与可嵌入引用一起使用?,java,hibernate,jpa,Java,Hibernate,Jpa,我想定义一个对象模型,如下所示: A包含一个X的列表,每个X包含一个Y,每个Y引用一个B。我已经将A和B设置为实体,并且X和Y都是可嵌入的。A包含X类型列表的@ElementCollection。下面是这些类的代码 当要求Hibernate(3.6.10)使用SchemaExport为该模型生成SQL时,我希望有三个表:A、B和一个集合表,用于从A到B的引用,其中包含A.id、B.id和一个order列以保持列表顺序。相反,Hibernate给了我以下信息: create table Colle

我想定义一个对象模型,如下所示:

A包含一个X的列表,每个X包含一个Y,每个Y引用一个B。我已经将A和B设置为
实体
,并且X和Y都是
可嵌入的
。A包含X类型列表的
@ElementCollection
。下面是这些类的代码

当要求Hibernate(3.6.10)使用
SchemaExport
为该模型生成SQL时,我希望有三个表:A、B和一个集合表,用于从A到B的引用,其中包含A.id、B.id和一个order列以保持列表顺序。相反,Hibernate给了我以下信息:

create table CollectionOfB (A_ID bigint not null, embeddableAList_ORDER integer not null, primary key (A_ID, embeddableAList_ORDER))
create table EntityA (id bigint not null, primary key (id))
create table EntityB (id bigint not null, primary key (id))
alter table CollectionOfB add constraint FKFEABC9AD9AECF0BB foreign key (A_ID) references EntityA
请注意,集合表中没有B.id列,也没有应该有的外键约束。在我看来,我想要的模式应该得到支持。它是否有一个很好的理由(如果是的话,原因是什么?),或者这是Hibernate或JPA中的一个bug/缺失特性


作为一个实验,我折叠了我的
embeddeble
关系,使它只有一个级别(完全删除Y,将对B的引用放在X的内部)。通过这一更改,模式按照我的预期生成,因此使用
ElementCollection
的基本思想是,其中
可嵌入的
具有
多个
功能:

create table CollectionOfB (A_ID bigint not null, B_ID bigint, embeddableAList_ORDER integer not null, primary key (A_ID, embeddableAList_ORDER))
create table EntityA (id bigint not null, primary key (id))
create table EntityB (id bigint not null, primary key (id))
alter table CollectionOfB add constraint FKFEABC9AD9AED651B foreign key (B_ID) references EntityB
alter table CollectionOfB add constraint FKFEABC9AD9AECF0BB foreign key (A_ID) references EntityA
此外,如果我创建一个不同的
实体
C,并将其定义为具有一个
可嵌入的
X(一个
元素集合
)它正确地引用了B。因此,有两个级别的
可嵌入的
,其中较低级别有一个
多个
的想法也起作用


@可嵌入
公共类EmbeddedX{
//注释掉“折叠”X和Y
@嵌入
嵌入嵌入;
//在“折叠”X和Y中添加注释
//@JoinColumn(name=“B_ID”)
//@ManyToOne(fetch=FetchType.EAGER)
//实体b实体b;
}
@可嵌入
嵌入的公共类{
@JoinColumn(name=“B_ID”)
@manytone(fetch=FetchType.EAGER)
实体b实体b;
}
@实体
公共类实体{
@身份证
长id;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name=“collectionFB”,joinColumns=@JoinColumn(name=“A\u ID”))
@列(name=“Bs”)
@订单列
清单1、清单1;
}
@实体
公共类实体B{
@身份证
长id;
}
public void testSchema()引发异常{
Configuration config=new Configuration().addAnnotatedClass(EntityA.class).addAnnotatedClass(EntityB.class);
config.setProperty(Environment.dialent,hsqldealent.class.getName());
新建SchemaExport(config).create(true,false);
}

Note,我决定在Hibernate的JIRA上发布一期:现在是2016年,Hibernate-5.1仍然存在。
@Embeddable
public class EmbeddedX {
    // Comment out to "collapse" X and Y
    @Embedded
    EmbeddedY embeddedY;

    // Comment in to "collapse" X and Y
    //    @JoinColumn(name = "B_ID")
    //    @ManyToOne(fetch = FetchType.EAGER)
    //    EntityB entityB;
}


@Embeddable
public class EmbeddedY {
    @JoinColumn(name = "B_ID")
    @ManyToOne(fetch = FetchType.EAGER)
    EntityB entityB;
}

@Entity
public class EntityA {
    @Id
    Long id;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "CollectionOfB", joinColumns = @JoinColumn(name = "A_ID"))
    @Column(name = "Bs")
    @OrderColumn
    List<EmbeddedA> embeddableAList;
}

@Entity
public class EntityB {
    @Id
    Long id;
}

public void testSchema() throws Exception {
    Configuration config = new Configuration().addAnnotatedClass(EntityA.class).addAnnotatedClass(EntityB.class);
    config.setProperty(Environment.DIALECT, HSQLDialect.class.getName());

    new SchemaExport(config).create(true, false);
}