【发布时间】:2020-09-06 04:12:34
【问题描述】:
我有 2 个类,一父一子,我使用 this 解决方法来定义混合继承策略,但是在使用 maven 编译时,出现错误。
父类:
@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}
错误:
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
pom.xml:
<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>
DDL:
CREATE TABLE ITEMS(
ITEM_ID bigint auto_increment primary key,
.
.
.
FOREIGN KEY(RELATED_TO_ITEM) references ITEMS(ITEM_ID)
);
CREATE TABLE TASKS( #INCLUDES REPEATED TASKS, ONETIME TASKS, LEARNING DUTY AND RESPONSES
ITEM_ID bigint primary key,
.
.
.
FOREIGN KEY(ITEM_ID) references ITEMS(ITEM_ID)
);
【问题讨论】:
-
能否提供您的表格的 DDL。
-
@SternK,已添加,谢谢
标签: java spring-boot hibernate jpa spring-data-jpa