【问题标题】:Object [id=1] was not of the specified subclass对象 [id=1] 不属于指定的子类
【发布时间】:2018-07-18 15:05:59
【问题描述】:

我正在将一部电影添加到基本 MovieEntity 类,然后是另一个标题 MovieOtherEnity 继承自 MovieInfoEntity 的类

MovieInfoEntity 有一个指向MovieEntity 的外键。 MovieEntity 具有继承自 MovieInfoEntity 类的实体列表。

我有一个测试,每次测试后都会清除基地中的物体。

/**
 * Clean out the db after every test.
 */
@After
public void cleanup() {
    this.contributionRepository.deleteAll();
    this.messageRepository.deleteAll();
    this.movieRepository.deleteAll(); // here he throws an exception
    this.movieInfoRepository.deleteAll();
    this.userRepository.deleteAll();
}

上面代码中标记的行被抛出

org.springframework.orm.ObjectRetrievalFailureException: Object [id=1] was not of the specified subclass [com.jonki.popcorn.core.jpa.entity.movie.MovieBoxOfficeEntity] : loaded object was of wrong class class com.jonki.popcorn.core.jpa.entity.movie.MovieOtherTitleEntity; nested exception is org.hibernate.WrongClassException: Object [id=1] was not of the specified subclass [com.jonki.popcorn.core.jpa.entity.movie.MovieBoxOfficeEntity] : loaded object was of wrong class class com.jonki.popcorn.core.jpa.entity.movie.MovieOtherTitleEntity

我会提到它测试方法,我将 Object MovieOtherTitle 项添加到 MovieEntity 对象的列表中。

【问题讨论】:

    标签: java spring spring-mvc spring-boot


    【解决方案1】:

    确保在数据库中,您没有在鉴别器列中没有定义值的条目(可能是手动输入),或者该列中的值不是您的任何实体的鉴别器值。这只会影响检索,不会影响保存。

    【讨论】:

      【解决方案2】:

      我的问题是,我需要将 DiscrimininatorValue 注释放在顶部

          @DiscriminatorValue("BillAddress")
          public class BillAddress extends Address {
                ...
          }
      

      为此浪费了两个小时,在这里找到了解决方案:jboss forum

      【讨论】:

      • 就是这样,谢谢
      【解决方案3】:

      似乎不同表之间的 id 具有相同的值,因此当 hibernate 尝试加载具有特定 id 的实体时,如果内存中已经存在具有相同 id 的另一个实体,那么 hibernate 会抱怨这个问题。

      【讨论】:

        猜你喜欢
        • 2017-10-06
        • 1970-01-01
        • 1970-01-01
        • 2019-08-26
        • 1970-01-01
        • 2017-07-24
        • 1970-01-01
        • 1970-01-01
        • 2019-10-11
        相关资源
        最近更新 更多