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 764897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:48:12+00:00 2026-05-14T16:48:12+00:00

So let’s say I have an ArrayList full of Products that need to be

  • 0

So let’s say I have an ArrayList full of Products that need to be committed to the database via Hibernate. There are already a large number of Products in the database. Each product has an ID. Note this is NOT the PK that is autogenerated by Hibernate.

My questions is: what is the best way to detect conflicts with this ID? I am looking for a relatively efficient method of obtaining, from the the database, a List of Products that share an ID with any of the Products in my ArrayList. This is all in a single table called Products and the ID attribute is in column ProductID.

The way I’ve done it is grabbing a list of all Products in the database, and compared each one with each entry in my ArrayList – but that is seriously inefficient and I don’t think it would work well with a larger database. How should it be done? Thanks. I say “relatively” efficient because efficiency is not the primary concern, but it shouldn’t take noticeably long to test against a table of ~1000-5000 rows. Help?

EDIT* I’m very new to hibernate and below is the best I’ve come up with. How does this look?

for(long id : idList){ //idList just holds the IDs of each Product in my ArrayList
    Query query = session.createQuery("select product from Product product where product.id = :id");
    query.setLong("id", id);
    for(int i = 0; i < query.list().size(); i++){
        listOfConflictingProducts.add((Product) query.list().get(i));
    }
}
  • 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-05-14T16:48:13+00:00Added an answer on May 14, 2026 at 4:48 pm

    I would call query.list() only once: otherwise the query could be rerun each time:

    for(long id : idList){ //idList just holds the IDs of each Product in my ArrayList
        Query query = session.createQuery("select product from Product product where product.id = :id");
        query.setLong("id", id);
        List result = query.list();
        for(int i = 0; i < result.size(); i++){
            listOfConflictingProducts.add((Product) result.get(i));
        }
    }
    

    Or, more simply:

    for(long id : idList){ //idList just holds the IDs of each Product in my ArrayList
        Query query = session.createQuery("select product from Product product where product.id = :id");
        query.setLong("id", id);
        for(Object obj: query.list()){
            listOfConflictingProducts.add((Product)obj);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a SQLite database that I create in a separate
Let me explain best with an example. Say you have node class that can
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a bunch of links that share a click event: <a
Let's say that I have a set of relations that looks like this: relations
Let's say I have some data in a CouchDB database. The overall size is
Let's say that I have a html form (actually I have an editor -
Let me try to explain what I need. I have a server that is

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.