简体   繁体   中英

Hibernate,Mysql - OnetoOne Mapping not working

I am having two tables called Purchase and PurchaseDetails,I am trying to do One to One Mapping while trying to inserting the values not inserting second table ie), PurchaseDetails,but its inserting into Purchase table perfectly.

Could any one help me on this.

@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.
}

Table - 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.
} 

Inserting Values :

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();

Could any one help me to resolve this.

First of all, you have JPA annotations on fields and on getters. It's one or the other, but not both. Hibernate looks where the annotation @Id is, and ignores all the annotations that are not on the same place. So, in your example, the OneToOne annotation is ignored, since it's on a getter whereas the Id annotation is on a field.

What is not ignored is the annotation @Transient , placed on the purchaseDetails field. But @Transient means: this field shouldn't be persisted. So... it isn't.

To recap, remove the Transient annotation, and put the OneToOne annotation on the field purchaseDetails instead of putting it on its getter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM