【问题标题】:Hibernate mapping annotation skipped by the system系统跳过的 Hibernate 映射注解
【发布时间】:2014-06-22 18:40:07
【问题描述】:

我对休眠下的映射有疑问。我有一个实现接口的抽象类:

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractPromotion implements IPromotion {
}

这个抽象类是超类(正如您在注释中看到的那样),3 个带有@Entity 的映射类扩展了这个超类。当我启动“maven install”时,我在控制台中收到此失败消息,它说:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: product.model.Product.promotion[product.promotion.AbstractPromotion]

我不明白,因为有问题的类被映射(作为扩展它的类),但就像跳过了注释...... 任何帮助将不胜感激

最好的问候

【问题讨论】:

    标签: java spring hibernate maven


    【解决方案1】:

    AbstractPromotion 不是实体。它是MappedSuperclass。要应用继承策略并成为关联的目标,类必须是实体。将@MappedSuperclass 替换为@Entity

    Mappedsuperclass 仅用于能够在几个不相关的实体中继承公共字段和方法(例如,在几个不相关的实体中继承 idcreationDate)。

    【讨论】:

      【解决方案2】:

      如果您想为所有需要的子类共享同一个数据库表,请使用鉴别器。 在主类中你需要添加这个注解。

        @Entity
        @Table(name = "foo")
        @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
        @DiscriminatorColumn(name = "foo_type", discriminatorType = DiscriminatorType.STRING
      

      并且在子类中这个注解

             @Entity
             @DiscriminatorValue("The value of your foo_type that discriminate this entity")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 2016-09-20
        相关资源
        最近更新 更多