Daniel
Daniel

Reputation: 28104

Hibernate - Setting a cascade persist just for one session

I have this relation in Hibernate:

    n      1
A   ------->   B

The cascade types in the @OneToMany from A to B is not CascadeType.PERSIST, and cannot be made so. The @ManyToOne from B to A doesn't have CascadeType.PERSIST either.

Now I have the case where I have a new unpersisted A referring a new unpersisted B which refers to the unpersisted A.

Persisting A leads to the exception, that A referes to the "null or transient instance" B, of course. Persisting B beforehand leads to the same error, because A is not persisted.

How do I solve this? Can I specify a one-time-Cascade-persist?

Upvotes: 2

Views: 472

Answers (1)

limc
limc

Reputation: 40176

If you don't have any cascading set up on the one-to-many part, the only way I can think of is to create and persist A first.. and then you can create and add Bs into the persisted A.

Any particular reason why you do not want to set up the cascade in this bidirectional relationship?

Upvotes: 1

Related Questions