hibernate更新一对多项设置

时间:2014-04-07 21:17:34

标签: java mysql hibernate annotations

  • 销售订单实体。

    @Entity
    @Table(name = "sales_orders")
    @IdClass(ReceiptPK.class)
    public class SalesOrders implements Serializable {
    
        public SalesOrders() {
        }
        @Id
        protected Integer receiptID;
        @Id
        protected Integer dateKey;
    
        public SalesOrders(Integer receiptID, Integer dateKey) {
            this.receiptID = receiptID;
            this.dateKey = dateKey;
        }
    //order contains many details
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "salesOrders")
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    private Set<SalesOrderDetails> orderDetails = new HashSet<SalesOrderDetails>();
    
    public Set<SalesOrderDetails> getOrderDetails() {
        return orderDetails;
    }
    
    public void setOrderDetails(Set<SalesOrderDetails> orderDetails) {
        this.orderDetails = orderDetails;
    }
    // other property ..
    
  • 订单详情实体。

    @Entity
    @Table(name = "sales_order_details")
    public class SalesOrderDetails implements Serializable {
    
        public SalesOrderDetails() {
        }
        private int id;
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        // Order holder
        private SalesOrders salesOrders;
    
        @ManyToOne
        @JoinColumns({
            @JoinColumn(name = "order_num", referencedColumnName = "receiptID"),
            @JoinColumn(name = "date_key", referencedColumnName = "dateKey")})
        public SalesOrders getSalesOrders() {
            return salesOrders;
        }
    
        public void setSalesOrders(SalesOrders salesOrders) {
            this.salesOrders = salesOrders;
        }
         // other property ...
    

我的问题:当我尝试删除不影响sales_order_details的订单项时。

            SalesOrders saleOrder = (SalesOrders) getSession().get(SalesOrders.class ,new ReceiptPK(receiptID,dateKey));
            saleOrder.getOrderDetails().remove(someDetails);
            getSession().beginTransaction();
            getSession().saveOrUpdate(saleOrder);
            getSession().getTransaction().commit();  

someDetails未删除。

- 任何帮助将不胜感激......

2 个答案:

答案 0 :(得分:0)

我认为您必须提交相同的交易。

getSession().getTransaction().begin();
getSession().saveOrUpdate(saleOrder);
getSession().getTransaction().commit();

答案 1 :(得分:0)

对于双向关联,您应该(必须)始终确保正确设置关联。

  • 不允许直接访问您的收藏集。
  • 提供添加和删除修改方法。

public Set<SalesOrderDetails> getOrderDetails() {
  //force to use add/remove to ensure consistent object model
  return Collections.unmodifiableSet(orderDetails);
}

public void addOrderDetails(SalesOrderDetails salesOrderDetails){
 orderDetails.add(salesOrderDetails);
 salesOrderDetails.setSalesOrders(this); //important
}

public void removeOrderDetails(SalesOrderDetails salesOrderDetails){
 orderDetails.remove(salesOrderDetails);
 salesOrderDetails.setSalesOrder(null); //important
}

此外,您是否在Entitites上实现了equals()和hashCode(),即当您调用salesOrder.getOrderDetails()时,remove(someDetails)是否真正从集合中删除了什么?

如果你没有在SalesOrderDetails上实现equals()和hashCode(),可能不会。

Fianlly,你大喊将OneToMany映射上的orphanRemoval标志设置为true:

http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Orphan_Removal_.28JPA_2.0.29