在Spring Data / Hibernate中获取实体的异常

时间:2018-08-28 05:27:00

标签: java spring hibernate spring-boot

尝试在Jboss上部署应用程序时出现以下错误:

  

无法启动的服务:服务   jboss.undertow.deployment.default-server.default-host./naturallatex-rest:   java.lang.RuntimeException:   org.springframework.beans.factory.BeanCreationException:错误   在类路径中创建名称为“ entityManagerFactory”的bean   资源   [org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]:   调用init方法失败;嵌套异常为   org.hibernate.MappingException:无法找到具有逻辑的列   名称:customerCollection中   org.hibernate.mapping.Table(natural_latex.billing_address)及其   相关的超级表和辅助表

Customer.java

package naturallatex.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
*
*   @author sam
*/
@Entity
@Table(name = "customer", catalog = "natural_latex", schema = "")
@XmlRootElement
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cust_id")
    @Basic(optional = false)
    private Integer custId;
    @Size(max = 60)
    @Column(name = "first_name")
    private String firstName;
    @Size(max = 60)
    @Column(name = "last_name")
    private String lastName;
    @Size(max = 256)
    private String password;
    @Size(max = 125)
    @Column(name = "email_address")
    private String emailAddress;
    @Size(max = 60)
    private String phone;
    @Size(max = 56)
    private String fax;
    private Boolean status;
    @Column(name = "reg_date")
    @Temporal(TemporalType.DATE)
    private Date regDate;
    @Column(name = "cancel_date")
    @Temporal(TemporalType.DATE)
    private Date cancelDate;
    @JoinColumn(name = "billing_address_id", referencedColumnName = "customerCollection")
    @ManyToOne(optional = false)
    private BillingAddress billingAddressId;
    @JoinColumn(name = "shipping_address_id", referencedColumnName = "customerCollection")
    @ManyToOne(optional = false)
    private ShippingAddress shippingAddressId;

    public Customer() {
    }

    public Customer(Integer cust_id) {
        this.custId = cust_id;
    }

    public Integer getId() {
        return custId;
    }

    public void setId(Integer cust_id) {
        this.custId = cust_id;
    }

    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 String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public Date getRegDate() {
        return regDate;
    }

    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }

    public Date getCancelDate() {
        return cancelDate;
    }

    public void setCancelDate(Date cancelDate) {
        this.cancelDate = cancelDate;
    }

    public BillingAddress getBillingAddressId() {
        return billingAddressId;
    }

    public void setBillingAddressId(BillingAddress billingAddressId) {
        this.billingAddressId = billingAddressId;
    }

    public ShippingAddress getShippingAddressId() {
        return shippingAddressId;
    }

    public void setShippingAddressId(ShippingAddress shippingAddressId) {
        this.shippingAddressId = shippingAddressId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (custId != null ? custId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Customer)) {
            return false;
        }
        Customer other = (Customer) object;
        if ( (this.custId == null && other.custId != null) || (this.custId != null && !this.custId.equals(other.custId)) ) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "naturallatex.Customer[ id=" + custId + " ]";
    }

}

BillingAddress.java

@OneToMany(cascade = CascadeType.ALL, mappedBy = "billingAddressId")
private Collection<Customer> customerCollection;

referencedColumnName 的值应该是什么?

立即获得以下例外。

  

无法启动的服务:service> jboss.undertow.deployment.default-server.default-host./naturallatex-rest:> java.lang.RuntimeException:> org.springframework.beans.factory.BeanCreationException:创建错误在类路径资源>>

中定义的名称为'entityManagerFactory'的bean      
    

[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]>:调用init方法失败;嵌套的异常是?> org.hibernate.MappingException:无法在> org.hibernate.mapping.Table(natural_latex.billing_address)及其相关的>上表和辅助表

中找到逻辑名称为ID的列   

1 个答案:

答案 0 :(得分:1)

referencedColumnName定义另一个实体中的列名,该列名将称为您的外键。

在您的BillingAddress实体表中,hibernate期望有一列名为customerCollection的列,而在BillingAddress实体中似乎没有任何名为{{1 }}您所拥有的对象是用于指定与customerCollection实体的双向关系。

因此,在您的Customer中,请提及referencedColumnName实体中的数据库列名称,该列将与BillingAddress实体一起用作外键。