Hibernate,Mysql - OnetoOne映射不起作用

时间:2014-08-22 20:12:44

标签: java spring hibernate jpa

我有两个名为Purchase和PurchaseDetails的表,我试图在尝试插入不插入第二个表的值时执行一对一映射,即),PurchaseDetails,但它完全插入到购买表中。

任何人都可以帮助我。

@Entity
@Table(name="Purchase")
public class Purchase implements Serializable {
     @Id
      @GeneratedValue
      private Integer order_no;
     private String order_date;
     private String sales_person;
     private String ship_via;
     private String ship_from;
     private String ship_to;
     private String total;
     private String tax_amt;
     private String grand_total;

     @Transient 
     private PurchaseDetails purchaseDetails;


     @OneToOne(fetch = FetchType.LAZY, mappedBy = "Purchase", cascade = CascadeType.ALL) 
    public PurchaseDetails getPurchaseDetails() {
        return purchaseDetails;
    }
    public void setPurchaseDetails(PurchaseDetails purchaseDetails) {
        this.purchaseDetails = purchaseDetails;
    }
    Getters and Setters.
}

表 - PurchaseDetails:

@Entity
@Table(name="purchase_details")
public class PurchaseDetails implements Serializable {
     @Id
      @GeneratedValue

      private Integer order_no;
     private Integer item_no;
     private String item_name;
     private String quantity;
     private String unit;
     private String unit_price;
     private String line_total;

     @OneToOne(fetch = FetchType.LAZY)
     @PrimaryKeyJoinColumn
     private Purchase purchase;

    public Purchase getPurchase() {
        return purchase;
    }
    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
}
Getters and Setters.
} 

插入值:

Purchase purc=new Purchase();
                        purc.setShip_from(purchase.getShip_from());
                        purc.setGrand_total("11");
                        purc.setOrder_date(purchase.getOrder_date());
                        purc.setOrder_no(purchase.getOrder_no());
                        purc.setSales_person(purchase.getSales_person());
                        purc.setShip_via(purchase.getShip_via());
                        purc.setTax_amt("123");
                        purc.setShip_to(purchase.getShip_to());
                        purc.setTotal("1234");

                        PurchaseDetails podetail= new PurchaseDetails();
                        podetail.setItem_name(podetails.getItem_name());
                        podetail.setItem_no(podetails.getItem_no());
                        podetail.setLine_total(podetails.getLine_total());
                        podetail.setOrder_no(podetails.getOrder_no());
                        podetail.setQuantity(podetails.getQuantity());
                        podetail.setUnit(podetails.getUnit());
                        podetail.setUnit_price(podetails.getUnit_price());
                        purc.setPurchaseDetails(podetail);
                        podetail.setPurchase(purc);

                      em.merge(purc);
                      entr.commit();

任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

首先,您在字段和getter上有JPA注释。它是一个或另一个,但不是两个。 Hibernate查找注释@Id的位置,并忽略不在同一位置的所有注释。因此,在您的示例中,OneToOne注释被忽略,因为它位于getter上,而Id注释位于字段上。

未被忽略的是注释@Transient,放在purchaseDetails字段上。但@Transient表示:此字段不应保留。所以......事实并非如此。

回顾一下,删除Transient注释,并将OneToOne注释放在字段purchaseDetails上,而不是将其放在其getter上。