aspose file tools*
The moose likes Object Relational Mapping and the fly likes Struts2 and Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Struts2 and Hibernate" Watch "Struts2 and Hibernate" New topic
Author

Struts2 and Hibernate

Hardik Patel p
Ranch Hand

Joined: Jan 23, 2014
Posts: 38

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

Joined: Oct 29, 2001
Posts: 8968
    
    9

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.


[How To Ask Questions On JavaRanch]
Anil Sable
Greenhorn

Joined: Jan 08, 2012
Posts: 10
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();
 
 
subject: Struts2 and Hibernate