Java Spring Boot - “创建名为 'entityManagerFactory' 的 bean 时出错” - 开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41385044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - "Error creating bean with name 'entityManagerFactory'" - Start
提问by ReNaR
I have seen a lot of questions but the answers are not satisfying my problem... I start with Spring Boot and I am completely lost.
我看到了很多问题,但答案并不能满足我的问题......我从Spring Boot开始,我完全迷失了。
The error is :
错误是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: pack.datas.entities.Degree
/// others lines
/// 其他行
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: pack.datas.entities.Degree
The entry point :
入口点:
package pack;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@RestController
@EntityScan
public class CandidatesRecruitmentApplication {
public static void main(String[] args) {
SpringApplication.run(CandidatesRecruitmentApplication.class, args);
}
}
The classes :
课程:
package pack.datas;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.stereotype.Component;
@Component
@Configuration
@EnableJpaRepositories
public class DatasFactory {
private DataSource dataSource;
public DatasFactory() {
super();
}
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
if(this.dataSource == null)
return this.dataSource = DataSourceBuilder.create().build();
else
return this.dataSource;
}
package pack.datas.entities;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import org.springframework.data.annotation.Id;
@Entity
@Table(name = "degree")
public class Degree implements Serializable {
private static final long serialVersionUID = -8900492704842756948L;
@Id
@GeneratedValue
@Column(name = "pk_id")
private Integer id;
@Column(name = "degree_i")
private Integer degreeId;
@Column(name = "degree_s_en")
private String degreeEn;
@Column(name = "degree_s_fr")
private String degreeFr;
protected Degree() {
}
public Degree(Integer id, Integer degreeId, String degreeEn, String degreeFr) {
super();
this.id = id;
this.degreeId = degreeId;
this.degreeEn = degreeEn;
this.degreeFr = degreeFr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getDegreeId() {
return degreeId;
}
public void setDegreeId(Integer degreeId) {
this.degreeId = degreeId;
}
public String getDegreeEn() {
return degreeEn;
}
public void setDegreeEn(String degreeEn) {
this.degreeEn = degreeEn;
}
public String getDegreeFr() {
return degreeFr;
}
public void setDegreeFr(String degreeFr) {
this.degreeFr = degreeFr;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((degreeEn == null) ? 0 : degreeEn.hashCode());
result = prime * result + ((degreeFr == null) ? 0 : degreeFr.hashCode());
result = prime * result + ((degreeId == null) ? 0 : degreeId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Degree other = (Degree) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (degreeEn == null) {
if (other.degreeEn != null)
return false;
} else if (!degreeEn.equals(other.degreeEn))
return false;
if (degreeFr == null) {
if (other.degreeFr != null)
return false;
} else if (!degreeFr.equals(other.degreeFr))
return false;
if (degreeId == null) {
if (other.degreeId != null)
return false;
} else if (!degreeId.equals(other.degreeId))
return false;
return true;
}
@Override
public String toString() {
return "Degree [ " + degreeId + " - " + degreeEn + "/" + degreeFr + " ]";
}
}
package pack.datas.controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DaoController {
@GetMapping("/")
@ResponseBody
@Transactional(readOnly = true)
String home() {
return "<h1>Test Program</h1>";
}
}
The pom.xml :
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pack</groupId>
<artifactId>candidates-recruitment</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>candidates-recruitment</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.surefire/surefire-api -->
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
<version>2.19.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>org.jboss.repository.releases</id>
<name>JBoss Maven Release Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
The application.yml :
application.yml :
# DATASOURCE (DataSourceProperties)
spring:
datasource:
url: jdbc:postgresql://localhost:5432/recruitments_db
username: postgresql
password: postgresql
driver-class-name: org.postgresql.Driver
# HIBERNATE (HibernateProperties)
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show_sql: true
hbm2ddl.auto: create
#SECURITY (SecuriyProperties)
security:
user:
name: TestU
password: Test1
Please, I need your help !
拜托我需要你的帮忙 !
采纳答案by Piotr Podraza
Seems like you used @Id
annotation from the wrong package in your Degree
class. Change your import from org.springframework.data.annotation.Id
to javax.persistence.Id
. That should help with the exception you're getting.
似乎您@Id
在Degree
班级中使用了错误包中的注释。将您的导入从 更改org.springframework.data.annotation.Id
为javax.persistence.Id
。这应该有助于解决您遇到的异常。
回答by Anant Pathak
I encountered the same problem today.After some debugging,got to know that I annotated one of the fields in my model class with @GeneratedValue(strategy = GenerationType.AUTO)
but didn't add the @Id
annotation to that field.Adding one resolved it.
我今天遇到了同样的问题。经过一些调试,我知道我在模型类中注释了一个字段,@GeneratedValue(strategy = GenerationType.AUTO)
但没有将@Id
注释添加到该字段中。添加一个解决了它。
回答by Ashwini
use
用
javax.persistence.Id
instead of
代替
org.springframework.data.annotation.Id
will solve the problem
将解决问题
回答by Vinayak Shedgeri
Hibernate jar was corrupted in local repository of maven,it was taking corrupted library every time, after deleting local maven repository issue resolved
Hibernate jar 在 maven 本地存储库中已损坏,每次都使用损坏的库,删除本地 maven 存储库问题解决后
C:\Users\user.m2\repository
C:\Users\user.m2\repository
回答by khaoula bel
Encountered the same problem after importing my project to IntelliJ IDE. Turned out it was easier than I thought I went to pom.xmland right-clicked on "+ Add as a Maven project"
. This solved the problem.
将我的项目导入 IntelliJ IDE 后遇到同样的问题。原来它比我想象的要容易,我去pom.xml并右键单击"+ Add as a Maven project"
. 这解决了问题。
回答by akashdeep kashyap
This error might be related to wrong database configuration in your project. In your application.yml or application.properties file, you would be given wrong db name in "spring.datasource.url" .
此错误可能与项目中错误的数据库配置有关。在您的 application.yml 或 application.properties 文件中,您将在 "spring.datasource.url" 中获得错误的数据库名称。