I try to cascade persist two entities: Salon (parent) and Subscription with a bidirectional @OneToOne association. The Entities' code is following:
{code:java} @Entity public class Salon {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "custom_sequence") @SequenceGenerator(name = "custom_sequence", sequenceName = "custom_sequence", allocationSize = 1) @Column(nullable = false) private Long id;
@OneToOne(mappedBy = "salon", cascade = CascadeType.ALL, orphanRemoval = true) private Subscription subscription; } {code}
and
{code:java} @Entity public class Subscription {
@Id private Long id;
@OneToOne @MapsId private Salon salon; } {code}
I try to persist newly created Salon object with newly created Subscription object set to the Salon. This is the code snippet:
{code:java} @Test public void test() { Salon salon = generateRandomSalon(); Subscription subscription = new Subscription(); salon.setSubscription(subscription); salonService.save(salon); } {code}
And as a result I get "org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [my.project.subscription.Subscription.salon]" what inicates that Hibernate tries to persist Subscription entity before before persisting Salon entity.
I'm using Hibernate 5.2.17. |
|