使用休眠模式的场景的实体类

时间:2019-03-14 12:44:32

标签: java hibernate entity hibernate-mapping hibernate-criteria

场景

  1. 获取特定订单的产品详细信息。
  2. 获取特定产品的订单明细。

SQL服务器表

  1. Table_Orders
  2. Table_OrderDetails
  3. 表产品

实体类

  1. 型号订单

    @Entity
    @Table(name="Tbl_Orders")
    public class Order
    {
        public Order() {
        }
    
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="Order_ID")
        private Long orderId;
    
        @Column(name="OrderNo")
        private String orderNumber;
    
        @Column(name="OrderDate")
        private Date orderDate;
    
        @ManyToOne()
        @JoinColumn(name="User_ID")
        private Customer customerOrder;
    
    
    
        public Long getOrderId() {
            return orderId;
        }
    
        public void setOrderId(Long orderId) {
            this.orderId = orderId;
        }
    
        public String getOrderNumber() {
            return orderNumber;
        }
    
        public void setOrderNumber(String orderNumber) {
            this.orderNumber = orderNumber;
        }
    
        public Date getOrderDate() {
            return orderDate;
        }
    
        public void setOrderDate(Date orderDate) {
            this.orderDate = orderDate;
        }
    
        public Customer getCustomerOrder() {
            return customerOrder;
        }
    
        public void setCustomerOrder(Customer customerOrder) {
            this.customerOrder = customerOrder;
        }
    }
    
  2. Model_OrderDetails

    @Entity
    @Table(name = "Tbl_OrderDetails")
    public class OrderDetail {
    
        public OrderDetail() {
            super();
        }
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "OrderDetail_ID")
        private Long orderDetailsId;
    
        @ManyToOne
        @JoinColumn(name = "Order_ID")
        private Order orderId;
    
        @ManyToOne
        @JoinColumn(name = "Product_ID")
        private ProductMaster product;
    
    
        public Long getOrderDetailsId() {
            return orderDetailsId;
        }
    
        public void setOrderDetailsId(Long orderDetailsId) {
            this.orderDetailsId = orderDetailsId;
        }
    
        public Order getOrderId() {
            return orderId;
        }
    
        public void setOrderId(Order orderId) {
            this.orderId = orderId;
        }
    
    
    
        public ProductMaster getProduct() {
            return product;
        }
    
        public void setProduct(ProductMaster product) {
            this.product = product;
        }
    }
    
  3. Model_Products

    @Entity
    @Table (name = "Tbl_Products")
    public class ProductMaster
    {
        public Products() {
        }
    
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column (name = "Product_ID")
        @Field(index= Index.YES, analyze= Analyze.YES, store= Store.YES)
        private Long productId;
    
        @Column (name = "Product_Code")
        private String productCode;
    
        @Column (name = "Product_Name")
        @Field(index= Index.YES, analyze= Analyze.YES, store= Store.YES  ,analyzer=@Analyzer(definition = "customanalyzer"))
        private String productName;
    
    
    
        public Long getProductId() {
            return productId;
        }
    
        public void setProductId(Long productId) {
            this.productId = productId;
        }
    
    
        public String getProductCode() {
            return productCode;
        }
    
        public void setProductCode(String productCode) {
            this.productCode = productCode;
        }
    
        public String getProductName() {
            return productName;
        }
    
        public void setProductName(String productName) {
            this.productName = productName;
        }
    }
    

问题描述

在这个实体类中,我在所有三个类中使用了多对一映射。我上面提到的场景很多。一个订单可以有多个产品,同时将一个产品放在多个订单中。

我的问题是使用订单ID,我需要获取所有产品详细信息。同样,使用产品ID,我需要获取所有订单详细信息。我想在订单详细信息实体类中使用一对多映射,但我不知道如何映射它。那是正确的方法吗?

2 个答案:

答案 0 :(得分:0)

在您的代码中,Table_OrderDetails的目的仅仅是在Order_ID和Product_ID之间创建映射。因此,不应将其创建为单独的“实体”。相反,应使用休眠@ManyToMany。

Order.java

import javax.persistence.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name="Tbl_Orders")
public class Order
{
    public Order() {
    }
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Order_ID")
    private Long orderId;

    @Column(name="OrderNo")
    private String orderNumber;

    @Column(name="OrderDate")
    private Date orderDate;

    @ManyToMany
    @JoinTable(name = "Tbl_OrderDetails",
            joinColumns = @JoinColumn(name = "Order_ID"),
            inverseJoinColumns = @JoinColumn(name = "Product_ID"))
    private List<ProductMaster> productMasters = new ArrayList<>();

    public Long getOrderId() {
        return orderId;
    }

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

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Date getOrderDate() {
        return orderDate;
    }

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

    public List<ProductMaster> getProductMasters() {
        return productMasters;
    }

    public void setProductMasters(List<ProductMaster> productMasters) {
        this.productMasters = productMasters;
    }
}

ProductMaster.java

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "Tbl_Products")
public class ProductMaster
{
    public ProductMaster() {
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column (name = "Product_ID")
    private Long productId;

    @Column (name = "Product_Code")
    private String productCode;

    @Column (name = "Product_Name")
    private String productName;

    @ManyToMany(mappedBy = "productMasters")
    private List<Order> orders = new ArrayList<>();

    public Long getProductId() {
        return productId;
    }

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


    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getProductName() {
        return productName;
    }

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

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }
}

这将自动创建带有两个Id列的第三个表“ Tbl_OrderDetails”。 这样一来,您将可以获取一种产品的所有订单,以及所有产品的同一订单。

答案 1 :(得分:0)

获取特定订单的产品详细信息。

创建Order到OrderDetail的双向映射。

ArrayDeque

获取特定产品的订单详细信息。

创建ProductMaster到OrderDetail的双向映射。

element()