4

这是我的映射文件:

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实体集合,但问题出在 propertyApellido上。我在子类 Persona 中有它,并且该集合仅由父类而不是子类组成。

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

谢谢!!!

4

1 回答 1

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 用户,因此我提供的代码片段可能需要进行调整以供您使用。希望能帮助到你。

于 2010-03-23T21:28:16.897 回答