0
votes

I have a master table named bmu_mstr and corresponding entity class is

@Entity
@Table(name = "bmu_mstr")
@JsonIgnoreType
//@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property="@bmuId")
public class BmuMaster implements Serializable{


    private static final long serialVersionUID = -8284996311089344328L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "bmu_id", unique = true, nullable = false)
    private Integer bmuId;

    @Column(name = "bmu_desc")
    private String bmuDesc;

    public BmuMaster() {
    }

    public BmuMaster(Integer bmuId) {
        this.bmuId = bmuId;

and other table is comp_rsc_move_employee which has tranfer_from and transfer_to column..basically the bmu_id of the bmu master table. thecorresponding class is

@Entity
@Table(name = "comp_rsc_move_employee")
public class CompRscEmployee implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer rscId;

    @ManyToOne
    @JoinColumn(name = "transfer_from")
    private BmuMaster transferFrom;

    @ManyToOne
    @JoinColumn(name = "transfer_to")
    private BmuMaster transferTo;

now I want to update data in comp_rsc_move_employee table

        CompRscEmployee compRscEmployee = new CompRscEmployee();


        compRscEmployee.setTransferFrom(new BmuMaster(employeeDetails.getTransferFrom()));
        compRscEmployee.setTransferTo(new BmuMaster(employeeDetails.getTransferTo()));

        CompRscEmployee rscEmp = compDao.saveOrUpdate(compRscEmployee)
                .orElseThrow(() -> new CommonException("Unable to save or update CompRscEmployee data"));

But now when ever i want to save CompRscEmployee hibernate tells 'object references an unsaved transient instance - save the transient instance before flushing'

I searched all stackoverflow thread but did not come up with my scenario..My BmuMaster is MASTER table i dont want to save data in that table when inserting in CompRscEmployee table. obviously i can change the entity class CompRscEmployee and can change the data type from BmuMaster to Integer. But I dont want to do that.. Any ideas how to achieve that?? It seems like a very basic problem. But i am unable to do that as i am quite new to hibernate..

1

1 Answers

0
votes

You should load the BmuMaster instances from the database, not create new ones, to pass into the setTransferFrom and setTransferTo methods.

Not sure how your code is setup, but something like this


BmuMaster from = masterDao.findById(employeeDetails.getTransferFrom())
        .orElseThrow(() -> new CommonException("Unable to find department" + employeeDetails.getTransferFrom()));
BmuMaster to = masterDao.findById(employeeDetails.getTransferTo())
        .orElseThrow(() -> new CommonException("Unable to find department" + employeeDetails.getTransferTo()));

CompRscEmployee compRscEmployee = new CompRscEmployee();
compRscEmployee.setTransferFrom(from);
compRscEmployee.setTransferTo(to);

CompRscEmployee rscEmp = compDao.saveOrUpdate(compRscEmployee)
        .orElseThrow(() -> new CommonException("Unable to save or update CompRscEmployee data"));