SpringBoot:Error creating bean with name 'entityManagerFactory' defined in class path resource

博主学习SpringBoot连接MySQL数据库时,遇到entityManagerFactory相关错误。经不断尝试,发现问题出在导入的包上,文中给出了正确导入包后的完整代码,还附上了application.properties和pom.xml文件的配置代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在学习SpringBoot,在学习通过SpringBoot连接mysql数据库时,报出这样的错误:

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 org.hibernate.AnnotationException: No identifier specified for entity: com...

这个错误一度让我崩溃,各种Google后还是未能解决,我知道这又是一个非常规错误。但是在不断尝试下,终于找到问题所在。先看下源码:

package com.demo;

import org.springframework.data.annotation.Id;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import java.io.Serializable;

@Entity
public class Girl implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @Column
    private String cupSize;

    private Integer age;

    public Girl() {
    }

    public Girl(Integer id,  String cupSize, Integer age) {
        this.id = id;
        this.cupSize = cupSize;
        this.age = age;
    }

    /*
     *此处省略部分代码
     */
}

咋一看没问题,其实问题出在导入的包上。这里用到了@Id的注解,但是导入的包不应该是:

import org.springframework.data.annotation.Id;

而应该用:

import javax.persistence.Id;

所以,完整正确的代码如下:

package com.yangchen;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import java.io.Serializable;
import javax.persistence.Id;

@Entity
public class Girl implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @Column
    private String cupSize;

    private Integer age;

    public Girl() {
    }

    public Girl(Integer id,  String cupSize, Integer age) {
        this.id = id;
        this.cupSize = cupSize;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

最后在附上application.properties和pom.xml文件的配置代码:

yaml格式的application代码:

spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/girl?useSSL=false
    username: 你的数据库用户名
    password: 数据库密码
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true



properties类型的application代码:

spring.datasource.url=jdbc:mysql://localhost:3306/girl?characterEncoding=utf8&useSSL=true
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
pom.xml中的dependcies代码:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-test</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.3.12.RELEASE</version>
		</dependency>
	</dependencies>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值