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 :

Problème avec clefs étrangères


Sujet :

Hibernate Java

  1. #1
    Membre habitué

    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    639
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 639
    Points : 167
    Points
    167
    Par défaut Problème avec clefs étrangères
    Bonjour

    J'ai une base MySql avec deux tables :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Consultation :
    idConsultation (clef primaire)
    idPatient (clef étrangère sur 2ème table)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Patient :
    idPatient (clef primaire)
    nom
    Sous éclipse j'obtiens les classes suivantes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    public class Consultation implements java.io.Serializable {
    	private Integer id;
    	private Patient patient;
            (getters and setters)
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    public class Patient implements java.io.Serializable {
    	private Integer idpatient;
            private String nom;
    	private Set<Consultation> consultations = new HashSet<Consultation>(0);
           (getters and setters)
    }
    fichiers HBM :
    Consultation :
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <class name="db.Consultation" table="consultation">
            <id name="id" type="java.lang.Integer">
                <column name="id" />
                <generator class="increment" />
            </id>
            <many-to-one name="patient" class="db.Patient" fetch="select">
                <column name="idpatient" />
            </many-to-one>

    Patient:
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <class name="db.Patient" table="patient">
            <id name="idpatient" type="java.lang.Integer">
                <column name="idpatient" />
                <generator class="increment" />
            </id>
            <set name="consultations" inverse="true">
                <key>
                    <column name="idpatient" />
                </key>
                <one-to-many class="db.Consultation" />
            </set>

    Si je charge un patient j'arrive à obtenir ses consultation sans problème.
    Mais, inversement, en chargeant une consultation je voudrais obtenir les informations sur le patient correspondant. Mais mon objet patient est toujours égal à null. Est-ce possible néanmoins de faire ça avec Hibernate?

    Merci pour vos réponses, en espérant avoir été clair.

  2. #2
    En attente de confirmation mail
    Inscrit en
    Juin 2008
    Messages
    76
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 76
    Points : 58
    Points
    58
    Par défaut
    utilise lazy="false"

  3. #3
    Membre habitué

    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    639
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 639
    Points : 167
    Points
    167
    Par défaut
    Ca ne suffit pas à résoudre le problème.
    lazy="false" me permet d'avoir les consultations de mon patient, mais pas le patient rattaché à une consultation particulière.

  4. #4
    Membre confirmé

    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    298
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 298
    Points : 484
    Points
    484
    Par défaut
    Ils ressemblent à quoi tes getters et setters, tu gère bien les deux bout de ton association bidirectionnelle ?

  5. #5
    Membre habitué

    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    639
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 639
    Points : 167
    Points
    167
    Par défaut
    Je n'ai pas touché aux classes ou très peu, j'ai laissé le plugin hibernate de Eclipse faire le travail.
    Pour ce qui est de gérer les deux bout des associations bidirectionnelle, comme je n'ai rien fait de particulier je pense que je ne gère pas grand chose...

    Classe Consultation:
    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
    public class Consultation implements java.io.Serializable {
     
    	private Integer id;
    	private Prescription prescription;
    	private Patient patient;
    	private String commentaire;
    	private Date date;
    	private Date heureDebut;
    	private Integer idsymptome;
    	private Set<SymptomeConsultation> symptomeConsultations = new HashSet<SymptomeConsultation>(
    			0);
    	private DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
     
     
    	public String getStringDate()
    	{   
    	      return this.dateFormat.format(this.date);
    	}
    	public void setStringDate(String stringDate)
    	{   
    	      try {
    			this.date = this.dateFormat.parse(stringDate);
    		} catch (ParseException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	public Consultation() {
    	}
     
    	public Consultation(Prescription prescription, Patient patient,
    			String commentaire, Date date, Date heureDebut, Integer idsymptome,
    			Set<SymptomeConsultation> symptomeConsultations) {
    		this.prescription = prescription;
    		this.patient = patient;
    		this.commentaire = commentaire;
    		this.date = date;
    		this.heureDebut = heureDebut;
    		this.idsymptome = idsymptome;
    		this.symptomeConsultations = symptomeConsultations;
    	}
     
    	public Integer getId() {
    		return this.id;
    	}
     
    	public void setId(Integer id) {
    		this.id = id;
    	}
     
    	public Prescription getPrescription() {
    		return this.prescription;
    	}
     
    	public void setPrescription(Prescription prescription) {
    		this.prescription = prescription;
    	}
     
    	public Patient getPatient() {
    		return this.patient;
    	}
     
    	public void setPatient(Patient patient) {
    		this.patient = patient;
    	}
     
    	public String getCommentaire() {
    		return this.commentaire;
    	}
     
    	public void setCommentaire(String commentaire) {
    		this.commentaire = commentaire;
    	}
     
    	public Date getDate() {
    		return this.date;
    	}
     
    	public void setDate(Date date) {
    		this.date = date;
    	}
     
    	public Date getHeureDebut() {
    		return this.heureDebut;
    	}
     
    	public void setHeureDebut(Date heureDebut) {
    		this.heureDebut = heureDebut;
    	}
     
    	public Integer getIdsymptome() {
    		return this.idsymptome;
    	}
     
    	public void setIdsymptome(Integer idsymptome) {
    		this.idsymptome = idsymptome;
    	}
     
    	public Set<SymptomeConsultation> getSymptomeConsultations() {
    		return this.symptomeConsultations;
    	}
     
    	public void setSymptomeConsultations(
    			Set<SymptomeConsultation> symptomeConsultations) {
    		this.symptomeConsultations = symptomeConsultations;
    	}
     
    }
    Classe Patient:
    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
    public class Patient implements java.io.Serializable {
     
    	private Integer id;
    	private String nom;
    	private String prenom;
    	private Date dateNaissance;
    	private Set<Consultation> consultations = new HashSet<Consultation>(0);
     
    	private DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
     
    	public Date getDate()
    	{
    		return this.dateNaissance;
    	}
    	public void setDate(Date date)
    	{
    		this.dateNaissance = date;
    	}
    	public String getStringDate()
    	{   
    	      return this.dateFormat.format(this.dateNaissance);
    	}
    	public void setStringDate(String stringDate)
    	{   
    	      try {
    			this.dateNaissance = this.dateFormat.parse(stringDate);
    		} catch (ParseException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
     
     
    	public Patient() {
    	}
     
    	public Patient(String nom, String prenom, Date dateNaissance,
    			Set<Consultation> consultations) {
    		this.nom = nom;
    		this.prenom = prenom;
    		this.dateNaissance = dateNaissance;
    		this.consultations = consultations;
    	}
     
    	public Integer getId() {
    		return this.id;
    	}
     
    	public void setId(Integer id) {
    		this.id = id;
    	}
     
    	public String getNom() {
    		return this.nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	public String getPrenom() {
    		return this.prenom;
    	}
     
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
     
    	public Date getDateNaissance() {
    		return this.dateNaissance;
    	}
     
    	public void setDateNaissance(Date dateNaissance) {
    		this.dateNaissance = dateNaissance;
    	}
     
    	public Set<Consultation> getConsultations() {
    		return this.consultations;
    	}
     
    	public void setConsultations(Set<Consultation> consultations) {
    		this.consultations = consultations;
    	}
     
    }

  6. #6
    Membre confirmé

    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    298
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 298
    Points : 484
    Points
    484
    Par défaut
    Regarde tes getters, tes setters et tes constructeurs ( sans te soucier hibernate ) et tu verras qu'il est normal que ton patient soit null.

    Il faut que tu gère ta relation bidirectionnele. Tu as dit à hibernate que c'était le patient qui aurait la responsabilité de gérer l'association ( inverse=true du coté des consultations).
    Donc ton patient doit gérer cette association ( c'est pas une obligation mais ça va te simplifier le code )
    Ex dans ta classe patient

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    public void addConsultation(Consultation consultation) {
       if (consultation == null) {
           thrw new IllegalArgumentException("bla bla bla");
       }
       getConsultations.add(consultation)
       consultation.setPatient(this);
    }
    il faut aussi que tu modifie tes constructeurs ( appelle le addConsultation dans tes contsructeurs).

    Si ton getConsultations() ne renvoie pas une collection non modifiable, il faudrat éviter de faire dans ton code des trucs du style :
    patient.getConsultations().add(consultation);

Discussions similaires

  1. Problème de clef étrangère nullable
    Par teddyalbina dans le forum Entity Framework
    Réponses: 4
    Dernier message: 21/04/2010, 08h12
  2. [Win'Design] Problème de clef étrangére
    Par Invité dans le forum Autres
    Réponses: 2
    Dernier message: 02/05/2008, 17h03
  3. Réponses: 1
    Dernier message: 21/11/2007, 14h35
  4. Problème avec clé étrangère
    Par gothard dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 22/02/2007, 16h50
  5. [IB6] Problème de clef étrangère
    Par Neilos dans le forum InterBase
    Réponses: 8
    Dernier message: 28/03/2006, 19h40

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