Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:07:06+00:00 2026-06-08T17:07:06+00:00

When creating new entities from user input I can’t be sure that there is

  • 0

When creating new entities from user input I can’t be sure that there is no other entity with identical data. The only examples I was able to find used predefined Ids, so one could check if the entity exists by calling entityManager.find(id). If the id is generated, I see several ways to persist safely:

  • call entityManager.persist and hope for the best – catching the EntityExistsException and either swallowing it or displaying an error message
  • call entityManager.merge and let JPA do the checking…will it always work properly?
  • search for the entity by a query – jPQL or criteria and call entityManager.persist if not found

My intuition is to go with the third approach, but is there a better way still?

I am working with JPA 2 and would like to avoid vendor-specific solutions (like Hibernate’sSessionManager.saveOrUpdate)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:07:07+00:00Added an answer on June 8, 2026 at 5:07 pm

    If you really can’t predict whether the newly generated ID will be unique I’d definitely go with the third approach.

    Also, I don’t think you’ll need Criteria for such a simple query, and in most cases the cost of that extra query won’t be very costly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I avoid third party code from creating new threads, starting new VMs, or
when i am creating new android project from visual studio 2010 it gives the
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I need to restrict the development users from creating new schedules for SQL Agent
I have some .NET 4 entity framework objects that I get from the DB,
I have a working Action and JSP form that I'm using for creating new
Preamble: I'm creating some tables in SQL Server that will be accessed via Linq-to-entities
Good Day, I'm having few issues with Doctrine entities generator while creating new project
I am trying to add a transaction to keep from creating two entities with
I have a form that creates a new brand. The user chooses a name,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.