Use of @EmbeddedId and @MapsId causes NULL inserted (NOT NULL constraint violation)

Viewed 1541

With using following code

@Embeddable
public class EmployeeId implements Serializable {
    @Column(name = "company_id")
    private Long companyId;

    @Column(name = "employee_id")
    private Long employeeNumber;
}

@Entity
public class Employee {
    @EmbeddedId
    private EmployeeId id;

    private String name;

    @MapsId("name=companyId")
    @ManyToOne
    @JoinColumn(name = "company_id")
    private Company company;
}

When trying to persist or merge the Employee Entity, we can see that a NULL as attempted to be inserted into the company_id field.

How can I avoid a NULL to be inserted?

1 Answers
Related