Hibernate 继承映射未知 属性
Hibernate inheritance mapping unknown property
我正在努力处理我的继承结构,我有一个映射的超class,它包含具体classes 中的公共字段。这个 superclass 与 "wrapper" 对象有一对一的映射关系。
对象看起来像这样;
@Entity
public class Wrapper {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "wrapper_id", nullable = false)
private Long wrapperId;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "wrapper")
@Cascade(CascadeType.SAVE_UPDATE)
private Base base;
public Long getWrapperId() {
return wrapperId;
}
public void setWrapperId(Long wrapperId) {
this.wrapperId = wrapperId;
}
public Base getBase() {
return base;
}
public void setBase(Base base) {
this.base = base;
}
}
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Base {
@OneToOne(fetch = FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn(name = "wrapper_id")
protected Wrapper wrapper;
public Wrapper getWrapper() {
return wrapper;
}
public void setWrapper(Wrapper wrapper) {
this.wrapper = wrapper;
}
}
@Entity
public class SubA extends Base {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sub_a_id", nullable = false)
private Long subAId;
public Long getSubAId() {
return subAId;
}
public void setSubAId(Long subAId) {
this.subAId = subAId;
}
}
为简单起见,我只包含一个具体的 class,但我有多个。
当我在包装器对象中没有对 "Base" 的引用时,此映射效果很好。一旦我尝试在包装器和基础之间添加双向关系,我就开始收到此错误....这没有意义,因为该字段在那里。
Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.xxx.Wrapper.base, referenced property unknown: com.xxx.Base.wrapper
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:153)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:453)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
... 50 more
我错过了什么?
谢谢,
引自 Java 平台企业版:Java EE 教程:
37.2.2 Mapped Superclasses
Entities may inherit from superclasses that contain persistent state
and mapping information but are not entities. That is, the superclass
is not decorated with the @Entity annotation and is not mapped as an
entity by the Java Persistence provider. These superclasses are most
often used when you have state and mapping information common to
multiple entity classes. Mapped superclasses are specified by
decorating the class with the annotation
javax.persistence.MappedSuperclass:
...
Mapped superclasses cannot be queried and cannot be used in
EntityManager or Query operations. You must use entity subclasses of
the mapped superclass in EntityManager or Query operations. Mapped
superclasses can't be targets of entity relationships.
所以看起来你不能在实体关系中使用这个 Base class:
@OneToOne(fetch = FetchType.LAZY, mappedBy = "wrapper")
@Cascade(CascadeType.SAVE_UPDATE)
private Base base;
看起来像这个 hibernate
错误:Hibernate complains about an unknown mappedBy property when mapping a bidirectional OneToOne relation with a derived identifier,仅在 hibernate 4.2.2、4.3.0.Beta3 后期版本中得到修复。
我们最终用不同的选项(映射超级class、分层等)做了很多原型设计并权衡了选项。
最后,我们决定创建对象层次结构,结合 @Entity 注释、SINGLE_TABLE 的继承策略,并使用鉴别器值在不牺牲太多的情况下为我们提供我们所需要的东西。
谢谢大家的建议。
我正在努力处理我的继承结构,我有一个映射的超class,它包含具体classes 中的公共字段。这个 superclass 与 "wrapper" 对象有一对一的映射关系。
对象看起来像这样;
@Entity
public class Wrapper {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "wrapper_id", nullable = false)
private Long wrapperId;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "wrapper")
@Cascade(CascadeType.SAVE_UPDATE)
private Base base;
public Long getWrapperId() {
return wrapperId;
}
public void setWrapperId(Long wrapperId) {
this.wrapperId = wrapperId;
}
public Base getBase() {
return base;
}
public void setBase(Base base) {
this.base = base;
}
}
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Base {
@OneToOne(fetch = FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn(name = "wrapper_id")
protected Wrapper wrapper;
public Wrapper getWrapper() {
return wrapper;
}
public void setWrapper(Wrapper wrapper) {
this.wrapper = wrapper;
}
}
@Entity
public class SubA extends Base {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sub_a_id", nullable = false)
private Long subAId;
public Long getSubAId() {
return subAId;
}
public void setSubAId(Long subAId) {
this.subAId = subAId;
}
}
为简单起见,我只包含一个具体的 class,但我有多个。
当我在包装器对象中没有对 "Base" 的引用时,此映射效果很好。一旦我尝试在包装器和基础之间添加双向关系,我就开始收到此错误....这没有意义,因为该字段在那里。
Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.xxx.Wrapper.base, referenced property unknown: com.xxx.Base.wrapper
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:153)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:453)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
... 50 more
我错过了什么? 谢谢,
引自 Java 平台企业版:Java EE 教程:
37.2.2 Mapped Superclasses
Entities may inherit from superclasses that contain persistent state and mapping information but are not entities. That is, the superclass is not decorated with the @Entity annotation and is not mapped as an entity by the Java Persistence provider. These superclasses are most often used when you have state and mapping information common to multiple entity classes. Mapped superclasses are specified by decorating the class with the annotation javax.persistence.MappedSuperclass:
...
Mapped superclasses cannot be queried and cannot be used in EntityManager or Query operations. You must use entity subclasses of the mapped superclass in EntityManager or Query operations. Mapped superclasses can't be targets of entity relationships.
所以看起来你不能在实体关系中使用这个 Base class:
@OneToOne(fetch = FetchType.LAZY, mappedBy = "wrapper")
@Cascade(CascadeType.SAVE_UPDATE)
private Base base;
看起来像这个 hibernate
错误:Hibernate complains about an unknown mappedBy property when mapping a bidirectional OneToOne relation with a derived identifier,仅在 hibernate 4.2.2、4.3.0.Beta3 后期版本中得到修复。
我们最终用不同的选项(映射超级class、分层等)做了很多原型设计并权衡了选项。
最后,我们决定创建对象层次结构,结合 @Entity 注释、SINGLE_TABLE 的继承策略,并使用鉴别器值在不牺牲太多的情况下为我们提供我们所需要的东西。
谢谢大家的建议。