简体   繁体   English

保存具有多个相同类型对象的 hibernate 实体

[英]saving hibernate entity with multiple objects of same type

When I save the below entity, it is duplicating the same value in another object:当我保存以下实体时,它会在另一个 object 中复制相同的值:

For example, from my struts project (the screen) if i change the value of field shipmentShipper then shipmentConsignee is also updated with the same value.例如,从我的 struts 项目(屏幕)中,如果我更改字段shippingShipper的值,则shippingConsignee也会更新为相同的值。

Entity:实体:

package com.logistics.entities.orderManagement;

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

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.dsiksjane.beans.common.NecessaryFields;
import com.logistics.entities.customers.Customers;

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name="orders") 
@Getter
@Setter
public class Orders extends NecessaryFields {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;

    @Column(name="consignee_ntn")
    private String consigneeNtn;

    /**
     * Clearing Agent
     */
    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)   
    @JoinColumn(name="clearing_agent_name")
    private Customers clearingAgentName;

    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name="shipment_shipper")
    private Customers shipmentShipper;

    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name="shipment_consignee")
    private Customers shipmentConsignee;

    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name="shipment_notify_party")
    private Customers shipmentNotifyParty;

    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name="shipment_forwarder")
    private Customers shipmentForwarder;


    @Column(name="clearing_agent_challan")
    private String clearingAgentChallan;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "order_details_id", referencedColumnName = "id")
    private OrdersDetail orderDetails;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "order_address_id", referencedColumnName = "id")
    private OrdersAddress orderAddress;

}

The repository class:存储库 class:

@Service
public class OrderManagementService implements IOrderManagementService {

    @Autowired
    private IOrderManagementDAO dao;

    @Override
    public OrderManagementCO save(OrderManagementCO orderManagementCO, LoginBean loggedInUser) throws Exception {
    
        Orders obj = orderManagementCO.getOrder();
    
        if( obj.getId() != null ) {
            obj.setUpdatedBy(loggedInUser.getUserName());
            obj.setUpdatedDate( new java.util.Date() );
        } else {
            obj.setCreatedBy(loggedInUser.getUserName());
            obj.setCreatedDate( new java.util.Date() );
            obj.setCompanyId( loggedInUser.getCompanyId() );
        }
    
        dao.save( obj ); 
    
        return orderManagementCO;
    }
}

Fields mapping in JSP (working fine): JSP 中的字段映射(工作正常):

<s:select id="selectShipper" class="custom-field"
name="orderManagementCO.order.shipmentShipper.id" 
label="Service Type" headerKey="-1" headerValue="Select Shipper" 
list="%{customersList}" listKey="id" listValue="fullName" />

<s:select id="selectConsignee" class="custom-field"
name="orderManagementCO.order.shipmentConsignee.id" 
label="Consignee" headerKey="-1" headerValue="Select Consignee" 
list="%{customersList}" listKey="id" listValue="fullName" />

Please check in the screenshot, i changed shipper as C4 and saved.请检查屏幕截图,我将托运人更改为 C4 并保存。 Consignee was also selected as C4.收货人也被选为C4。 And please note that both selects are populated with same data list.请注意,两个选择都填充了相同的数据列表。 (customersList) (客户名单)

在此处输入图像描述

I think, you should use value attribute in your <s:select> tag, and that should have the value that you have sent to backend.我认为,您应该在<s:select>标记中使用value属性,并且该属性应该具有您发送到后端的值。 May be something like value="%{orderManagementCO.order.shipmentShipper.id}" and for the other one it should be value="%{orderManagementCO.order.shipmentConsignee.id}" .可能类似于value="%{orderManagementCO.order.shipmentShipper.id}" ,而另一个应该是value="%{orderManagementCO.order.shipmentConsignee.id}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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