IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

[hibernate] charger la jointure


Sujet :

Hibernate Java

  1. #1
    Membre habitué Avatar de agougeon
    Inscrit en
    Mai 2005
    Messages
    253
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 253
    Points : 147
    Points
    147
    Par défaut [hibernate] charger la jointure
    Bonjour,

    Voila, j'ai deux tables (collaborateur et client) qui sont rattaché par une porteuse (relation n-n).
    Dans mon code de chargeùent d'un collaborateur, je veux pouvoir charger le client si la collaborateur en possède 1 et sinon que client soit a null.
    La porteuse est un table mission qui possède des champs.
    Merci pour votre aide.

  2. #2
    Membre actif
    Inscrit en
    Mai 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Mai 2005
    Messages : 217
    Points : 220
    Points
    220
    Par défaut
    ouhlala

    Je voudrais etre sur d'avoir bien compris ...

    une porteuse, c'est une table associative (qui ds ton cas, possède des attributs)? (d'apres ce q je lis oui)

    Ta relation est uni ou bi-directionnel ?

    Pour moi (c'est comme ca q ca fonctionne chez moi), tu charges l'association en passant par l'accesseur de ton objet, apres c'est ds le code metier que tu dois gerer ca

    D'ailleurs tu peux nous montrer ton mapping stp ?

  3. #3
    Membre habitué Avatar de agougeon
    Inscrit en
    Mai 2005
    Messages
    253
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 253
    Points : 147
    Points
    147
    Par défaut
    Oui c une table d'association sauf que je l'utilise comme une relation 0-1
    En fait je dois recupérer uniquement le client en cours (par rapport à une date)
    Voici mon mapping de collaborateur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
     
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
     
    <hibernate-mapping package="fr.planning">
        <class
            name="Collaborateur"
            table="COLLABORATEUR"
        >
            <id
                name="id"
                type="integer"
                column="COL_ID"
            >
                <generator class="native"/>
            </id>
     
            <property
                name="nom"
                column="COL_NOM"
                type="string"
                not-null="false"
                length="30"
            />
            <property
                name="prenom"
                column="COL_PRENOM"
                type="string"
                not-null="false"
                length="50"
            />
            <property
                name="dateNaissance"
                column="COL_DATE_NAISSANCE"
                type="date"
                not-null="false"
                length="7"
            />
            <property
                name="numSecu"
                column="COL_NUM_SECU"
                type="string"
                not-null="false"
                length="15"
            />
            <property
                name="dateEntree"
                column="COL_DATE_ENTREE"
                type="date"
                not-null="false"
                length="7"
            />
            <property
                name="nbExp"
                column="COL_NB_EXP"
                type="integer"
                not-null="false"
                length="22"
            />
            <property
                name="adresse1"
                column="COL_ADRESSE1"
                type="string"
                not-null="false"
                length="250"
            />
            <property
                name="adresse2"
                column="COL_ADRESSE2"
                type="string"
                not-null="false"
                length="50"
            />
            <property
                name="codePostal"
                column="COL_CODE_POSTAL"
                type="string"
                not-null="false"
                length="6"
            />
            <property
                name="ville"
                column="COL_VILLE"
                type="string"
                not-null="false"
                length="100"
            />
            <property
                name="dateSortie"
                column="COL_DATE_SORTIE"
                type="date"
                not-null="false"
                length="7"
            />
            <property
                name="periodeEssai"
                column="COL_PERIODE_ESSAI"
                type="integer"
                not-null="false"
                length="22"
            />
            <property
                name="dateDispo"
                column="COL_DATE_DISPO"
                type="date"
                not-null="false"
                length="7"
            />
            <many-to-one
                name="situation"
                column="SIT_ID"
                class="Situation"
                not-null="false"
            >
            </many-to-one>
     
            <many-to-one
                name="societe"
                column="SOC_ID"
                class="Societe"
                not-null="false"
            >
            </many-to-one>
     
            <many-to-one
                name="fonction"
                column="RFN_ID"
                class="Fonction"
                not-null="false"
            >
            </many-to-one>
            <many-to-one
                name="marche"
                column="MCH_ID"
                class="Marche"
                not-null="false"
            >
            </many-to-one>
            <many-to-one
                name="statut"
                column="STT_ID"
                class="Statut"
                not-null="false"
            >
            </many-to-one>
     
            <set name="missions" inverse="true">
                <key column="COL_ID"/>
                <one-to-many class="Mission"/>
            </set>
     
     
            <set
                name="utilisateurs"
                table="RESERVATION"
                cascade="all"
            >
                <key column="COL_ID"/>
                <many-to-many column="UTL_ID" class="Utilisateur"/>
            </set>
     
     
        </class>    
    </hibernate-mapping>
    de Mission
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
     
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
     
    <hibernate-mapping package="fr.planning">
        <class
            name="Mission"
            table="MISSION"
        >
            <id
                name="id"
                type="integer"
                column="MSN_ID"
            >
                <generator class="native"/>
            </id>
     
            <property
                name="dateDebut"
                column="MSN_DATE_DEBUT"
                type="date"
                not-null="false"
                length="7"
            />
            <property
                name="dateFin"
                column="MSN_DATE_FIN"
                type="date"
                not-null="false"
                length="7"
            />
            <property
                name="duree"
                column="MSN_DUREE"
                type="java.lang.Float"
                not-null="false"
                length="126"
            />
     
            <property
                name="courant"
                column="MSN_COURANT"
                type="integer"
                not-null="false"
                length="22"
            />
     
            <many-to-one
                name="client"
                column="CLT_ID"
                class="Client"
                not-null="true"
            >
            </many-to-one>
            <many-to-one
                name="collaborateur"
                column="COL_ID"
                class="Collaborateur"
                not-null="true"
            >
            </many-to-one>
     
        </class>    
    </hibernate-mapping>
    et de client
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
     
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
     
    <hibernate-mapping package="fr.planning">
        <class
            name="Client"
            table="CLIENT"
        >
            <id
                name="id"
                type="integer"
                column="CLT_ID"
            >
                <generator class="native"/>
            </id>
     
            <property
                name="libelle"
                column="CLT_LIBELLE"
                type="string"
                not-null="false"
                length="50"
            />
            <property
                name="prospect"
                column="CLT_PROSPECT"
                type="integer"
                not-null="false"
                length="1"
            />
            <property
                name="adresse"
                column="CLT_ADRESSE"
                type="string"
                not-null="false"
                length="250"
            />
            <property
                name="ville"
                column="CLT_VILLE"
                type="string"
                not-null="false"
                length="100"
            />
            <property
                name="logo"
                column="CLT_LOGO"
                type="string"
                not-null="false"
                length="200"
            />
            <property
                name="codePostal"
                column="CLT_CODE_POSTAL"
                type="string"
                not-null="false"
                length="6"
            />
            <many-to-one
                name="secteur"
                column="SCT_ID"
                class="Secteur"
                not-null="false"
            >
            </many-to-one>
            <many-to-one
                name="utilisateur"
                column="UTL_ID"
                class="Utilisateur"
                not-null="false"
            >
            </many-to-one>
            <many-to-one
                name="groupe"
                column="GPE_ID"
                class="Groupe"
                not-null="false"
            >
            </many-to-one>
     
     
            <set name="missions" inverse="true">
                <key column="CLT_ID"/>
                <one-to-many class="Mission"/>
            </set>
     
        </class>    
    </hibernate-mapping>

  4. #4
    Membre habitué Avatar de agougeon
    Inscrit en
    Mai 2005
    Messages
    253
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 253
    Points : 147
    Points
    147
    Par défaut
    J'ai trouvé une solution mais très moche... si vous avez mieux, je suis prenneur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
        public static void main(String[] args){
     
            Session session = HibernateUtil.currentSession();
     
            int id = 3;
     
            Criteria crit = session.createCriteria(Collaborateur.class);
     
     
            Collaborateur collab = (Collaborateur)crit
                .add(Expression.eq("id",id))
                .setFetchMode("situation", FetchMode.JOIN)
                .setFetchMode("statut", FetchMode.JOIN)
                .setFetchMode("fonction", FetchMode.JOIN)
                .setFetchMode("societe", FetchMode.JOIN)
                .setFetchMode("marche", FetchMode.JOIN)
                .setFetchMode("utilisateur", FetchMode.JOIN)
                .setFetchMode("missions", FetchMode.JOIN)
                .createCriteria("missions").add(Expression.eq("courant",1))
                                           .setFetchMode("client",FetchMode.JOIN)
                .uniqueResult();
     
     
                //System.out.println(collab.getMissions().iterator().next().getClient().getLibelle());
     
     
            if(collab == null){
                collab = (Collaborateur)crit
                .add(Expression.eq("id",id))
                .setFetchMode("situation", FetchMode.JOIN)
                .setFetchMode("statut", FetchMode.JOIN)
                .setFetchMode("fonction", FetchMode.JOIN)
                .setFetchMode("societe", FetchMode.JOIN)
                .setFetchMode("marche", FetchMode.JOIN)
                .setFetchMode("utilisateur", FetchMode.JOIN)
                .uniqueResult();            
                System.out.println("Pas de mission");
            }
            else{
                System.out.println(collab.getMissions().iterator().next().getClient().getLibelle());
            }
     
     
     
        }

Discussions similaires

  1. API Criteria, charger une jointure, problème bizarre
    Par TiMiD dans le forum Hibernate
    Réponses: 2
    Dernier message: 17/10/2007, 17h06
  2. Hibernate : charger une collection dynamiquement
    Par Tail dans le forum Hibernate
    Réponses: 4
    Dernier message: 04/08/2006, 09h35
  3. [HIBERNATE] Table de jointure & <composite-element>
    Par miloo dans le forum Hibernate
    Réponses: 1
    Dernier message: 27/06/2006, 11h42
  4. [Hibernate] Jointures externes
    Par eXiaNazaire dans le forum Hibernate
    Réponses: 2
    Dernier message: 21/03/2006, 15h34
  5. Réponses: 13
    Dernier message: 14/09/2005, 16h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo