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