简体   繁体   中英

saving hibernate entity with multiple objects of same type

When I save the below entity, it is duplicating the same value in another 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.

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:

@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):

<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. Consignee was also selected as 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. May be something like value="%{orderManagementCO.order.shipmentShipper.id}" and for the other one it should be value="%{orderManagementCO.order.shipmentConsignee.id}" .

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