• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help in Hibernate accessing objects

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
In the following code am fetching a list object which has two different object values i.e Person,UserModel. can you please help me to retrive the values in the list.
i am getting list of values if am using one object person or Usermodel but the following code is not working..
am getting classcastException..

Please reply ASAP.

CODE
-------
String SQL_QUERY1="SELECT person.firstName, person.lastName, user.firstName, user.lastName "+
"FROM Person person,"+
"UserModel user " +
"WHERE person.id=user.userId";

ArrayList joinlist=(ArrayList)sessionFactory.getCurrentSession().createQuery(SQL_QUERY1).list();

for(int i=0;i<joinlist.size();i++)
{
System.out.println(joinlist.get(i) instanceof Person);
System.out.println(joinlist.get(i) instanceof UserModel);
System.out.println(joinlist.getClass());
System.out.println(joinlist.get(i) instanceof List);

Person ppp =(Person)joinlist.get(i); System.out.println("person table details");
System.out.println("--------------------");
System.out.println("firstname----->"+ppp.getFirstName());
System.out.println("lastname------>"+ppp.getLastName());
UserModel um =(UserModel)joinlist.get(i);
System.out.println("users table details");
System.out.println("--------------------");
System.out.println("firstname----->"+um.getFirstName());
System.out.println("lastname------>"+um.getLastName());
System.out.println("age------>"+um.getAge());
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String SQL_QUERY1="SELECT person.firstName, person.lastName, user.firstName, user.lastName "+
"FROM Person person,"+
"UserModel user " +
"WHERE person.id=user.userId";


Your query asks for specific properties of the object. In HQL (your variable name is confusing, you should probably change it) if you do this the return type is an object array. If is it Person objects you are after, don't specify specific fields.
reply
    Bookmark Topic Watch Topic
  • New Topic