I am using Jpa-Hibernate , I want to know if there is any effective way of checking whether the entity exist in the database before persisting it actually to the database.
Let me explain my question with the following example
I have created a function which takes object as an argument and does persisting for you with all the boilerplate code of creating transaction , closing session.
1. Create a Person entity
2. Persist the person entity using the above function.
3. Now if someone by mistakenly again calls the above function with the same entity which has been already persisted , then i will get
So i wanted to know whether there is an effective way (in terms of performance ) of checking whether the entity exist in the database and then do a persist.
Something like the following
I dont want to query the database for that check , some solution through which i can do a check in memory area which Hibernate uses.
I just found the solution to my typical scenario i should replace persist with merge in the function.
I am new to Jpa-Hibernate , so as i am learning i also want to consider the performance implications.
So i will still like to know/hear from ull if any better or appropriate solution with respect to Performance.