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

JPA Java Discussion :

souci de relation


Sujet :

JPA Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 226
    Points : 72
    Points
    72
    Par défaut souci de relation
    bonjour,
    j'essaye de mettre en relation 2 entity avec jpa le souci est que le champs keyClient dans adress est toujours null

    merci de votre aide .

    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
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package fr.ietevents.serviceweb.Entity;
     
    import java.io.Serializable;
    import java.math.BigDecimal;
    import java.util.Collection;
    import javax.persistence.Basic;
    import javax.persistence.CascadeType;
    import javax.persistence.CollectionTable;
    import javax.persistence.Column;
    import javax.persistence.ElementCollection;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.validation.constraints.Size;
    import javax.xml.bind.annotation.XmlRootElement;
     
    /**
     *
     * @author IETEVENTS
     */
    @Entity
    @Table(name = "Client")
    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"),
        @NamedQuery(name = "Client.findById", query = "SELECT c FROM Client c WHERE c.KeyClient = :KeyClient"),
        @NamedQuery(name = "Client.findByNom", query = "SELECT c FROM Client c WHERE c.nom = :nom"),
        @NamedQuery(name = "Client.findByPrenom", query = "SELECT c FROM Client c WHERE c.prenom = :prenom")})
    public class Client implements Serializable {
     
        private static final long serialVersionUID = 1L;
        // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "KeyClient")
        private BigDecimal KeyClient;
        @Size(max = 255)
        @Column(name = "nom")
        private String nom;
        @Size(max = 255)
        @Column(name = "prenom")
        private String prenom;
        @OneToMany(cascade = CascadeType.ALL,mappedBy = "client")
        private Collection<Adresse> adresse;
     
        public Client() {
        }
     
        public Client(BigDecimal id) {
            this.KeyClient = id;
        }
     
        public Collection<Adresse> getAdresse() {
            return adresse;
        }
     
        public void setAdresse(Collection<Adresse> adresse) {
            this.adresse = adresse;
        }
     
     
     
        public BigDecimal getId() {
            return KeyClient;
        }
     
        public void setId(BigDecimal id) {
            this.KeyClient = id;
        }
     
        public String getNom() {
            return nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public String getPrenom() {
            return prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (KeyClient != null ? KeyClient.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Client)) {
                return false;
            }
            Client other = (Client) object;
            if ((this.KeyClient == null && other.KeyClient != null) || (this.KeyClient != null && !this.KeyClient.equals(other.KeyClient))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "fr.ietevents.serviceweb.Entity.Client[ id=" + KeyClient + " ]";
        }
     
    }
    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
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package fr.ietevents.serviceweb.Entity;
     
    import java.io.Serializable;
    import javax.persistence.Basic;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;
    import javax.xml.bind.annotation.XmlRootElement;
     
    /**
     *
     * @author IETEVENTS
     */
    @Entity
    @Table(name = "Adresse")
    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "Adresse.findAll", query = "SELECT a FROM Adresse a"),
        @NamedQuery(name = "Adresse.findByKeyAdresse", query = "SELECT a FROM Adresse a WHERE a.keyAdresse = :keyAdresse"),
        @NamedQuery(name = "Adresse.findByAdresse", query = "SELECT a FROM Adresse a WHERE a.adresse = :adresse"),
        @NamedQuery(name = "Adresse.findByKeyLocalite", query = "SELECT a FROM Adresse a WHERE a.keyLocalite = :keyLocalite")})
    public class Adresse implements Serializable {
     
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "KeyAdresse")
        private Integer keyAdresse;
        @Size(max = 90)
        @Column(name = "Adresse")
        private String adresse;
        @Column(name = "KeyLocalite")
        private Integer keyLocalite;
        @ManyToOne()
        @JoinColumn(name = "KeyClient")
        private Client client ;
     
     
     
        public Adresse() {
        }
     
        public Adresse(Integer keyAdresse) {
            this.keyAdresse = keyAdresse;
        }
     
        public Client getClient() {
            return client;
        }
     
        public void setClient(Client client) {
            this.client = client;
        }
     
        public Integer getKeyAdresse() {
            return keyAdresse;
        }
     
        public void setKeyAdresse(Integer keyAdresse) {
            this.keyAdresse = keyAdresse;
        }
     
        public String getAdresse() {
            return adresse;
        }
     
        public void setAdresse(String adresse) {
            this.adresse = adresse;
        }
     
        public Integer getKeyLocalite() {
            return keyLocalite;
        }
     
        public void setKeyLocalite(Integer keyLocalite) {
            this.keyLocalite = keyLocalite;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (keyAdresse != null ? keyAdresse.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Adresse)) {
                return false;
            }
            Adresse other = (Adresse) object;
            if ((this.keyAdresse == null && other.keyAdresse != null) || (this.keyAdresse != null && !this.keyAdresse.equals(other.keyAdresse))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "fr.ietevents.serviceweb.Entity.Adresse[ keyAdresse=" + keyAdresse + " ]";
        }
     
    }

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 226
    Points : 72
    Points
    72
    Par défaut
    bonjour,
    j'ai essayer un code plus simple mais j'ai toujours le souci qu'il ne sauvegarde pas dans adress la reference vers client :
    pouvez vous m'aider ?

    Merci d'avance

    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
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package jpa01.Persistance;
     
    import java.io.Serializable;
    import java.util.List;
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
     
    /**
     *
     * @author IETEVENTS
     */
    @Entity
    public class Client implements Serializable {
     
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        String nom ;
        String prenom;
        @OneToMany(mappedBy = "client",cascade = CascadeType.ALL)
        private  List<AdresseClient> adress;
     
        public String getNom() {
            return nom;
        }
     
        public List<AdresseClient> getAdress() {
            return adress;
        }
     
        public void setAdress(List<AdresseClient> adress) {
            this.adress = adress;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public String getPrenom() {
            return prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Client)) {
                return false;
            }
            Client other = (Client) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "jpa01.Persistance.Client[ id=" + id + " ]";
        }
     
    }

    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
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package jpa01.Persistance;
     
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
     
    /**
     *
     * @author IETEVENTS
     */
    @Entity
    public class AdresseClient implements Serializable {
     
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @Column(name ="adress")
        private String adress;
        @ManyToOne()
        @JoinColumn(name="Client")
        private Client client ;
     
        public Long getId() {
            return id;
        }
     
        public String getAdress() {
            return adress;
        }
     
        public void setAdress(String adress) {
            this.adress = adress;
        }
     
        public Client getClient() {
            return client;
        }
     
        public void setClient(Client client) {
            this.client = client;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof AdresseClient)) {
                return false;
            }
            AdresseClient other = (AdresseClient) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "jpa01.Persistance.AdresseClient[ id=" + id + " ]";
        }
     
    }
    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
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package jpa01;
     
    import java.util.ArrayList;
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.EntityTransaction;
    import javax.persistence.Persistence;
    import jpa01.Persistance.AdresseClient;
    import jpa01.Persistance.Client;
     
    /**
     *
     * @author IETEVENTS
     */
    public class Jpa01 {
     
        /**
         * @param args the command line arguments
         */
       public static void main(String[] args)
        {
          //--- Création d'un EntityManageter
          System.out.println( "Création d'un EntityManager");
          EntityManagerFactory emf =
     				Persistence.createEntityManagerFactory( "Jpa01PU");
          EntityManager em = emf.createEntityManager();
     
          //--- création d'un nouveau client
          System.out.println( "Création d'un client");
          Client client = new Client();
          client.setNom( "Dupont" );
          client.setPrenom( "Marcel" );
          System.out.println( "Création d'une adress");
            AdresseClient adresseClient = new AdresseClient();
            adresseClient.setAdress("paris");
            List listAdress = new ArrayList();
            listAdress.add(adresseClient);
            client.setAdress(listAdress);
          //--- persistons ce client dans la base
          EntityTransaction tx = em.getTransaction();
          System.out.println( "Début de la transaction");
          tx.begin();
          try
          {
            System.out.println( "Ajout du client dans la base en cours..." );
            em.persist( client );
            tx.commit();
            System.out.println( "Transaction validée");
          }
          catch ( Exception e )
          {
            System.out.println( "Erreur :" + e.getMessage() );
            tx.rollback();
            System.out.println( "Transaction annulée");
          }
          finally
          {
            em.close();
            emf.close();
          }
        }
     
    }

  3. #3
    Membre actif
    Homme Profil pro
    Développeur Java/JavaEE
    Inscrit en
    Août 2014
    Messages
    194
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java/JavaEE

    Informations forums :
    Inscription : Août 2014
    Messages : 194
    Points : 290
    Points
    290
    Par défaut
    Bonjour ,

    Essai plutôt de faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
          Client client = new Client();
          client.setNom( "Dupont" );
          client.setPrenom( "Marcel" );
     
           AdresseClient adresseClient = new AdresseClient();
           adresseClient.setAdress("paris");
           //La en effecte le client à l'adresse mais pas le contraire pour que ton adresse obtienne un client ;)
            adresseClient.setClient(client);
           List listAdress = new ArrayList();
           listAdress.add(adresseClient);
           client.setAdress(listAdress);

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 226
    Points : 72
    Points
    72
    Par défaut
    un grand Merci depuis le temps que je cherchais mon erreur

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Soucis relation tables + controller/view
    Par Curator dans le forum ASP.NET MVC
    Réponses: 5
    Dernier message: 15/03/2013, 22h38
  2. Soucis avec la conception tables/relations
    Par Gdal dans le forum Schéma
    Réponses: 5
    Dernier message: 28/01/2008, 22h30
  3. souci sur une relation
    Par Eh_manu dans le forum Access
    Réponses: 22
    Dernier message: 05/06/2006, 10h06
  4. Réponses: 4
    Dernier message: 16/02/2003, 12h16
  5. Réponses: 2
    Dernier message: 03/10/2002, 17h24

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