-->
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.  [ 1 post ] 
Author Message
 Post subject: Querying fields from superclass entity on Hibernate Criteria
PostPosted: Tue Oct 19, 2010 1:26 pm 
Newbie

Joined: Tue Apr 13, 2010 6:14 pm
Posts: 6
Location: El Salvador
Hi to all, i have a little issue with Hibernate Criteria. I have 2 entities, one is the super entity of another, as you can see:

Code:
public class ComTiendas  implements java.io.Serializable {
     private long idtda;
     private ComPaises comPaises;
     private String nombre;
     private String estado;
     private String codigoNomina;
     private String codigoInv;
     private String codigoEmpresa;
//Stuff like constructor, getters and setters .....
}

public class ComPaises  implements java.io.Serializable {
     private long idpai;
     private String nombre;
     private String codigoNomina;
     private Set comTiendases = new HashSet(0);
//Stuff like constructor, getters and setters .....
}


ComPaises is father of ComTiendas. So, i want to build a query using Hibernate Criteria in the next way:

Code:
Criteria crit = hSession.createCriteria(ComTiendas.class, "tda").createAlias("tda.comVentaDiariaDptos", "vta").
                            setProjection(Projections.projectionList().
                            add(Projections.sum("vta.montoVenta")).
                            add(Projections.groupProperty("idtda")).
                            add(Projections.groupProperty("nombre")).
                            add(Projections.groupProperty("codigoNomina")).
                            add(Projections.groupProperty("codigoInv")).
                            add(Projections.groupProperty("estado"))
                            );


I, grouping by the fields of ComTiendas and getting a sum of montoVenta, a field from son entity of ComTiendas. This works well. But the problem is when i want to add to the query a field from father entity, ComPaises, i get an error. I tried in three ways:

1) Adding as an alias to criteria, like this:

Code:
Criteria crit = hSession.createCriteria(ComTiendas.class, "tda").createAlias("tda.comVentaDiariaDptos", "vta").
                            setProjection(Projections.projectionList().
                            add(Projections.sum("vta.montoVenta")).
                            add(Projections.groupProperty("idtda")).
                            add(Projections.groupProperty("nombre")).
                            add(Projections.groupProperty("codigoNomina")).
                            add(Projections.groupProperty("codigoInv")).
                            add(Projections.groupProperty("estado"))
                            ).createAlias("comPaises.nombre", "nomPais");


And i get this error:

Code:
org.hibernate.QueryException: not an association: nombre


2) Adding as a group property to Projection List:

Code:
Criteria crit = hSession.createCriteria(ComTiendas.class, "tda").createAlias("tda.comVentaDiariaDptos", "vta").
                            setProjection(Projections.projectionList().
                            add(Projections.sum("vta.montoVenta")).
                            add(Projections.groupProperty("idtda")).
                            add(Projections.groupProperty("nombre")).
                            add(Projections.groupProperty("codigoNomina")).
                            add(Projections.groupProperty("codigoInv")).
                            add(Projections.groupProperty("estado")).
                            add(Projections.groupProperty("comPaises.nombre"))
                            );


And i get this error:

Code:
org.hibernate.QueryException: could not resolve property: comPaises.nombre of: com.datasoft.zara.comis.orm.ComTiendas


2) Adding as a property to Projection List:

Code:
Criteria crit = hSession.createCriteria(ComTiendas.class, "tda").createAlias("tda.comVentaDiariaDptos", "vta").
                            setProjection(Projections.projectionList().
                            add(Projections.sum("vta.montoVenta")).
                            add(Projections.groupProperty("idtda")).
                            add(Projections.groupProperty("nombre")).
                            add(Projections.groupProperty("codigoNomina")).
                            add(Projections.groupProperty("codigoInv")).
                            add(Projections.groupProperty("estado")).
                            add(Projections.property("comPaises.nombre"))
                            );


And i get this error:

Code:
org.hibernate.QueryException: could not resolve property: comPaises.nombre of: com.datasoft.zara.comis.orm.ComTiendas


So i don't know the way to include in my query a field of father entity, or isn't possible to do that?

Regards.

_________________
SCJP 6


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

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.