-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Casting from Hibernate query Object to custom class object
PostPosted: Wed Mar 29, 2006 5:49 am 
Newbie

Joined: Wed Mar 29, 2006 5:21 am
Posts: 4
Hibernate version: 3.1

Name and version of the database you are using: MySQL 5



Hi,

I have 2 tables (Owners, Cats) on my DB, both mapped with MyEclipse Hibernate support.

This DAO works very well and returns all the owners:



public Owners[] getAllOwners()
{
List ownersList = new ArrayList();


//Open communication with DB
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();

String query = "from Owners";


List tempList = session.createQuery(query).list();

for(Iterator i = tempList.iterator(); i.hasNext();)
{
ownersList.add((Owners) i.next());
}


tx.commit(); //Execute command
HibernateSessionFactory.closeSession();


return (Owners[]) ownersList.toArray(new Owners[0]);
}//end Get



Then, in the JSP page i use Struts logic:iterator to show (in a table's column) all Owners names ('OwnerName' is an attribute of Owners table on DB)


Now, I've created the 'PetTeam' class (a PetTeam is an association between an Owner and his cat):


public class PetTeam implements java.io.Serializable
{
private String ownersName;
private String catName;


constructor()..

set()...

get()...

}




When I try to use the following DAO:


public PetTeam[] getAllPetTeam()
{
List petTeamList = new ArrayList();


//Open communication with DB
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();

String query = "select o.OwnerName , c.CatName from Owners as o, Cats as c where o.idOwner = c.idOwner";


List tempList = session.createQuery(query).list();

for(Iterator i = tempList.iterator(); i.hasNext();)
{
petTeamList.add((PetTeam) i.next()); //CAST EXCEPTION HERE!
}


tx.commit(); //Execute command
HibernateSessionFactory.closeSession();


return (PetTeam[]) petTeamList.toArray(new PetTeam[0]);
}//end Get




I receive a class cast exception..

I've verified (with debug) that the result objects from query have exactly two Strings as a PetTeam instance..

PetTeam class have not a specific table on DB, so hibernate mapping doesn't exist..

Is it possible to fill the attributes of a custom class (without a table on DB), starting from results of hibernate query?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 7:53 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

The answer to your question is yes.

What's the problem in your code ? Your query

String query = "select o.OwnerName , c.CatName from Owners as o, Cats as c where o.idOwner = c.idOwner";

is returning a List of Object[] arrays. So the code should be something like:

public PetTeam[] getAllPetTeam()
{
List petTeamList = new ArrayList();


//Open communication with DB
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();

String query = "select o.OwnerName , c.CatName from Owners as o, Cats as c where o.idOwner = c.idOwner";


List tempList = session.createQuery(query).list();

for(Iterator i = tempList.iterator(); i.hasNext();)
{
Object[] petComponents = (Object[]) pairs.next();
PetTeam petTeam = new PetTeam();
petTeam.setOwnersName = (String)petComponents[0];
petTeam.setCatName = (String)petComponents[1];

petTeamList.add(petTeam); //NO LONGER CAST EXCEPTION HERE!
}


tx.commit(); //Execute command
HibernateSessionFactory.closeSession();


return (PetTeam[]) petTeamList.toArray(new PetTeam[0]);
}//end Get

I didn't try to compile the code, so maybe you will have to do some minor changes.

Enjoy.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 8:25 am 
Newbie

Joined: Wed Mar 29, 2006 5:21 am
Posts: 4
Thanks!

your tweak works well, but is it possibile to use "select new PetTeam()" statement to get the same result?

"select new" could simplify the code... I've tried it, but I receive a ClassNotFound PetTeam exception , but PetTeam class is correctly compiled... I don't undestand...

thanks again..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 8:37 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi again,

Well, I think it can be done, but I don't think it will simplify your code. And since your not an advanced hibernate user go on with this solution. Later you will be able to discover yourself the solution.

And if the previoius answer helped, don't forget to rate (somethere where's a magic buton).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.