Java 未能初始化JPA EntityManagerFactory:[PersistenceUnit:default]无法生成Hibernate SessionFactory
基于此项目(更新至当前最新版本的spring boot+java 11): 我正在尝试从IntelliJ运行应用程序,但我得到:Java 未能初始化JPA EntityManagerFactory:[PersistenceUnit:default]无法生成Hibernate SessionFactory,java,spring-boot,hibernate,Java,Spring Boot,Hibernate,基于此项目(更新至当前最新版本的spring boot+java 11): 我正在尝试从IntelliJ运行应用程序,但我得到: 2021-04-02 11:18:08.185 INFO 86413 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect 2021-04-02 11:18:08.422 E
2021-04-02 11:18:08.185 INFO 86413 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-04-02 11:18:08.422 ERROR 86413 --- [ main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: book, for columns: [org.hibernate.mapping.Column(authors)]
2021-04-02 11:18:08.423 WARN 86413 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: book, for columns: [org.hibernate.mapping.Column(authors)]
2021-04-02 11:18:08.423 INFO 86413 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
这看起来是最重要的部分:
Could not determine type for: java.util.Set, at table: book, for columns: [org.hibernate.mapping.Column(authors)]
有什么建议吗
以及:
Author.java
package com.example.demospring.domain;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Author {
private String firstName;
private String lastName;
@ManyToMany(mappedBy = "authors")
private Set<Book> books = new HashSet<>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Author() {
}
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
public void setId(Long id) {
this.id = id;
}
@Id
public Long getId() {
return id;
}
@Override
public String toString() {
return "Author{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", books=" + books +
", id=" + id +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Author author = (Author) o;
return id != null ? id.equals(author.id) : author.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
package com.example.demospring.domain;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Book {
private String title;
private String isbn;
@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name="book_id"), inverseJoinColumns = @JoinColumn(name="author_id"))
private Set<Author> authors = new HashSet<>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Book() {
}
public Book(String title, String isbn) {
this.title = title;
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Set<Author> getAuthors() {
return authors;
}
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
public void setId(Long id) {
this.id = id;
}
@Id
public Long getId() {
return id;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", isbn='" + isbn + '\'' +
", authors=" + authors +
", id=" + id +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return id != null ? id.equals(book.id) : book.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
package com.example.demospring.domain;
导入javax.persistence.*;
导入java.util.HashSet;
导入java.util.Set;
@实体
公共类作者{
私有字符串名;
私有字符串lastName;
@许多(mappedBy=“作者”)
private Set books=new HashSet();
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
公共作者(){
}
公共作者(字符串名、字符串名){
this.firstName=firstName;
this.lastName=lastName;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串lastName){
this.lastName=lastName;
}
公共集getBooks(){
还书;
}
公共书籍(套书){
这本书=书;
}
公共无效集合id(长id){
this.id=id;
}
@身份证
公共长getId(){
返回id;
}
@凌驾
公共字符串toString(){
返回“作者{”+
“firstName=”+firstName+“\”+
“,lastName='”+lastName+'\''+
“,books=“+books”+
“,id=“+id+
'}';
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
作者=(作者)o;
返回id!=null?id.equals(author.id):author.id==null;
}
@凌驾
公共int hashCode(){
返回id!=null?id.hashCode():0;
}
}
Book.java
package com.example.demospring.domain;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Author {
private String firstName;
private String lastName;
@ManyToMany(mappedBy = "authors")
private Set<Book> books = new HashSet<>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Author() {
}
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
public void setId(Long id) {
this.id = id;
}
@Id
public Long getId() {
return id;
}
@Override
public String toString() {
return "Author{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", books=" + books +
", id=" + id +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Author author = (Author) o;
return id != null ? id.equals(author.id) : author.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
package com.example.demospring.domain;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Book {
private String title;
private String isbn;
@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name="book_id"), inverseJoinColumns = @JoinColumn(name="author_id"))
private Set<Author> authors = new HashSet<>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Book() {
}
public Book(String title, String isbn) {
this.title = title;
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Set<Author> getAuthors() {
return authors;
}
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
public void setId(Long id) {
this.id = id;
}
@Id
public Long getId() {
return id;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", isbn='" + isbn + '\'' +
", authors=" + authors +
", id=" + id +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return id != null ? id.equals(book.id) : book.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
package com.example.demospring.domain;
导入javax.persistence.*;
导入java.util.HashSet;
导入java.util.Set;
@实体
公共课堂用书{
私有字符串标题;
专用字符串isbn;
@许多
@JoinTable(name=“author\u book”,joinColumns=@JoinColumn(name=“book\u id”),inverseJoinColumns=@JoinColumn(name=“author\u id”))
private Set authors=new HashSet();
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
公共书籍(){
}
公共图书(字符串标题,字符串isbn){
this.title=标题;
这是isbn=isbn;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getIsbn(){
返回isbn;
}
公共无效集合isbn(字符串isbn){
这是isbn=isbn;
}
公共集getAuthors(){
返回作者;
}
公共void集合作者(集合作者){
this.authors=作者;
}
公共无效集合id(长id){
this.id=id;
}
@身份证
公共长getId(){
返回id;
}
@凌驾
公共字符串toString(){
返回“Book{”+
“title=”+title+“\”+
“,isbn=”+isbn+“\”+
“,authors=“+作者+
“,id=“+id+
'}';
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
簿册=(簿册)o;
返回id!=null?id.equals(book.id):book.id==null;
}
@凌驾
公共int hashCode(){
返回id!=null?id.hashCode():0;
}
}
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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-spring</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.4.4
com.example
演示弹簧
0.0.1-快照
演示弹簧
SpringBoot的演示项目
11
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
com.h2数据库
氢
运行时
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
这是
您必须从
Author
和Book
中的getId()
中删除@Id
注释,这是否回答了您的问题?