创建名为“jpamappingcontext”的bean时出错:调用init方法失败;嵌套的异常是org.hibernate

weylhg0b  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(308)

你好,我是新春数据。我得到这个错误,我不知道为什么。我正在尝试连接到mysql数据库并进行填充。
我遇到的错误:创建名为“customerrepo”的bean时出错,该bean在practice.banking_atm.repo.customerrepo中定义,该bean在jparepositoriesregistra.enablejparepositoriesconfiguration上声明@enablejparepositories中定义:设置bean属性“mappingcontext”时无法解析对bean“jpamappingcontext”的引用;嵌套异常为org.springframework.beans.factory.beancreationexception:创建名为“jpamappingcontext”的bean时出错:调用init方法失败;嵌套异常为org.hibernate.annotationexception:非法尝试将非集合Map为@onetomany、@manytomany或@collectionfelements:practice.banking\u atm.model.checkingaccount.customer

2020-12-03 05:42:26.854  INFO 17112 --- [           main] P.banking_atm.BankingAtmApplication      : Starting BankingAtmApplication using Java 1.8.0_45 on Mohammad with PID 17112 (C:\Users\mohda_000\Desktop\banking_atm\target\classes started by mohda_000 in C:\Users\mohda_000\Desktop\banking_atm)
2020-12-03 05:42:26.878  INFO 17112 --- [           main] P.banking_atm.BankingAtmApplication      : No active profile set, falling back to default profiles: default
2020-12-03 05:42:29.502  INFO 17112 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-12-03 05:42:29.638  INFO 17112 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 112 ms. Found 2 JPA repository interfaces.
2020-12-03 05:42:32.194  INFO 17112 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2020-12-03 05:42:32.250  INFO 17112 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-03 05:42:32.250  INFO 17112 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-12-03 05:42:32.955  INFO 17112 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-03 05:42:32.955  INFO 17112 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5741 ms
2020-12-03 05:42:33.939  INFO 17112 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-03 05:42:34.286  INFO 17112 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-12-03 05:42:34.550  WARN 17112 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-12-03 05:42:34.838  INFO 17112 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.23.Final
2020-12-03 05:42:36.738  INFO 17112 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2020-12-03 05:42:37.401  INFO 17112 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2020-12-03 05:42:37.417  INFO 17112 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-12-03 05:42:37.441  INFO 17112 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-12-03 05:42:38.844  INFO 17112 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-12-03 05:42:38.922  INFO 17112 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-12-03 05:42:39.722  WARN 17112 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepo' defined in Practice.banking_atm.Repo.CustomerRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: Practice.banking_atm.Model.CheckingAccount.customer
2020-12-03 05:42:39.722  INFO 17112 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-12-03 05:42:39.722  WARN 17112 --- [           main] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method failed on bean with name 'entityManagerFactory': org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: Practice.banking_atm.Model.CheckingAccount.customer
2020-12-03 05:42:39.722  INFO 17112 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-12-03 05:42:39.730  INFO 17112 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-12-03 05:42:39.770  INFO 17112 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-12-03 05:42:40.568  INFO 17112 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-12-03 05:42:40.662  INFO 17112 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-03 05:42:40.850 ERROR 17112 --- [           main] o.s.boot.SpringApplication               : Application run failed

模型包:

package Practice.banking_atm.Model;

import javax.persistence.*;

@Entity
@Table(name="checking_account")
public class CheckingAccount {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="checking_id")
    private Integer checkingId;
    @Column(name="balance")
    private Integer balance;
    @Column(name="add_balance")
    private Integer addBalance;
    @Column(name="new_balance")
    private Integer newBalance;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "checking_id", referencedColumnName = "checking_id")
    private Customer customer;

    public CheckingAccount() {
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Integer getCheckingId() {
        return checkingId;
    }

    public void setCheckingId(Integer checkingId) {
        this.checkingId = checkingId;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    public Integer getAddBalance() {
        return addBalance;
    }

    public void setAddBalance(Integer addBalance) {
        this.addBalance = addBalance;
    }

    public Integer getNewBalance() {
        return newBalance;
    }

    public void setNewBalance(Integer newBalance) {
        this.newBalance = newBalance;
    }
}
package Practice.banking_atm.Model;

import javax.persistence.*;

@Entity
@Table(name="customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="customer_id")
    private Integer customerId;
    @Column(name="first_name")
    private String firstName;
    @Column(name="last_name")
    private String lastName;

    public Customer() {
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

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

}

检查帐户报告:

package Practice.banking_atm.Repo;

import Practice.banking_atm.Model.CheckingAccount;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CheckingAccountRepo extends JpaRepository<CheckingAccount,Integer> {
}
  • 客户报告:
package Practice.banking_atm.Repo;

import Practice.banking_atm.Model.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerRepo extends JpaRepository<Customer,Integer> {
}
  • 应用程序属性:
server.port = 8082
spring.datasource.url = jdbc:mysql://localhost:3306/banking_atm
spring.datasource.username = root
spring.datasource.password = rootroot

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

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.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>Practice</groupId>
    <artifactId>banking_atm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>banking_atm</name>
    <description>banking atm</description>

    <properties>
        <java.version>1.8</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>mysql</groupId>
            <artifactId>mysql-connector-java</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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题