使用休眠

时间:2018-07-29 10:56:12

标签: java spring hibernate jpa

我正在设计一个应用程序,其中有三个表Customer,Invoices,Containers。一个客户可以有很多发票,一个容器可以有很多发票,因此发票表有两个外键,一个来自客户,一个来自容器。现在,我想检索整个发票表,而不是针对客户或容器检索它,然后将数据发送到jsp页面。 Here is the image of database erd

实体看起来像这样。

Customer.java

@Entity
@Table(name="customer")
public class Customer {

@Id
@Column(name="Id")
private Integer Id;

@Column(name="Name")
private String Name;

@Column(name="PhoneNo")
private String PhoneNo;


@Column(name="Email")
private String Email;

@Column(name="Address")
private String Address;

@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="Customer_Id")
private List<Invoice> invoices;

public Customer(){

}

Container.java

    @Entity
    @Table(name="container")
    public class Container {

    @Id
    @Column(name="Id")
    private int Id;

    @Column(name="Code")
    private int Code;

    @Column(name="Name")
    private String Name;

    @Column(name="From")
    private String From;

    @Column(name="To")
    private String To;

    @Column(name="Depart")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Temporal(TemporalType.DATE)
    private Date Depart;

    @Column(name="Arrival")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Temporal(TemporalType.DATE)
    private Date Arrival;

    @OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    @JoinColumn(name="Container_Id")
    private List<Invoice> invoices;

Invoices.java

 @Entity
 @Table(name="invoice")
 public class Invoice {

@Id
@Column(name="Id")
private int Id;

@Column(name="WeightQty")
private String WeightQty;

@Column(name="Details")
private String Details;

@Column(name="Rate")
private int Rate;

@Column(name="Debit")
private int Debit;

@Column(name="Credit")
private int Credit;

public Invoice(){

}
enter code here

1 个答案:

答案 0 :(得分:0)

在与客户和容器的绑定中添加许多功能


     @Entity
         @Table(name="invoice")
         public class Invoice {

        @Id
        @Column(name="Id")
        private int Id;

        @Column(name="WeightQty")
        private String WeightQty;

        @Column(name="Details")
        private String Details;

        @Column(name="Rate")
        private int Rate;

        @Column(name="Debit")
        private int Debit;

        @Column(name="Credit")
        private int Credit;

       @ManyToOne
    @JoinColumn(name = "customerid")
private Customer customer;

@ManyToOne
    @JoinColumn(name = "containerid")
private Container container;


        public Invoice(){

        }