I’m trying to do something that I don’t know if it’s possible.
I’ll put you in context.
I’m trying to save an Object with this structure.
class ObjectOne {
@Id...@GenerateValue...
private Integer Id;
private String name;
@OneToMany(mappedBy = "objectOne", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private final List<ObjectTwo> objects = new ArrayList<>();
...
}
class ObjectTwo {
@EmbeddedId
private ObjectTwoId id;
private String description;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColum(name = "object_one_id", referencedColumnName = "id" , nullable = false)
private ObjectOne objectOne;
...
}
@Embeddable
class ObjectTwoId {
private Integer objectOneId;
private String languageIso;
}
With this structure I trying to save a new ObjectOneEntity with a ObjectTwoEntity list.
I was hoping Hibernate could handle this by first doing an INSERT of ObjectOneEntity, and assigning it to ObjectTwoEntity with the generated id of ObjectOneEntity.
But unfortunately, it is first trying to insert the nested class ObjectTwoEntity and it gives me an error because it cannot assign a null value to the id ObjectOneId of the embeddedId class.
Is there any way to delegate this work to Hibernate or should I do the inserts separately?