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
|