我们正在使用Hibernate为我们的实体生成JPA Metamodel - 类。
对于大多数情况,这种情况非常好,但如果与@ManyToOne
中的实体存在关联(@Embeddable
),那么就会生成SingularAttribute
类是这样实现的(遵循"代间隙模式"):
@Entity
public class EntityA extends EntityABase {
....
}
@MappedSuperClass
public abstract class EntityABase {
@EmbeddedId
private EntityAPrimaryKey primaryKey;
}
@Embeddable
public class EntityAPrimaryKey extends EntityAPrimaryKeyBase {
...
}
@MappedSuperClass
public class EntityAPrimaryKeyBase {
@ManyToOne
@NotNull
private EntityB entityB;
private String someText;
}
结果就像这样
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(EntityAPrimaryKeyBase.class)
public abstract class EntityAPrimaryKeyBase_ {
public static volatile SingularAttribute<EntityAPrimaryKeyBase, String> someText;
}
所以,&#34;普通&#34;字段someText生成正常,但缺少与EntityB的关系的属性。
预期输出为
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(EntityAPrimaryKeyBase.class)
public abstract class EntityAPrimaryKeyBase_ {
public static volatile SingularAttribute<EntityAPrimaryKeyBase, String> someText;
public static volatile SingularAttribute<EntityAPrimaryKeyBase, EntityB> entityB;
}
所有其他元模型类都生成正常(EntityB,EntityA,EntityABase等)
我尝试删除EntityAPrimaryKey
和EntityAPrimaryKeyBase
之间的间接(并使用@ EntityAPrimaryKeyBase
注释Embeddable
),但这并没有改变输出。
为什么没有生成属性entityB
的任何想法?会非常有帮助的!
答案 0 :(得分:0)
EmbeddedId注释应用于实体类或映射超类的持久字段或属性,以表示作为可嵌入类的复合主键。可嵌入类必须注释为Embeddable。[104] 不支持在嵌入式ID类中定义的关系映射。
指定为映射超类的类可以以与实体相同的方式映射,除了映射仅适用于其子类,因为映射的超类本身不存在表。
您不能在要用作@EmbeddedId的@Embeddable中进行关系映射。即使你在@MappedSuperclass中有关系,2.11.2也指出映射应用于子类,在本例中是@Embeddable。
答案 1 :(得分:0)
对于记录,改变类结构(从而根据规范制作)解决了问题:
@Entity
public class EntityA extends EntityABase {
....
}
@MappedSuperClass
public abstract class EntityABase {
@EmbeddedId
private EntityAPrimaryKey primaryKey;
@ManyToOne
@MapsId("entityBID")
private EntityB entityB;
}
@Embeddable
public class EntityAPrimaryKey extends EntityAPrimaryKeyBase {
...
}
@MappedSuperClass
public class EntityAPrimaryKeyBase {
private Long entityBID;
private String someText;
}