• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Effective way for checking whether the entity exist before persisting

 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic