使用映射休眠一对多查询 [英] hibernate one to many query using mappings

查看:84
本文介绍了使用映射休眠一对多查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



// SigTcContraloriaObjetivos,SigTcContraloriaIniciativas,SigTcContraloriaAcciones

 < class dynamic-insert =falsedynamic-update =truemutable =truename =org.citi.tablero.contraloria.planes.model.db .hibernate.dto.SigTcContraloriaObjetivosoptimistic-lock =versionpolymorphism =implicitselect-before-update =falsetable =SIG_TC_CONTRALORIA_OBJETIVOS> 
< id column =ID_OBJETIVOname =idObjetivo>
< generator class =sequence>
< param name =sequence> SEQ_SIG_CONTRALORIA_OBJETIVOS< / param>
< / generator>
< / id>
< property column =DESCRIPCIONname =descripcion/>
< key column =ID_OBJETIVO/>
<一对多等级=SigTcContraloriaIniciativas/>
< / set>
< / class>

< id column =ID_INICIATIVAname =idIniciativa>
< generator class =sequence>
< param name =sequence> SEQ_SIG_CONTRALORIA_INICIATIVA< / param>
< / generator>
< / id>
< property column =DESCRIPCIONname =descripcion/>
< property column =ID_OBJETIVOname =idObjetivo/>
< key column =ID_INICIATIVA/>
<一对多等级=SigTcContraloriaAcciones/>
< / set>
< / class>

< id column =ID_ACCIONname =idAccion>
< generator class =sequence>
< param name =sequence> SEQ_SIG_CONTRALORIA_ACCIONES< / param>
< / generator>
< / id>
< property column =DESCRIPCIONname =descripcion/>
< property column =ID_INICIATIVAname =idIniciativa/>
< property column =ID_ORGANIZACIONname =idOrganizacion/>
< / class>

我需要一种方法来选择一个SigTcContraloriaIniciativa以及相关的SigTcContraloriaObjetivo和相关的SigTcContraloriaAccion



这是使用以下查询:

  String sql =select distinct p from SigTcContraloriaObjetivos p join p.children c join c.children b where and b.idOrganizacion = 8; (在数据库中,我只有一个SigTcContraloriaAccion idOrganizacion = 8,所以我的预期结果是一个SigTcContraloriaObjetivos与相应的SigTcContraloriaIniciativas与相应的SigTcContraloriaAccion即时选择)

我的问题是当我执行query.list()它返回我一个SigTcContraloriaObjetivos(如预期),两个SigTcContraloriaIniciativas更新:



b

b

更新:这是表格的图像:

解决方案

我认为您的查询正在返回正确的结果,但您不明白。
$ b

正如您所说的那样,正在返回正确的SigTcContraloriaObjetivos对象。你得到的是对象和它的所有关联(假设获取类型是EAGER)。这些关联不会根据您的查询进行过滤。



我认为您期待:

  SigTcContraloriaObjetivos(ID = 1)
----- SigTcContraloriaIniciativas(ID = 1)
----- SigTcContraloriaAcciones(ID = 5)

JPA / Hibernate不能这样工作。这种查询的结果将始终是符合条件的对象,并且这些对象将包含所有关联的对象。


I have 3 classes with the corresponding relationship parent child on them:

//SigTcContraloriaObjetivos, SigTcContraloriaIniciativas, SigTcContraloriaAcciones

<class dynamic-insert="false" dynamic-update="true" mutable="true" name="org.citi.tablero.contraloria.planes.model.db.hibernate.dto.SigTcContraloriaObjetivos" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="SIG_TC_CONTRALORIA_OBJETIVOS">
    <id column="ID_OBJETIVO" name="idObjetivo">
        <generator class="sequence">
            <param name="sequence">SEQ_SIG_CONTRALORIA_OBJETIVOS</param>
        </generator>    
    </id>
    <property column="DESCRIPCION" name="descripcion"/>
     <set name="children" inverse="false" cascade="all" lazy="false">
        <key column="ID_OBJETIVO"/>
        <one-to-many class="SigTcContraloriaIniciativas"/>
    </set>          
</class>

<class dynamic-insert="false" dynamic-update="true" mutable="true" name="org.citi.tablero.contraloria.planes.model.db.hibernate.dto.SigTcContraloriaIniciativas" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="SIG_TC_CONTRALORIA_INICIATIVAS">
    <id column="ID_INICIATIVA" name="idIniciativa">
        <generator class="sequence">
            <param name="sequence">SEQ_SIG_CONTRALORIA_INICIATIVA</param>
        </generator>    
    </id>
    <property column="DESCRIPCION" name="descripcion"/>
    <property column="ID_OBJETIVO" name="idObjetivo" /> 
     <set  name="children" inverse="false" cascade="all" lazy="false">
        <key column="ID_INICIATIVA"/>
        <one-to-many class="SigTcContraloriaAcciones"/>
    </set>      
</class>

<class dynamic-insert="false" dynamic-update="true" mutable="true" name="org.citi.tablero.contraloria.planes.model.db.hibernate.dto.SigTcContraloriaAcciones" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="SIG_TC_CONTRALORIA_ACCIONES">
    <id column="ID_ACCION" name="idAccion">
        <generator class="sequence">
            <param name="sequence">SEQ_SIG_CONTRALORIA_ACCIONES</param>
        </generator>    
    </id>
    <property column="DESCRIPCION" name="descripcion"/>
    <property column="ID_INICIATIVA" name="idIniciativa" /> 
    <property column="ID_ORGANIZACION" name="idOrganizacion" />
</class>

I need a way to select one SigTcContraloriaIniciativa with the associated SigTcContraloriaObjetivo and with the associated SigTcContraloriaAccion

This is the query im using:

String sql = "select distinct p from SigTcContraloriaObjetivos p join p.children c join c.children b where and b.idOrganizacion = 8";

(In the database i only have one SigTcContraloriaAccion with idOrganizacion= 8, so my expected result is one SigTcContraloriaObjetivos with the corresponding SigTcContraloriaIniciativas with the corresponding SigTcContraloriaAccion im selecting)

My problem is when I execute query.list() it returns me one SigTcContraloriaObjetivos (as expected), two SigTcContraloriaIniciativas(Not expected, i only expect one), and two SigTcContraloriasAcciones (only one expected) for each SigTcContraloriaIniciativas

UPDATE:

This is the image of the tables:

解决方案

I think your query is returning the correct result but that you do not understand it.

As you have stated, the correct SigTcContraloriaObjetivos object is being returned. What you get back is the object and ALL its associations (assuming fetch type is EAGER). Those associations are NOT filtered based upon your query though.

I think you are expecting:

SigTcContraloriaObjetivos (ID=1)
----- SigTcContraloriaIniciativas (ID=1)
       ----- SigTcContraloriaAcciones (ID=5)

JPA/Hibernate does not work that way. The result of such a query will always be the object(s) that meet the criteria and those object(s) will contain all their associated objects.

这篇关于使用映射休眠一对多查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆