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

4179 ワード

最近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;
    }
}

最後にアプリケーションを添付します.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 :


		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			mysql
			mysql-connector-java
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		

		
			junit
			junit
			4.12
		
		
			org.springframework.boot
			spring-boot-test
			RELEASE
		
		
			org.springframework
			spring-test
			4.3.12.RELEASE