-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Same EXACT code yet session.find returns wrongly
PostPosted: Thu Apr 29, 2004 9:25 pm 
Newbie

Joined: Thu Apr 01, 2004 6:28 pm
Posts: 15
I have ALMOST the same function but for some
reason session.find returns me very wrongly.
I've struggled with this for almost five hours and I'm giving up
Here is the right code, that returns INSTANCES of Product CLASS
Code:
   public Collection findProductsByProductsIds(Map argMapProductsIds){
   List foundProducts = new ArrayList();
   try{
      Session session = HibernateUtil.currentSession();
         Transaction tx = null;
      try {
         tx = session.beginTransaction();
         String sqlcmd = "select product from Product as product where product.productid=:productid";
         for (Iterator i = argMapProductsIds.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry e = (Map.Entry) i.next();
             System.out.println("Finding:" + e.getKey() + " - " + e.getValue());
foundProducts.add(session.find(sqlcmd,e.getKey(),Hibernate.INTEGER));
         }
                  if (foundProducts.size() == 0) {
            System.out.println("Products nao encontrados - using map as guide");
            return null;
         }
      }catch (Exception e) {
      System.out.println("Erro tx");
         try {
            if (tx!=null) tx.rollback();
            throw e;
         }catch(Exception exrollback){
            exrollback.printStackTrace();
         }
      }
      finally {
         HibernateUtil.closeSession();
      }//segundo try
   } catch(Exception ex){
      ex.printStackTrace();
   }//primeiro try
   return foundProducts;
   }

When I do foundProducts.get(0) gives me an @org.jboss.Product etc. Which is right, but Here the ALMOST SAME FUNCTION returns wrongly

Code:
   public Collection findProductsByProductsIds(List argListProductsIds){
   List foundProducts = new ArrayList();
   try{
      Session session = HibernateUtil.currentSession();
         Transaction tx = null;
      try {
         tx = session.beginTransaction();
         String sqlcmd = "select product from Product as product where product.productid=:productid";
         
         Integer tmpInteger = new Integer(0);
         for (int i = 0; i < argListProductsIds.size(); i++) {
         System.out.println("adicionando:" + argListProductsIds.get(i));
         tmpInteger = (Integer) argListProductsIds.get(i);
foundProducts.add(session.find(sqlcmd,tmpInteger,Hibernate.INTEGER));
//foundProducts.add(session.find(sqlcmd,argListProductsIds.get(i),Hibernate.INTEGER));
         }
         if (foundProducts.size() == 0) {
            System.out.println("Produtos nao encontrados - findProductsByProductsIds");
            return null;
         }
      }catch (Exception e) {
      System.out.println("Erro tx");
         try {
            if (tx!=null) tx.rollback();
            throw e;
         }catch(Exception exrollback){
            exrollback.printStackTrace();
         }
      }
      finally {
         HibernateUtil.closeSession();
      }//segundo try
   } catch(Exception ex){
      ex.printStackTrace();
   }//primeiro try
   return foundProducts;
   }

When I do foundProducts.get(0) I receive "[ ]". AND I cant access the data. ALMOST EXACT CODE returning diferrent instances
??
Where is the error, I mean I did the above code by CUT AND PASTE, how you can FORCE find to return instances instead of arrays?
OBS: The arguments on session.find(x,Object,x); are BOTH objects, I dont get it


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 4:21 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
What do you insert into your Map?

From what I can see, your List is a list of Integers of the product ID's you want to find.

Your Map, uses the Key OBJECT, not necessarily an Integer to find on.

Do a System out of e.getKey() (and try cast it to Integer) and of your tmpInteger, and see if they're the same.

In fact, what you can do, is write a method that takes the map AND the list, and then while you iterate the Map, simply see if you can find the corresponding item in the list.

Also, try using the List items as Objects instead of Integers in your List method, and see if that changes your result. i.e. Don't type cast the list items)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 7:30 am 
Newbie

Joined: Thu Apr 01, 2004 6:28 pm
Posts: 15
Brannor McThife wrote:
What do you insert into your Map?

From what I can see, your List is a list of Integers of the product ID's you want to find.
Your Map, uses the Key OBJECT, not necessarily an Integer to find on.

just tried foundProducts.add(session.find(sqlcmd,argListProductsIds.get(i),Hibernate.INTEGER));
but with the arguments on the list as object. or even foundProducts.add(session.find(sqlcmd, (Object)argListProductsIds.get(i),Hibernate.INTEGER));
same result
Quote:
Do a System out of e.getKey() (and try cast it to Integer) and of your tmpInteger, and see if they're the same.
In fact, what you can do, is write a method that takes the map AND the list, and then while you iterate the Map, simply see if you can find the corresponding item in the list.

I think I will have to do what I was avoindind I will have to CONVERT my list to a map then use the same function. but that make you feel lika Luser not a decent programmer.
Quote:
Also, try using the List items as Objects instead of Integers in your List method, and see if that changes your result. i.e. Don't type cast the list items)

Didnt work as you can see


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.