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>