1

It's a simple question, but I'm new to Hibernate and working in Hibernate 4.2.7. Here is my scenario.

I have a list of product codes so I'm looping through the values like:

Vector<productslist> p = prodcts.getlists();
int size = p.size();
for (int i = 0; i < size; i++) {
    productslist pl = p.elementAt(i) 
    p.setProductcode(pl.getProductcode());
    p.setProductName(pl.getProductName()); 
    session.save(p);
}


Before inserting into database I need to check if the productcode exists in the database. If not I can insert the values, if it exists I need to return back the productID for that productcode.

productID is auto-generated by database.

Please advise how to achieve this.

5
  • Vector!! you really need Vector? Commented Dec 3, 2013 at 19:14
  • @TheEwook That! That's what you notice as the big problem here? Commented Dec 3, 2013 at 19:17
  • Unrelated notes. 1) You seem to have errors in your code: p.setProductcode(), p.setProductName(). "p" is a Vector in your code, so of course there are no such methods there. 2) Related to your productslist class: class names in Java by convention should be upper camelCase (a.k.a. PascalCase). Read more in Code Conventions for the Java Programming Language Commented Dec 4, 2013 at 2:21
  • 1
    @ElliottFrisch Of course not! but I'm always astonished of seeing people posting code that doesn't even compile. So if the op didn't want to bother doing this simple check, why should I ;) +1 for your answer btw! Commented Dec 4, 2013 at 11:46
  • @TheEwook - I apologizes for error code here. What i thought was its just an idea for question, so it doesn't matter for answer. But you guys expect with runnable code.Then i will correct it my mistake and going forward will post compile code. Commented Dec 4, 2013 at 18:41

1 Answer 1

1

You can use HQL to do this; try something like "from products where lower(productcode) = " + productCode. Here are some tutorials on HQL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.