微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java.lang.ClassCastException:org.hibernate.mapping.SingleTableSubclass无法转换为org.hibernate.mapping.RootClass

如何解决java.lang.ClassCastException:org.hibernate.mapping.SingleTableSubclass无法转换为org.hibernate.mapping.RootClass

我有2个类,一个父级和一个子级,我使用this解决方法来定义混合继承策略,但是在使用maven进行编译时,出现了错误

父类

<FlatList keyExtractor={(item,index) => item.id} data={courseGoals} renderItem={itemData =>itemData.item.value } />

儿童班:

@Entity
@Table(name="ITEMS")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@discriminatorColumn(name="ITEM_CATEGORY",discriminatorType=discriminatorType.INTEGER)
  public class Item {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name = "ITEM_ID")
  protected Long itemId;
.
.
.}

错误

 @Entity
    @SecondaryTable(name="TASKS",pkJoinColumns = 
    {@PrimaryKeyJoinColumn(name = "ITEM_ID",referencedColumnName = "ITEM_ID")})
    @discriminatorValue(value=ItemCategory.Values.TASK)
    public class Task extends Item {
        //no @Id field}

pom.xml:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method Failed; nested exception is java.lang.classCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass
Caused by: java.lang.classCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass

DDL:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>MysqL</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

解决方法

我尝试将您的映射(稍作更改)与您的休眠版本5.2.14.Final(spring-boot-starter-data-jpa 2.0.0.RELEASE)完全相同:

表格:

CREATE TABLE ITEMS (
   ITEM_ID bigint auto_increment primary key,ITEM_CATEGORY bigint,RELATED_TO_ITEM bigint,FOREIGN KEY(RELATED_TO_ITEM) references ITEMS(ITEM_ID)
);

insert into ITEMS
values (1,1,null),(2,1),(3,(4,2);

CREATE TABLE TASKS(
   ITEM_ID bigint primary key,code varchar(100),FOREIGN KEY(ITEM_ID) references ITEMS(ITEM_ID)
);

insert into TASKS
values (1,'TASK 1'),'Task 2'),'Task 4');

休眠映射:

@Entity
@Table(name = "ITEMS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="ITEM_CATEGORY",discriminatorType = DiscriminatorType.INTEGER)
public class Item
{
   private Long itemId;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ITEM_ID")
   public Long getItemId()
   {
      return itemId;
   }
   public void setItemId(Long itemId)
   {
      this.itemId = itemId;
   }
}

@Entity
@SecondaryTable(name="TASKS",pkJoinColumns = {
   @PrimaryKeyJoinColumn(name = "ITEM_ID",referencedColumnName = "ITEM_ID")
})
@DiscriminatorValue("1")
public class Task extends Item
{
   private String code;

   @Column(table = "TASKS",name = "code")
   public String getCode()
   {
      return code;
   }
   public void setCode(String code)
   {
      this.code = code;
   }
}

@Entity
@DiscriminatorValue("0")
public class Job extends Item
{

}

以及用于选择测试的简单代码:

List<Item> items = session.createQuery("select i from Item i",Item.class).getResultList();

将生成以下sql:

/* select i from Item i */
select
  item0_.ITEM_ID as ITEM_ID2_0_,item0_1_.code as code1_1_,item0_.ITEM_CATEGORY as ITEM_CAT1_0_ 
from DB_A.ITEMS item0_ 
left outer join DB_A.TASKS item0_1_ on item0_.ITEM_ID=item0_1_.ITEM_ID

并插入测试:

Task task = new Task();
task.setCode("my new task 1");
session.persist(task);

将生成以下sql:

 /* insert com.sternkn.hibernate.model.Task */
insert into DB_A.ITEMS(ITEM_CATEGORY)  
values (1)

/* insert com.sternkn.hibernate.model.Task */
insert into DB_A.TASKS(code,ITEM_ID) 
values (?,?)

如果这样做没有帮助,请提供您的异常的完整堆栈跟踪和继承层次结构中所有实体的完整代码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。