File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Help in Hibernate accessing objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Help in Hibernate accessing objects" Watch "Help in Hibernate accessing objects" New topic
Author

Help in Hibernate accessing objects

Rajeswari Gopal
Greenhorn

Joined: Apr 14, 2005
Posts: 26
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());
}
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help in Hibernate accessing objects