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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: problem with casting
PostPosted: Tue Mar 18, 2008 3:14 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
hi, I recive fallowing problem
java.util.ArrayList cannot be cast to org.hibernate.Query

what can I do?
please help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:18 pm 
Beginner
Beginner

Joined: Fri Aug 24, 2007 4:46 am
Posts: 36
Location: Bielefeld / Germany
Can you provide a code-sample which demonstrates the problem? It's hard to guess what's wrong without any code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:23 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
ProductsManager:
Code:
public List sucheProdukte (String name) {
      
      List res = new ArrayList();
      
      Session session = HibernateUtil.getSessionFactory().openSession();
       Transaction tx = session.beginTransaction();

        res = session.createQuery("from Products where name='" + name + "'").list();
        ((Query) res).setFirstResult(0);
        ((Query) res).setMaxResults(10);
        System.out.println(name);
           
        tx.commit();
        session.close();
      
      return res;
   }


ProductsController
Code:
ProductsManager pm = new ProductsManager();
public String suchen() {
      
      
      
      result = pm.sucheProdukte(this.name);
      
      return "suchen_success";
   }
   

   
   public List getResult() {
   
      return result;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public void setResult(List result) {
      this.result = result;
   }



I'm doing it with jsf , i will create searchfunction and put results in teh table


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:25 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
there is your problem:

Code:
((Query) res).setFirstResult(0);
        ((Query) res).setMaxResults(10);



res is the returned list. You need to assign query to a variable first and then calling its methods.


Code:
Query query = session.createQuery("from Products where name='" + name + "'");

query.set....

res = query.list();





Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:33 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
ok, I made so ,but I recive as result :

select
products0_.productId as productId0_,
products0_.categoryId as categoryId0_,
products0_.Name as Name0_,
products0_.Description as Descript4_0_,
products0_.UnitPrice as UnitPrice0_
from
products products0_
where
products0_.Name='a' limit ?
no answare:(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:35 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What do you mean no answer? There is nothing coming back from database?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:36 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
yes I recive olny :
Code:
20:31:55,230 INFO  [STDOUT] Hibernate:
    select
        products0_.productId as productId0_,
        products0_.categoryId as categoryId0_,
        products0_.Name as Name0_,
        products0_.Description as Descript4_0_,
        products0_.UnitPrice as UnitPrice0_
    from
        products products0_
    where
        products0_.Name='a' limit ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:38 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Does this sql result in any answer in your database? Do you iterate over the returned list in order to see what you get back?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:43 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
I recive answare from sql, but I don't use iteration


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:45 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
patrick1984 wrote:
I recive answare from sql, but I don't use iteration



but you need to. You should iterate over the returned list and use the Product objects for whatever reason you needed to retrieve them in the first place.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:48 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
I used iteration , but resullt is the same
iteration :
Code:
public List hitsS = pm.sucheProdukte(name);
      {
         for(int i=0; i < hitsS.size(); i++){
         Products p = (Products) hitsS.get(i);   
           System.out.println(p.getProductid()+  p.getDescription()+ p.getName()+p.getUnitPrice()+p.getCategorie());
         }
      }
      
      
       public List getHitsS(){
          return this.hitsS;
       }
      
       public int getSize(){
          return this.hitsS.size();
       }


I recive :
Code:
20:44:47,258 INFO  [STDOUT] Hibernate:
    select
        products0_.productId as productId0_,
        products0_.categoryId as categoryId0_,
        products0_.Name as Name0_,
        products0_.Description as Descript4_0_,
        products0_.UnitPrice as UnitPrice0_
    from
        products products0_
    where
        products0_.Name='c' limit ?



Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:54 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I don't think if I understand your question correctly. What else are you looking for? What do you think is it should do?



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 4:00 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
I should recive searchresults , for example I'm scheraching name :"a"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 4:06 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
patrick1984 wrote:
I should recive searchresults , for example I'm scheraching name :"a"



Code:
List res = new ArrayList();
     
      Session session = HibernateUtil.getSessionFactory().openSession();
       Transaction tx = session.beginTransaction();

        res = session.createQuery("from Products where name='" + name + "'").list();

for (int i=0; i<res.size(); i++)
{
Products p = (Products) res.get(i);

System.out.println(p.getName());
}
           
        tx.commit();
        session.close();
     



you query does not have any match unless this writes something on screen.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 4:22 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
thans a lot!!!!:D
I have one more problem .....I'd like to load records from last 3 days .I's it possible?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.