在"一对多"在更新父表时,将映射hibernate数据插入子表中

时间:2017-01-28 12:36:10

标签: java sql hibernate

                import java.util.Set;
                import javax.persistence.CascadeType;
                import javax.persistence.Column;
                import javax.persistence.Entity;
                import javax.persistence.FetchType;
                import javax.persistence.Id;
                import javax.persistence.OneToMany;
                import javax.persistence.Table;

                @Entity
                @Table(name="BILLING_INFORMATION")
                public class BillingInformation {

                    @Id
                    @Column(name="BILL_NO", unique = true, nullable = false, length = 100)
                    private String bNo;

                    @Column(name="BILL_DATE", length = 100)
                    private String billDate;

                    @Column(name="PARTY_NAME", length = 100)
                    private String partyName;

                    @Column(name="SUB_TOTAL", length = 100)
                    private String subTotal;

                    @Column(name="TOTAL", length = 100)
                    private String total;

                    @OneToMany(fetch=FetchType.EAGER, mappedBy="billInformation", cascade = CascadeType.ALL,orphanRemoval=true)
                    Set<BillingItemInformation> setBillingItemInformation;

                    @OneToMany(fetch=FetchType.EAGER, mappedBy="billInformation", cascade = CascadeType.ALL,orphanRemoval=true)
                    Set<BillingTaxInformation> setBillingTaxInformation;

            ----all getters and setters----
            }


        import javax.persistence.Column;
        import javax.persistence.Entity;
        import javax.persistence.GeneratedValue;
        import javax.persistence.GenerationType;
        import javax.persistence.Id;
        import javax.persistence.JoinColumn;
        import javax.persistence.ManyToOne;
        import javax.persistence.Table;

        @Entity
        @Table(name="BILLING_ITEM_INFORMATION")
        public class BillingItemInformation {


            @Id
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            @Column(name = "RECORD_ID", unique = true, nullable = false, length = 100)
            private int recordNo;

            @Column(name = "S_NO", length = 100)
            private String sNo;

            @Column(name="ITEM", length = 100)
            private String item;

            @Column(name="QTY", length = 100)
            private String qty;

            @Column(name="PRICE", length = 100)
            private String price;

            @Column(name="AMOUNT", length = 100)
            private String amount;

            @ManyToOne
            @JoinColumn(name="BILL_NO", nullable=false)
            private BillingInformation billInformation;

        ---all getters and setters----

    }

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="BILLING_TAX_INFORMATION")
public class BillingTaxInformation {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "RECORD_ID", unique = true, nullable = false, length = 100)
    private int recordNo;

    @Column(name = "TAX_NAME", length = 100)
    private String taxName;

    @Column(name = "TAX_PERCENTAGE", length = 100)
    private String taxInPercent;

    @Column(name = "TAX_AMOUNT", length = 100)
    private String taxAmount;

    @ManyToOne
    @JoinColumn(name="BILL_NO", nullable=false)
    BillingInformation billInformation;
--------all getters and setters----
}

我刚开始从简单的java jdbc学习hibernate并且作为我们项目要求的一部分我必须维护上面三个类但是当我更新BillingInformation.java实体类对象然后BillingTaxInformation.java和BillingItemInformation.java类对象被插入具有不同recordNo的相同对象。请纠正我错误的地方。

1 个答案:

答案 0 :(得分:0)

您在集合的@OneToMany映射中使用了cascade = CascadeType.ALL。

这意味着无论在BillingInformation实体上执行什么操作,它都会传播到子BillingItemInformation和BillingTaxInformation。

那是你

的时候
  • PERSIST
  • REFRESH
  • MERGE
  • REMOVE
  • DETACH

..同样适用于这些孩子。

如果您只想在父实体

时级联
  • PERSISTED(或以hibernate语言保存)
  • 和DELETED(@OneToMany的两个最常见选项)

,但在MERGE和后续更新期间,您不希望将这些操作级联到子级,然后按如下方式设置映射:

@OneToMany(fetch=FetchType.EAGER, mappedBy="billInformation"
          , cascade = { CascadeType.PERSIST, CascadeType.REMOVE }
          ,orphanRemoval=true)
Set<BillingItemInformation> setBillingItemInformation;

@OneToMany(fetch=FetchType.EAGER, mappedBy="billInformation"
         , cascade ={ CascadeType.PERSIST, CascadeType.REMOVE }
         ,orphanRemoval=true)
Set<BillingTaxInformation> setBillingTaxInformation;

<强>更新

使用hibernate注释,您可以配置如下:

@Cascade({CascadeType.PERSIST, CascadeType.DELETE})