S
S
student_12022-04-16 16:42:22
Java
student_1, 2022-04-16 16:42:22

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

don't know how to solve
@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;
}

descendant:
@Data
@NoArgsConstructor
@Entity
public class Company {

    @Id
    private Long id;
    private String name;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private Employee employee;

}

Mistake:
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 question

Ask a Question

731 491 924 answers to any question