Hibernate-reactive: session.remove && session.getReference

Created on 7 Sep 2020  ·  3Comments  ·  Source: hibernate/hibernate-reactive

Hello, as i understand, following jpa, there is only one way to remove detached entity:

factory.withTransaction((session, transaction) -> 
    session.merge(sampleEntity).invokeUni(session::remove)
).await().indefinitely();

Everything is fine, but if it's about performance, merge operation takes large amount of time and can cause real performance troubles. For instance, according to my inaccurate time measurements, removing 5000 entities takes ~4s, in hibernate-orm it takes 50ms.

I've just read about a new method called session.getReference which returns the persistent instance of given entity. Is it possible to allow its use for session.remove?

bug

All 3 comments

I've just read about a new method called session.getReference which returns the persistent instance of given entity. Is it possible to allow its use for session.remove?

Ah, good point, absolutely you should be able to do that. I've made it work in #360.

However, I want you to understand that there's no way to remove() an object without fetching it first. That's because remove() has semantics like cascade delete, and collection removal that depend on having the state of the entity in the session.

If you absolutely need to be able to delete an entity without fetching it, you should use StatelessSession.delete() or a HQL delete query.

@Xset-s P.S. thanks for reporting this, it was a big oversight on my part

No problem, thanks for your detailed response about removing operation and for a very quick fix. I'm conducting experiments right now and rewriting my server-side application using your new amazing library, so its easy for me to catch some tricky issues, which could only be captured only so (or using a large number of tests 😄)

Was this page helpful?
0 / 5 - 0 ratings