-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping Avancé : Join et SubSelect pour une Many-to-One
PostPosted: Tue Sep 15, 2009 4:35 am 
Newbie

Joined: Tue Sep 15, 2009 3:42 am
Posts: 2
Bonjour,

Voila mon problème: J'ai un Usager (objet UsaUsager) qui possède plusieurs adresses postales (objet UsaAdresse). J'ai donc une table d'association UsaUsaadr entre ma table UsaUsager et ma table UsaAdresse. Un booléen usaadrPrinOn dans ma table UsaUsaadr permet de savoir quelle est l'adresse Principale. Ce que je souhaite faire, c'est récupérer la commune (BdntCommune) de l'adresse principale de l'usager directement dans mon mapping de l'objet UsaUsager. L'objet BdntCommune est associé a l'objet UsaAdresse par une relation many-to-one.

J'essaye de suivre le pattern des " mappings plus complexes" du chapitre 7.6:
http://docs.jboss.org/hibernate/stable/ ... oc-complex

In a more complex example, imagine that the association between Employee and Organization is maintained in an Employment table full of historical employment data. An association to the employee's most recent employer, the one with the most recent startDate, could be mapped in the following way:

<join>
<key column="employeeId"/>
<subselect>
select employeeId, orgId
from Employments
group by orgId
having startDate = max(startDate)
</subselect>
<many-to-one name="mostRecentEmployer"
class="Organization"
column="orgId"/>
</join>

This functionality allows a degree of creativity and flexibility, but it is more practical to handle these kinds of cases using HQL or a criteria query.

Je suis en effet dans le même cas de figure.

J'ai donc mappé une association entre mon usager et la commune de cette façon:

<join table="USA_USAADR" optional="true" fetch="join">
<key column="USA_USAADR_CDN" />
<subselect>
(select usaAdresse.bdntCommune from usaUsaadrs where
usaUsaadrs.usaadrPrinOn = 1)
</subselect>
<many-to-one name="bdntCommune" class="niobe.btf.business.BdntCommune"
insert="false" update="false">
<column name="ID_COMMUNE" />
</many-to-one>
</join>

J'obtiens une erreur de mapping suivante:

GRAVE: Error parsing XML: XML InputStream(107) The content of element type "join" must match "(subselect?,comment?,key,(property|many-to-one|component|dynamic-component|any)*,sql-insert?,sql-update?,sql-delete?)".
15 sept. 2009 09:37:06 fr.gouv.agriculture.orion.util.Logs logFatal
GRAVE: Erreur d'initialisation de contexte.
fr.gouv.agriculture.o2.kernel.KernelException: org.hibernate.InvalidMappingException: Could not parse mapping document from resource niobe/buf/business/UsaUsager.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1443)


Est ce que quelqu'un a déjà utilisé ce pattern là et pourrait m'aider a le mettre en œuvre?

cordialement,
Jérôme


Top
 Profile  
 
 Post subject: Re: Mapping Avancé : Join et SubSelect pour une Many-to-One
PostPosted: Tue Sep 15, 2009 6:43 am 
Newbie

Joined: Tue Sep 15, 2009 3:42 am
Posts: 2
En fait j'ai trouvé. Il fallait utiliser la syntaxe suivante dans le mapping:

<many-to-one name="bdntCommune" class="niobe.btf.business.BdntCommune"
insert="false" update="false">
<formula>(SELECT adresse.ID_COMMUNE FROM NIOBE.dbo.USA_USAADR as
usadress, NIOBE.dbo.USA_USAGER as usager, NIOBE.dbo.USA_ADRESSE as adresse where
usager.USA_CDN=usadress.USA_CDN and usadress.USAADR_PRIN_ON=1 and
usadress.ADR_CDN=adresse.ADR_CDN and usager.USA_CDN=USA_CDN)</formula>
</many-to-one>

La formula est en SQL est effectue la jointure entre les différentes tables pour récupérer la commune principale de l'usager.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.