简体   繁体   English

无法初始化 JPA EntityManagerFactory:[PersistenceUnit:默认] 无法构建 Hibernate SessionFactory

[英]Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

Based on this project (updated to current latest release of spring boot + java 11):基于这个项目(更新到当前最新版本的 spring boot + java 11):

https://github.com/springframeworkguru/spring5webapp/tree/bootstrap-v2 https://github.com/springframeworkguru/spring5webapp/tree/bootstrap-v2

I am trying to run the application from IntelliJ but I get:我正在尝试从 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 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...

Where this looks like the essential part:这看起来像是必不可少的部分:

Could not determine type for: java.util.Set, at table: book, for columns: [org.hibernate.mapping.Column(authors)]

Any suggestions?有什么建议么?

And:和:

Author.java作者.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;
    }


}

Book.java Book.java

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

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 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA Hibernate Persistence 异常 [PersistenceUnit: default] 无法构建 Hibernate SessionFactory - JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory PersistenceUnit:默认无法建立Hibernate SessionFactory Spring JPA - PersistenceUnit: default Unable to build Hibernate SessionFactory Spring JPA PersistenceUnit:default无法构建Hibernate SessionFactory - Hibernate - PersistenceUnit: default Unable to build Hibernate SessionFactory - Hibernate 创建名为“entityManagerFactory”的 bean 时出错:[PersistenceUnit: default] 无法构建 Hibernate SessionFactory - Error creating bean with name 'entityManagerFactory' : [PersistenceUnit: default] Unable to build Hibernate SessionFactory Controller中的调音方法。 [PersistenceUnit:默认]无法建立Hibernate SessionFactory - Tune method in Controller. [PersistenceUnit: default] Unable to build Hibernate SessionFactory javax.persistence.PersistenceException:[PersistenceUnit:默认]无法构建 Hibernate SessionFactory; - javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 多个带有JPA的@ ElementCollection`:[PersistenceUnit:JPA]无法建立EntityManagerFactory - Multiple `@ElementCollection`s with JPA Error: [PersistenceUnit: JPA] Unable to build EntityManagerFactory Hibernate - OGM [PersistenceUnit:person]无法构建Hibernate SessionFactory - Hibernate - OGM [PersistenceUnit: person] Unable to build Hibernate SessionFactory PersistenceUnit:jpaData-无法建立EntityManagerFactory - PersistenceUnit: jpaData - Unable to build EntityManagerFactory [PersistenceUnit:<name>]:无法构建EntityManagerFactory - [PersistenceUnit: <name>]: unable to build EntityManagerFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM