Bidirectional mapping is giving recusrsion on owning side repository save

Employee (empCode, empAddress, email)

LoginUser (userName, isLocked, empCode(fk @oneToOne))
LoginUser table contains empCode as foreign key.

Employee{
    private String empCode;
    private String empAddress;
    @OneToOne(fetch = FetchType.LAZY, mappedBy = "employee", orphanRemoval = true, cascade = CascadeType.ALL)
    private LoginUser
}

LoginUser{
    private String userName;
    private Boolean isLocked;
    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "empCode")
    private Employee employee;
}

I tried without cascade on both side too, everything. But, it’s going into infinite loop to StackOverFlowError.

Same for save employee too

Employee emp = new Employee();
emp.setEmpCode(“Zenitsu”);
emp.setEmpCode(“NY”);
LoginUser loginUser = new LoginUser();
loginUser.setUserName(“Zenitsu12”);
loginUser.setIsLocked(false);
emp.setLoginUser(loginUser);
empRepo.save(emp);

I am getting Error. This scenario where CascadeType.ALL is even there
Caused by: org.hibernate.AssertionFailure: non-transient entity has a null id: com.titan.poss.user.dao.LoginUser

And in hashcode I am excluding the other Object too

Temporary solution what I am doing is saving both object differently from each one’s repo.


Employee emp = new Employee();
emp.setEmpCode(“Zenitsu”);
emp.setEmpCode(“NY”);
Employee responseEmp  = empRepo.save(emp);

LoginUser loginUser = new LoginUser();
loginUser.setUserName(“Zenitsu12”);
loginUser.setIsLocked(false);
loginUser.setEmployee(responseEmp);
loginRepo.save(loginUser);