my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes Effective way for checking whether the entity exist before persisting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Effective way for checking whether the entity exist before persisting" Watch "Effective way for checking whether the entity exist before persisting" New topic
Author

Effective way for checking whether the entity exist before persisting

Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

Hi ,
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.

Regards,
Shroff.

OCJP-90%,OCPWCD-95%
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

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.

Thanks,
Shroff.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Effective way for checking whether the entity exist before persisting