【问题标题】:Getting exception for entities in Spring Data/hibernate在 Spring Data/hibernate 中获取实体的异常
【发布时间】:2019-02-02 15:54:10
【问题描述】:

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

启动失败的服务: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/HibernateJpaAutoConfiguration.class]: 调用 init 方法失败;嵌套异常是 org.hibernate.MappingException:找不到具有逻辑的列 名称:customerCollection in 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 的值应该是多少?

现在遇到异常。

服务启动失败:服务>jboss.undertow.deployment.default-server.default-host./naturallatex-rest:>java.lang.RuntimeException:>org.springframework.beans.factory.BeanCreationException: 创建错误bean >在类路径资源中定义名称为“entityManagerFactory”>>

[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]>:init方法调用失败;嵌套异常是 ?>org.hibernate.MappingException: Unable to find column with logical name: id in >org.hibernate.mapping.Table(natural_latex.billing_address) 及其相关 >supertables 和辅助表

【问题讨论】:

    标签: java spring hibernate spring-boot


    【解决方案1】:

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

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

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

    【讨论】:

    • 非常感谢您的帮助
    猜你喜欢
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2018-01-11
    • 1970-01-01
    • 2018-07-24
    • 2019-06-13
    • 1970-01-01
    相关资源
    最近更新 更多