Paste hibernation without custom instructions

I am trying to insert a new record into a table, which, as I know, is unique in front of you. I tried calling save () on the object, but it does a bunch of SELECT statements before doing any INSERT that I don't want to do, because I know that the object is already unique.

I open a new session for each transaction, and I see that this is a problem, but this is a limitation of my domain. Is there a way to get Hibernate to not do any SELECT before this INSERT?

+4
source share
3 answers

You can use the persist () method rather than save ().

https://forum.hibernate.org/viewtopic.php?f=1&t=1011405

However, unlike save (), persist () does not guarantee that the identifier value will be set immediately in the saved instance.

https://forum.hibernate.org/viewtopic.php?t=951275

+7
source

Hibernate is trying to determine if the object is temporary or not, so it performs a SELECT before INSERT . You may be able to adapt this answer from matching. Hibernate OneToOne executes a select statement before inserting; not sure why to avoid SELECT .

Or, I remember a forum post redefining the version column that hibernate uses in transient checking (and for optimistic blocking). I will edit this answer when I find it.

+2
source

Hibernate does not issue a choice to see if the object is unique with save() . In fact, Hibernate doesn't even throw out an insert when you call save() . This happens when you flush() or commit a transaction. You will need to find out what they are intended for and why to initiate them. To reduce it, you can write a quick test, for example

 Session session = openSession(); Transaction tx = session.beginTransaction(); session.save(myObject); tx.commit(); 

and see what expressions are created.

+1
source

All Articles