最近在学习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>