简体   繁体   中英

Hibernate one to many relation with update child table

I have two pojo classes named OwnerDetails & VehicleDetails , which are show below

@Entity
public class OwnerDetails implements Serializable{


    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="Owner_id")
    private int id;
    @Column(name="owner_name")
    private String ownerName;
    @Column(name="email_id")
    private String email;
    @Column(name="mobile")
    private String mobile;
    @Column(name="land_line")
    private String phone;
    @Column(name="Mailing_Address")
    private String mailing_address;

    @OneToMany(mappedBy="ownerDetails", cascade=CascadeType.ALL)
    private Set<VehicleDetails> vehicleDetails;
    // getter & Setter...

}

@Entity
public class VehicleDetails implements Serializable{


    private static final long serialVersionUID = 1L;


    @Id
    private int v_id;
    @Column(name="TYPE")
    private String type;
    @Column(name="AC_OR_NON_AC")
    private boolean aircondition;
    @Column(name="SEATING_CAPACITY")
    private int seatingCapacity;
    @Column(name="FUEL_TYPE")
    private String fuel_type;
    @Column(name="Reg_number")
    private String reg_number;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="Owner_id")
    private OwnerDetails ownerDetails;
// getter & Setter....

}

And the main program is

public static void main(String[] args) throws InterruptedException {


        SessionFactory sf=getSessionFactory();
        Session session=sf.getCurrentSession();
        Transaction txn=session.beginTransaction();

        OwnerDetails own=(OwnerDetails)session.get(OwnerDetails.class, 1);

        Set<VehicleDetails> vhSet=new HashSet<VehicleDetails>();
        VehicleDetails vh=new VehicleDetails();
        vh.setAircondition(true);
        vh.setFuel_type("DIESEL");
        vh.setReg_number("TN32 AA 5555");
        vh.setSeatingCapacity(12);
        vh.setV_id(11);
        vh.setType("INNOVA");
        vh.setOwnerDetails(own);
        vhSet.add(vh);

        own.setVehicleDetails(vhSet);

         session.saveOrUpdate(own);
         txn.commit();

    }

The issue I am having happens after ownerdetails and vehicledetails are saved successfully without any issue.

But I need to add one more vihicledetails with same ownerdetails. It updates current vihicledetails instance with ownerid and existing row in vihicledetails are updated with null ownerid.

please find the below tables.

在此处输入图片说明

Looks like the statement own.setVehicleDetails(vhSet); is causing the vehicleDetails set to be replaced with just the new one. Instead use own.getVehicleDetails().add(vh) . This will add the new vehicleDetails entity to the existing vehicleDetails set associated with ownerDetails instead of replacing it.

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