java - Hibernate 继承映射未知属性

标签 java hibernate inheritance one-to-one mappedsuperclass

我正在努力解决我的继承结构,其中我有一个映射的父类(super class),其中包含具体类中的公共(public)字段。该父类(super class)与“包装器”对象具有一对一的映射。

对象看起来像这样;

@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;
    }

}

为了简单起见,我只包含了一个具体类,但我有几个。

当我在包装器对象中没有对“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.

所以看起来你不能在实体关系中使用这个基类:

@OneToOne(fetch = FetchType.LAZY, mappedBy = "wrapper")
@Cascade(CascadeType.SAVE_UPDATE)
private Base base;

关于java - Hibernate 继承映射未知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959206/

相关文章:

java - Android:在多线程Service中,LocalBroadcastManager安全吗?

java - 为什么 Fortify SCA 针对我的项目中不再存在的文件报告问题?

java - 为什么我不能执行返回 Iterator().next().length() 的方法?

java - Hibernate Criteria API - 添加标准 : string should be in collection

c# - 接口(interface)继承和抽象方法覆盖

c++ - 对象标识符、返回值或变量?

c++ - 在虚函数表中找不到派生类的虚函数地址

java - Map.Entry 接口(interface)如何在不创建对象的情况下调用 getValue()/getKey() 方法?

java - Hibernate Search 对一个查询非常慢,对另一个查询很快

java - 如何在 Hibernate 中使用 org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmountAndCurrency 类型映射 Joda Money?