-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Using Criteria for joined class fields
PostPosted: Wed May 28, 2008 4:57 am 
Newbie

Joined: Wed Jan 23, 2008 10:02 am
Posts: 3
Scenario:
I've got this class join chain (all of them 'many-to-one' and using fetch="join":
Ticket -> Identita -> Cliente

Within Ticket i have a identita:Identita field
within Identita I have a cliente:Cliente field

I need to express a filter clause over a property of Cliente (say: "name"), which is NOT the PK/FK field

I tried several ways:
1.
Code:
// String name is initialized (a input parameter or whatsoever)
Criteria criteria = session.createCriteria(Ticket.class);
criteria.createCriteria("identita")
          .createCriteria("cliente")
          .add(Restrictions.eq("name", name));
List list = criteria.list();


This code generates exception:
org.hibernate.QueryException: duplicate association path: identita.cliente...

2.
Using Example
I build an Identita istance:
Code:
Identita iden = new Identita();
cliente cli = new Cliente();
cli.setName(name);
iden.setCliente(cli);

Criteria criteria = session.createCriteria(Ticket.class);      criteria.createCriteria("identita")
                   .add(Example.create(sdic));

This code generates no errors, ma the example criteria results in an useless clause "and (1=1)"

What's missing?

below the hbm.xml



Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <!-- TICKET -->
   <class name="it.coopitalia.tew.pojo.base.BaseTicketConsultazione"
      table="TVCE_ANA_TCK">

      <id name="id" column="id_ticket" />


      <discriminator
         formula="case when cod_tip_tck in ('3', '4') then 'MNP'
                       when cod_tip_tck in ('7', '8') then 'CMB'
                       else 'STD' end"
         type="string" />


      <many-to-one name="identita"
         class="it.coopitalia.tew.pojo.StoricoDocumentoIdentitaConsultazione"
         column="num_prg_doc_idt" fetch="join" />

      <!-- Vedere se trasformare in relazioni -->
      <property name="numeroTelefono" column="num_tel" />
      <many-to-one name="pdv"
                    class="net.coopitalia.pojo.PdvCoop" column="cod_pdv"
                    fetch="join" />
        <many-to-one name="cooperativa"
                    class="net.coopitalia.pojo.Coop" column="cod_coop"
                    fetch="join" />
      <property name="dataInserimento" column="dt_ini" />
      <property name="dataInizioUltimoStato" column="dt_ini_ult_sta" />
      <property name="nomeOperatorePdv" column="nom_ope_pdv" />
      <property name="nomeOperatoreDataEntry" column="nom_ope_de" />
      <property name="descrizioneEsitoDataEntry" column="des_esi_de" />
      <property name="descrizioneEsitoPdv" column="des_esi_pdv" />
      <property name="noteDataEntry" column="des_note_de" />
      <property name="notePdv" column="des_note_pdv" />
      <property name="statoDataEntry" column="cod_sta_tck_de" />
      <property name="statoPdv" column="cod_sta_tck_pdv" />
      <property name="dataScadenza" type="timestamp"
         column="dt_sca_tck" />
      <property name="codiceTipo" column="cod_tip_tck" />

      <subclass name="it.coopitalia.tew.pojo.TicketConsultazione"
         discriminator-value="STD">
      </subclass>

      <subclass name="it.coopitalia.tew.pojo.TicketConsultazioneMnp"
         discriminator-value="MNP">
         <join table="tvce_dti_tck_mnp">
            <key column="id_ticket"></key>
            <property name="codiceIccdVecchioOperatore"
               column="cod_iccd_old" />
            <property name="codiceIccdNuovoOperatore"
               column="cod_iccd_new" />
            <property name="codiceFiscaleVecchioOperatore"
               column="cod_fis_old_ope" />
            <property name="cognomeVecchioOperatore"
               column="cog_old_ope" />
            <property name="nomeVecchioOperatore"
               column="nom_old_ope" />
            <property name="tipoDocumentoVecchioOperatore"
               column="tip_doc_old_ope" />
            <property name="numeroDocumentoVecchioOperatore"
               column="num_doc_old_ope" />
            <many-to-one name="vecchioGestore"
               class="net.coopitalia.pojo.GestoreTelefonia" column="cod_gst_old"
               fetch="join" />
            <property name="tipoSimAltroGestore"
               column="des_tip_sim" />
         </join>
      </subclass>
      
      <subclass
         name="it.coopitalia.tew.pojo.TicketConsultazioneCambioCarta"
         discriminator-value="CMB">
         <join table="TVCE_DTI_AGG_CMB">
            <key column="id_ticket"></key>
            <property name="nuovoIccd" column="cod_iccd_new" />
            <property name="vecchioIccd" column="cod_iccd_old" />
            <many-to-one name="causale"
               class="net.coopitalia.pojo.Causale" column="cod_cau_stz"
               fetch="join" />
         </join>
      </subclass>

   </class>
   
   <!-- IDENTITA -->
   <class name="it.coopitalia.tew.pojo.StoricoDocumentoIdentitaConsultazione" table="TVCE_STO_DOC_IDT" >
        <id name="progressivo" column="num_prg_doc_idt">
        </id>
        <property name="numeroDocumento" column="num_doc_idt"/>
        <property name="dataRilascio" column="dt_ril_doc"/>
        <property name="dataPrimoUtilizzo" column="dt_pim_utlz_doc" />
        <property name="dataUltimoUtilizzo" column="dt_ult_utlz_doc" />
       
        <property name="stato" column="cod_sta_doc" />
        <many-to-one name="tipo" class="net.coopitalia.pojo.TipoDocumento"
                     column="cod_tip_doc" fetch="join"/>
        <many-to-one name="cliente" class="net.coopitalia.pojo.Cliente"
                     column="cod_fis_cli" fetch="join"/>

    </class>
   
   <!-- CLIENTE -->
    <class name="net.coopitalia.pojo.Cliente" table="TVCE_CLI">
        <id name="codiceFiscale" column="cod_fis_cli">
        </id>
        <property name="nome" column="nome_cli"/>
       <property name="cognome" column="cog_cli"/>
       <property name="dataNascita" column="dt_nsc_cli"/>
      <property name="codiceComuneNascita" column="cod_com_it" />
      <property name="siglaProvinciaNascita" column="sig_prv_it"/>
      <property name="codiceNazioneNascita" column="cod_nazne"/>
      <property name="sesso" column="flg_sex"/>
      <property name="dataAttivazione" column="dt_atv_cli" />
      <property name="dataCessazione" column="dt_csz_cli" />
      <property name="comuneEsteroNascita" column="com_est_nsc" />
      <property name="email" column="des_ind_mail" />
      <property name="telefonoFisso" column="num_tel_fix" />
      <property name="fax" column="num_fax" />
      <property name="codiceStato" column="cod_sta" />
      
      <set name="utenzeTelefoniche" table="TVCE_UTZ_TEL">
         <key column="COD_FIS_CLI" />
         <one-to-many class="net.coopitalia.pojo.AnagraficaUtenzaTelefonica"/>
      </set>
      
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 9:26 am 
Newbie

Joined: Thu Jul 10, 2008 9:21 am
Posts: 2
Location: Courbevoie, France
Hi,

Did you find a solution for this problem ? That's exactly the problem I am facing right now.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 9:38 am 
Newbie

Joined: Wed Jan 23, 2008 10:02 am
Posts: 3
Quote:
Did you find a solution for this problem ?

Sorry, no solultion...
I exploited a dirty workaround:
1. first I extract the List<Identita> matching cliente properties:
Criteria criteriaDocs = session.createCriteria(StoricoDocumentoIdentitaConsultazione.class);
criteriaDocs.setProjection(Projections.property(StoricoDocumentoIdentitaConsultazione.PROP_ID_DOCUMENTO));
criteriaDocs.createCriteria(StoricoDocumentoIdentitaConsultazione.PROP_CLIENTE)
.add(Example.create(cli));
List docs = criteriaDocs.list();

2. I use a In clause over Ticket

if(docs==null || docs.isEmpty()){
return false;
}
// filter (in clause) by idDocumento property
criteria.add(Restrictions.in(BaseTicketConsultazione.PROP_DOC_IDENTITA
+ "." + StoricoDocumentoIdentitaConsultazione.PROP_ID_DOCUMENTO, docs));


Well: I feel is far away from Criteria apporach, but I guess you cannot do otherwise;
Hibernate maps Relations via FK/PK, so fields other by PK not available.

s.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 11:01 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Cool workaround...Thanks for the update!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Id attribute
PostPosted: Fri Jul 11, 2008 9:20 am 
Newbie

Joined: Thu Jul 10, 2008 9:21 am
Posts: 2
Location: Courbevoie, France
My problem was that I was trying to use an id attribute as query parameter.

The following code works :
Code:
    // Build the example criteria
    A a = new A();
    a.setName("A1");// generic attribute
   
    // Get the Hibernate Session
    Session session = (Session) getEntityManager().getDelegate();
   
    // Build the criteria
    Criteria aCriteria = session.createCriteria(A.class);
    Example aExample = Example.create(a);
    aCriteria.add(aExample);

    // Execute the query
    log.debug("Will execute the query...");
    List<A> result = aCriteria.list();
    log.debug("Found "+result.size()+" A entities");


But this one does not :
Code:
    // Build the example criteria
    A a = new A();
    a.setId(1);// id attribute
   
    // Get the Hibernate Session
    Session session = (Session) getEntityManager().getDelegate();
   
    // Build the criteria (root criteria)
    Criteria aCriteria = session.createCriteria(A.class);
    Example aExample = Example.create(a);
    aCriteria.add(aExample);

    // Execute the query
    log.debug("Will execute the query...");
    List<A> result = aCriteria.list(); // get a (1=1) where clause
    log.debug("Found "+result.size()+" A entities");


In case that may help you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.