在 Spring Boot Web 应用程序中创建名称为“entityManagerFactory”的 bean 时出错
Posted
技术标签:
【中文标题】在 Spring Boot Web 应用程序中创建名称为“entityManagerFactory”的 bean 时出错【英文标题】:Error creating bean with name 'entityManagerFactory' error in a Spring boot web application 【发布时间】:2020-04-20 07:44:23 【问题描述】:我是 Spring Boot 编码的初学者 当我运行我的 Spring Boot Web 应用程序时,出现以下错误。
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
我正在使用 STS Eclipse 和 mysql 数据库
Application.Properties 中的我的连接字符串是
spring.datasource.url=jdbc:mysql://localhost:3306/dreamhospital?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.main.banner-mode=off
server.port=8100
这是我的 pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.DreamHospital</groupId>
<artifactId>DreamHospital</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DreamHospital</name>
<description>Demo project for Spring Boot</description>
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<java.version>1.8</java.version>
</properties>
<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-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.16.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是我的实体类: 这是患者实体:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
@DiscriminatorValue("patient")
public class patient implements Serializable
@Id
private int id;
private String nom;
private String prenom;
@ManyToOne
@JoinColumn(name="idLit")
lit lit;
@ManyToOne
@JoinColumn(name="idM")
medecin medecin;
这是用户实体:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="fonction" , discriminatorType=DiscriminatorType.STRING,length=10)
public class User implements Serializable
@Id
private String UserName;
private String Password;
@Column(name="fonction", insertable = false, updatable = false)
protected String fonction;
这是医学实体:
@Data
@Entity
@DiscriminatorValue("medecin")
public class medecin extends User implements Serializable
@Id @GeneratedValue
private UUID idM;
private String nom;
private String specialite;
@OneToMany(mappedBy="idM")
List<patient> patients;
我尝试了***中提出的许多解决方案,但都没有成功
【问题讨论】:
你有扩展另一个类的实体类吗? 请提供实体类 @benjaminc 我编辑了我的问题并添加了您可以检查的实体类 ***.com/questions/12087011/… 的可能重复项 【参考方案1】:显然User
和medecin
的映射存在问题,您不能在超类中拥有@Id
而在子类中拥有另一个@Id
。从子类 (medecin
) 中的 idM
中删除 @Id
应该防止异常。
【讨论】:
@EyaTayari 还是同样的异常?以上是关于在 Spring Boot Web 应用程序中创建名称为“entityManagerFactory”的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章
我如何在 Spring Boot/MVC 中创建错误处理程序(404、500...)
在 Spring Boot 中创建 ResponseEntity 类的对象的方法
如何在 Spring Boot 应用程序中创建第二个 RedisTemplate 实例