NHibernate - Retrieve specific columns and count using criteria query
This is my mapping file:
class name="CRMStradCommon.Entities.OportunidadEntity,CRMStradCommon" table="oportunidad">
<id name="Id" column="id" type="int">
<generator class="native" />
</id>
<property name="Titulo" column="titulo" type="string" not-null="true" />
<many-to-one name="Estado" column="estado" class="CRMStradCommon.Entities.EstadoOportunidadEntity,CRMStradCommon" />
<many-to-one name="Dueno" column="dueno" class="CRMStradCommon.Entities.ContactoEntity,CRMStradCommon" />
<property name="FechaCierreEstimado" column="fecha_cierre_estimado" type="DateTime" not-null="false"/>
<property name="FechaVencimiento" column="fecha_vencimiento" type="DateTime" not-null="false"/>
</class>
This is the other with joined-subclass
class name="CRMStradCommon.Entities.ContactoEntity,CRMStradCommon" table="contacto" dynamic-update="true">
<id name="Id" column="id" type="int">
<generator class="native" />
</id>
<property name="Nombre" column="nombre" type="string" not-null="true" />
<property name="Email1" column="email1" type="string" />
<property name="Email2" column="email2" type="string" />
<property name="Web1" column="web1" type="string" />
<property name="Web2" column="web2" type="string" />
<bag name="DuenoOportunidadList" lazy="true" inverse="true">
<key column="dueno"/>
<one-to-many class="CRMStradCommon.Entities.OportunidadEntity,CRMStradCommon"/>
</bag>
<joined-subclass name="CRMStradCommon.Entities.EmpresaEntity,CRMStradCommon" table="empresa" lazy="false">
<key column="id" />
<many-to-one name="Categoria" column="categoria" class="CRMStradCommon.Entities.CategoriaEmpresaEntity,CRMStradCommon" />
开发者_如何学Python <many-to-one name="Calificacion" column="calificacion" class="CRMStradCommon.Entities.CalificacionEmpresaEntity,CRMStradCommon" />
</joined-subclass>
<joined-subclass name="CRMStradCommon.Entities.PersonaEntity,CRMStradCommon" table="persona" lazy="false">
<key column="id" />
<property name="Saludo" column="saludo" type="string" />
<property name="Apellido" column="apellido" type="string" />
<property name="SegundoNombre" column="segundo_nombre" type="string" />
</joined-subclass>
</class>
How can I make this query with criteria?
SELECT
contacto.id, contacto.nombre, persona.apellido, COUNT(*) AS Cant
FROM
contacto
INNER JOIN
oportunidad ON contacto.id = oportunidad.dueno
LEFT OUTER JOIN
persona ON contacto.id = persona.id
LEFT OUTER JOIN
empresa ON contacto.id = empresa.id
GROUP BY
contacto.id, contacto.nombre, persona.apellido
ORDER BY
contacto.nombre, persona.apellido
Thanks a lot!
Thanks a lot! It solved a part of my problem. I did this:
ICriteria criteria = session.CreateCriteria(typeof(ContactoEntity));
criteria.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("Id"),"Id")
.Add(Projections.GroupProperty("Nombre"),"Nombre")
.Add(Projections.GroupProperty("Apellido"), "Apellido")
.Add(Projections.GroupProperty("TipoContacto"), "TipoContacto")
.Add(Projections.RowCount(),"CantOportunidadesDueno"));
criteria.CreateCriteria("DuenoOportunidadList");
criteria.AddOrder(Order.Asc("Nombre")).AddOrder(Order.Asc("Apellido"));
criteria.SetResultTransformer(
new NHibernate.Transform.AliasToBeanResultTransformer(typeof(ContactoEntity)));
IList<ContactoEntity> ContLst = (criteria.List<ContactoEntity>());
I used Transformer in order to make a collection of Contacto
entities, but the problem is with property Apellido
. I have it in subclass Persona and the collection is made only with parent class and not with children classes.
do you know if it is possible to solve that? or the only solution I have is to go throw each collection item without using transformer and creating a new collection of Contacto entities creating each new object?
Thanks!!!
You want to use projections. Something like this:
session.createCriteria(ContactoEntity.class)
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("id")
.add(Projections.groupProperty("nombre")
.add(Projections.groupProperty("apellido")
.add(Projections.rowCount()));
UPDATE - I just noticed that you're using NHibernate, and I'm a Java user so the code snippet I gave may need to be tweaked for your use. Hope it helps.
精彩评论