jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
186 stars 55 forks source link

auto-persist objects added to an association #634

Closed gavinking closed 1 month ago

gavinking commented 1 month ago

Hibernate has always had the ability to automatically apply the persist operation to a new entity referenced by an association at flush-time. Historically, this feature, along with orphan deletion, was mixed into our native CascadeTypes, and was never an explicit option.

JPA 2.0 added separated orphan deletion into a dedicated orphanRemoval setting, which is fine and at least arguably better, but it seems like the "auto persist" side of this escaped everyone's attention (including mine).

I happen to think this is pretty useful, and so I would propose adding a new setting to association mapping annotations, so that you could write, for example:

@OneToMany(mappedBy=PARENT, 
           autoPersist=true, orphanRemoval=true)
List<Child> children;

Then you could make a Child persistent simply by calling parent.children.add(child) with no explicit call to em.persist(). Of course the Child only becomes persistent after a flush.

gavinking commented 1 month ago

Apologies for the noise, what I wrote above is nonsense and JPA already has this rolled into the semantics of PERSIST.

Embarrassing.