This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help in Hibernate accessing objects