"IdentifierGenerationException: attempted to assign id from null one-to-one property" in Hibernate
Issue
-
Using
@JoinColumn
to explicitly set the column name for an inherited@Id
property for a derived entity using@MapsId
@Entity public class Employee { @Id public String getName() { ... } private void setName(String name) { ... } @OneToOne(mappedBy = "employee", cascade = CascadeType.ALL ...) public Task getTask() { ... } public void setTask(Task task) { ... } ... } @MappedSuperclass public abstract class BaseEntity { ... @Id @Column(name = "id", insertable = true, updatable = false) public String getId() { ... } private void setId(String id) { ... } ... } @Entity public class Task extends BaseEntity { ... @OneToOne @JoinColumn(name = "id", nullable = false) // otherwise key column would be employee_name @MapsId public Employee getEmployee() { ... } protected void setEmployee(Employee employee) { ... } ... }
-
On merging an
Employee
instance after supplying an associatedTask
, encountering an exception... org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property ...
Environment
- Red Hat JBoss Enterprise Application Platform (EAP) 7.2
- Hibernate 5.3
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.