Sachin Titus
Sachin Titus

Reputation: 2349

JpaSystemException: attempted to assign id from null one-to-one property Error with Jpa and Hibernate

Resolved [org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.example.bootapp.entity.Employee.user]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.example.bootapp.entity.Employee.user]]

User Entity

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "id")
@JsonIgnore
@Getter @Setter
private Employee employee;

Employee Entity

@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JsonIgnore
private User user;

Thanks in advance!

Upvotes: 1

Views: 5884

Answers (1)

Sachin Titus
Sachin Titus

Reputation: 2349

Solved!

user.setEmployee(null); // I added this line
        userCommandService.save(user);
        employee.setUser(user);
        employeeCommandService.save(employee);

By setting employee inside the user entity to null, my error disappeared!

Upvotes: 1

Related Questions