Spring-MVC项目中实体错误映射中的重复列

时间:2018-07-26 11:49:11

标签: java mysql spring-mvc

我通过实体将MySql数据库连接到我的Java项目(使用Spring-MVC)。 当我尝试运行该应用程序时,出现此错误:

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: Repeated column in mapping for entity: com.example.WebAppProcess20.Entities.OrdersitemsEntity column: order_id (should be mapped with insert="false" update="false")

OrdersItems实体:

    package com.example.WebAppProcess20.Entities;


import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "ordersitems", schema = "theprocess", catalog = "")
@IdClass(OrdersitemsEntityPK.class)
public class OrdersitemsEntity {
    private String productId;
    private String orderId;
    private Integer qunatity;
    private ProductsEntity productsByProductId;
    private OrdersEntity ordersByOrderId;

    @Id
    @Column(name = "product_id")
    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    @Id
    @Column(name = "orderId", nullable = false, insertable = false, updatable = false)
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @Basic
    @Column(name = "qunatity")
    public Integer getQunatity() {
        return qunatity;
    }

    public void setQunatity(Integer qunatity) {
        this.qunatity = qunatity;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OrdersitemsEntity that = (OrdersitemsEntity) o;
        return Objects.equals(productId, that.productId) &&
                Objects.equals(orderId, that.orderId) &&
                Objects.equals(qunatity, that.qunatity);
    }

    @Override
    public int hashCode() {

        return Objects.hash(productId, orderId, qunatity);
    }

    @ManyToOne
    @JoinColumn(name = "product_id", referencedColumnName = "product_id", nullable = false)
    public ProductsEntity getProductsByProductId() {
        return productsByProductId;
    }

    public void setProductsByProductId(ProductsEntity productsByProductId) {
        this.productsByProductId = productsByProductId;
    }

    @ManyToOne
    @JoinColumn(name = "orderId", referencedColumnName = "orderId", nullable = false)
    public OrdersEntity getOrdersByOrderId() {
        return ordersByOrderId;
    }

    public void setOrdersByOrderId(OrdersEntity ordersByOrderId) {
        this.ordersByOrderId = ordersByOrderId;
    }
}

订单实体:

package com.example.WebAppProcess20.Entities;


import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Objects;

@Entity
@Table(name = "orders", schema = "theprocess", catalog = "")
public class OrdersEntity {
    private String orderId;
    private String notesFromClient;
    private Timestamp orderDate;
    private String orderStatus;
    private String totalSum;
    private ClientsEntity clientsByClientId;
    private Collection<OrdersitemsEntity> ordersitemsByOrderId;

    @Id
    @Column(name = "orderId")
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @Basic
    @Column(name = "notes_from_client")
    public String getNotesFromClient() {
        return notesFromClient;
    }

    public void setNotesFromClient(String notesFromClient) {
        this.notesFromClient = notesFromClient;
    }

    @Basic
    @Column(name = "order_date")
    public Timestamp getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Timestamp orderDate) {
        this.orderDate = orderDate;
    }

    @Basic
    @Column(name = "order_status")
    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    @Basic
    @Column(name = "totalSum")
    public String getTotalSum() {
        return totalSum;
    }

    public void setTotalSum(String totalSum) {
        this.totalSum = totalSum;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OrdersEntity that = (OrdersEntity) o;
        return Objects.equals(orderId, that.orderId) &&
                Objects.equals(notesFromClient, that.notesFromClient) &&
                Objects.equals(orderDate, that.orderDate) &&
                Objects.equals(orderStatus, that.orderStatus) &&
                Objects.equals(totalSum, that.totalSum);
    }

    @Override
    public int hashCode() {

        return Objects.hash(orderId, notesFromClient, orderDate, orderStatus, totalSum);
    }

    @ManyToOne
    @JoinColumn(name = "client_id", referencedColumnName = "client_id", nullable = false)
    public ClientsEntity getClientsByClientId() {
        return clientsByClientId;
    }

    public void setClientsByClientId(ClientsEntity clientsByClientId) {
        this.clientsByClientId = clientsByClientId;
    }

    @OneToMany(mappedBy = "ordersByOrderId")
    public Collection<OrdersitemsEntity> getOrdersitemsByOrderId() {
        return ordersitemsByOrderId;
    }

    public void setOrdersitemsByOrderId(Collection<OrdersitemsEntity> ordersitemsByOrderId) {
        this.ordersitemsByOrderId = ordersitemsByOrderId;
    }
}

OrdersItemsPK类:

package com.example.WebAppProcess20.Entities;


import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Objects;

public class OrdersitemsEntityPK implements Serializable {
    private String productId;
    private String orderId;

    @Column(name = "product_id")
    @Id
    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    @Column(name = "orderId")
    @Id
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OrdersitemsEntityPK that = (OrdersitemsEntityPK) o;
        return Objects.equals(productId, that.productId) &&
                Objects.equals(orderId, that.orderId);
    }

    @Override
    public int hashCode() {

        return Objects.hash(productId, orderId);
    }
}

产品实体:

package com.example.WebAppProcess20.Entities;

import javax.persistence.*;
import java.util.Collection;
import java.util.Objects;

@Entity
@Table(name = "products", schema = "theprocess", catalog = "")
public class ProductsEntity {
    private String productId;
    private Integer availableInStock;
    private String brand;
    private String image;
    private Integer price;
    private String productCategoryTree;
    private String productName;
    private Integer rating;
    private Collection<OrdersitemsEntity> ordersitemsByProductId;

    @Id
    @Column(name = "product_id")
    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    @Basic
    @Column(name = "available_in_stock")
    public Integer getAvailableInStock() {
        return availableInStock;
    }

    public void setAvailableInStock(Integer availableInStock) {
        this.availableInStock = availableInStock;
    }

    @Basic
    @Column(name = "brand")
    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    @Basic
    @Column(name = "image")
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    @Basic
    @Column(name = "price")
    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Basic
    @Column(name = "product_category_tree")
    public String getProductCategoryTree() {
        return productCategoryTree;
    }

    public void setProductCategoryTree(String productCategoryTree) {
        this.productCategoryTree = productCategoryTree;
    }

    @Basic
    @Column(name = "product_name")
    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @Basic
    @Column(name = "rating")
    public Integer getRating() {
        return rating;
    }

    public void setRating(Integer rating) {
        this.rating = rating;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ProductsEntity that = (ProductsEntity) o;
        return Objects.equals(productId, that.productId) &&
                Objects.equals(availableInStock, that.availableInStock) &&
                Objects.equals(brand, that.brand) &&
                Objects.equals(image, that.image) &&
                Objects.equals(price, that.price) &&
                Objects.equals(productCategoryTree, that.productCategoryTree) &&
                Objects.equals(productName, that.productName) &&
                Objects.equals(rating, that.rating);
    }

    @Override
    public int hashCode() {

        return Objects.hash(productId, availableInStock, brand, image, price, productCategoryTree, productName, rating);
    }

    @OneToMany(mappedBy = "productsByProductId")
    public Collection<OrdersitemsEntity> getOrdersitemsByProductId() {
        return ordersitemsByProductId;
    }

    public void setOrdersitemsByProductId(Collection<OrdersitemsEntity> ordersitemsByProductId) {
        this.ordersitemsByProductId = ordersitemsByProductId;
    }
}

Ordersitems连接Orders和Products之间-它包含每个订单的产品,因此productId和orderId都是Orderitems中的外键。

我认为我理解错误,如果我没有记错,那么重复的列错误是指:

@Id
    @Column(name = "orderId", nullable = false, insertable = false, updatable = false)
    public String getOrderId() {
        return orderId;
    }

和:

@ManyToOne
    @JoinColumn(name = "orderId", referencedColumnName = "orderId", nullable = false)
    public OrdersEntity getOrdersByOrderId() {
        return ordersByOrderId;
    }

    public void setOrdersByOrderId(OrdersEntity ordersByOrderId) {
        this.ordersByOrderId = ordersByOrderId;
    }

但是我尝试做的任何事情都没有解决问题,甚至没有添加其他错误。 您能帮我解决这个问题吗? 谢谢!

0 个答案:

没有答案