• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 and Hibernate

 
Hardik Patel p
Ranch Hand
Posts: 40
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

str="select * from stud where id=1"; // this will fetch one record.

Student s = (Student) session.createQuery(str);

system.out.println( s.getid + " " + s.getname);


ERROR :

org.hibernate.impl.QueryImpl cannot be cast to Student


Note : Inshort , Its ok if we use iterate if query return multiple records. In above query return only one record so I dont want use iterator. and like this (classname.class, 1). Request all user to solve this.

Many thanks
Hardik

 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code cannot possibly work as createQuery returns a Query instance, not a database entity (see the documentation). You still need to execute the query to get the results.
However, since you want to find a single entity, you should probably look at something like this.
Since this has nothing to do with Struts, I'll move it to the Hibernate forum if you have any further questions.
 
Anil Sable
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey , looks you want to retrived one record from database with perticular id

try to use Criteria for such purpose



consider HibernateUtil is your helper class to obtain sessionFactory


then code looks like


Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Criteria cr = session.createCriteria(Student.class);

cr.add(Restrictions.eq("id",1));

Student obj = cr.uniqueResult();

then perform what ever you want to perform


if you think that multiple student will retrive then you could use

List<Student> list = cr.list();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic