NHibernate - 使用条件查询检索特定列和计数

时间:2010-03-22 15:24:46

标签: c# nhibernate joined-subclass

这是我的映射文件:

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>

这是另一个连接子类

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"  />
        <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>

如何使用条件进行此查询?

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

非常感谢!

非常感谢!它解决了我的一部分问题。我这样做了:

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>());

我使用Transformer来制作Contacto个实体的集合,但问题在于属性Apellido。我在子类Persona中有它,并且该集合仅使用父类而不是子类。

你知道是否有可能解决这个问题?或者我唯一的解决方案就是抛出每个集合项而不使用变换器并创建一个新的Contacto实体集合来创建每个新对象?

感谢!!!

1 个答案:

答案 0 :(得分:1)

您想要使用投影。像这样:

session.createCriteria(ContactoEntity.class)
  .setProjection(Projections.projectionList()
    .add(Projections.groupProperty("id")
    .add(Projections.groupProperty("nombre")
    .add(Projections.groupProperty("apellido")
    .add(Projections.rowCount()));

更新 - 我刚刚注意到你正在使用NHibernate,而且我是一个Java用户,因此我提供的代码片段可能需要进行调整以供您使用。希望它有所帮助。