hibernate one to many query using mappings

2019-08-08 07:18发布

问题:

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:

回答1:

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.