Answer the question
In order to leave comments, you need to log in
Why can't I save?
Hello!
please tell me how to solve this problem.
I have two entities with a shared primary key, when saving to the database, it swears:
an attempt was made to assign an identifier from a null unique property
@Data
@NoArgsConstructor
@Entity(name = "Employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(mappedBy = "employee", cascade = CascadeType.ALL)
private Company company;
}
@Data
@NoArgsConstructor
@Entity
public class Company {
@Id
private Long id;
private String name;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private Employee employee;
}
org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.example.hibernate.model.Company.employee]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question