使用Hibernate将实体映射到表时BeanCreationException

时间:2018-05-13 23:10:54

标签: hibernate spring-boot jpa

Spring Boot项目中工作时,我使用JPA映射数据库中的实体,但不幸的是我遇到了这个错误:

  

org.springframework.beans.factory.BeanCreationException:错误   在类路径中定义名为'entityManagerFactory'的bean   资源   [组织/ springframework的/引导/自动配置/ ORM / JPA / HibernateJpaConfiguration.class]:   调用init方法失败;嵌套异常是   org.hibernate.AnnotationException:@OneToOne或@ManyToOne on   com.userFront.domain.SavingsTransaction.savingsAccount引用一个   未知实体:com.userFront.domain.SavingsAccount

据我所知,问题在于这两个类之间的关系。根据我的观点,我认为所有的注释都是正确的,逻辑是正确的。

这是SavingsAccount类:

package com.userFront.domain;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import net.minidev.json.annotate.JsonIgnore;

public class SavingsAccount {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id ;
    private int accountNumber; 
    private BigDecimal accountBalance; 
    @OneToMany(mappedBy="savingsAccount",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    @JsonIgnore
    private List<SavingsTransaction > SavingsTransactionList;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public int getAccountNumber() {
        return accountNumber;
    }
    public void setAccountNumber(int accountNumber) {
        this.accountNumber = accountNumber;
    }
    public BigDecimal getAccountBalance() {
        return accountBalance;
    }
    public void setAccountBalance(BigDecimal accountBalance) {
        this.accountBalance = accountBalance;
    }
    public List<SavingsTransaction> getSavingsTransactionList() {
        return SavingsTransactionList;
    }
    public void setSavingsTransactionList(List<SavingsTransaction> savingsTransactionList) {
        SavingsTransactionList = savingsTransactionList;
    }

}

这是SavingsTransaction类:

package com.userFront.domain;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class SavingsTransaction {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id ; 
    private Date date ; 
    private String description ; 
    private String type ; 
    private String status ; 
    private double amount; 
    private BigDecimal availableBalance ; 
    @ManyToOne
    @JoinColumn(name="savings_account_id")
    private SavingsAccount savingsAccount; 
    public SavingsTransaction() {}
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }
    public BigDecimal getAvailableBalance() {
        return availableBalance;
    }
    public void setAvailableBalance(BigDecimal availableBalance) {
        this.availableBalance = availableBalance;
    }
    public SavingsAccount getSavingsAccount() {
        return savingsAccount;
    }
    public void setSavingsAccount(SavingsAccount savingsAccount) {
        this.savingsAccount = savingsAccount;
    }
    public SavingsTransaction(Date date, String description, String type, String status, double amount,
            BigDecimal availableBalance, SavingsAccount savingsAccount) {
        super();
        this.date = date;
        this.description = description;
        this.type = type;
        this.status = status;
        this.amount = amount;
        this.availableBalance = availableBalance;
        this.savingsAccount = savingsAccount;
    }
}

1 个答案:

答案 0 :(得分:1)

您的SavingsAccount课程未使用@Entity进行注释。你可以尝试看看它是否能解决问题吗?