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

发布于 2024-08-25 22:44:14 字数 4084 浏览 12 评论 0 原文

这是我的映射文件:

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 实体集合来创建每个新对象?

谢谢!!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-09-01 22:44:14

您想使用投影。像这样的事情:

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

更新 - 我刚刚注意到您正在使用 NHibernate,并且我是 Java 用户,因此我给出的代码片段可能需要根据您的使用进行调整。希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文