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 :

JPA ManyToMany ajouter un champ


Sujet :

JPA Java

  1. #1
    Membre à l'essai
    Inscrit en
    avril 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : avril 2010
    Messages : 12
    Points : 12
    Points
    12
    Par défaut JPA ManyToMany ajouter un champ
    Bonjour.
    J'ai deux modèles (Paiement et Echeance) liés par une relation ManyToMany, c'est à dire qu'un paiement peut concerner zéro ou plusieurs échéances et une échéance peut être concernée par zéro ou plusieurs paiements.

    Code java

    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
    package com.sil.gpc.domains;
     
    import java.sql.Date;
     
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.ForeignKey;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.PrimaryKeyJoinColumn;
     
    @Entity(name = "Paiement")
    public class Paiement{
    		@Id
     
    		@Column(name = "numPaiement", length = 20)
    		@PrimaryKeyJoinColumn(name = "numPaiement" )
    		private String numPaiement;
    		private Date datePaiement;
    		@Column(length = 50)
    		private String contribuable;
    		private boolean validePaiement;
    		@Column(length = 150)
    		private String Observation;
    		private Date dateSaisie;
     
    		//Liaison avec la table Echeance
    		@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE, targetEntity = Echeance.class)
    		@JoinTable(name = "PaiementEcheance", joinColumns = {@JoinColumn(name="numPaiement")},
    		inverseJoinColumns = {@JoinColumn(name="idEcheance")})
    		private List<Echeance> echeancesPaiement;
     
    		public Paiement() {
    			super();
    			// TODO Auto-generated constructor stub
    		}
     
     
    		public String getNumPaiement() {
    			return numPaiement;
    		}
     
    		public void setNumPaiement(String numPaiement) {
    			this.numPaiement= numPaiement;
    		}
     
    		public Date getDatePaiement() {
    			return datePaiement;
    		}
     
    		public void setDatePaiement(Date datePaiement) {
    			this.datePaiement= datePaiement;
    		}
     
    		public String getContribuable() {
    			return contribuable;
    		}
     
    		public void setContribuable(String contribuable) {
    			this.contribuable = contribuable;
    		}
     
    		public boolean isValidePaiement() {
    			return validePaiement;
    		}
     
    		public void setValidePaiement(boolean validePaiement) {
    			this.validePaiement= validePaiement;
    		}
     
    		public String getObservation() {
    			return Observation;
    		}
     
    		public void setObservation(String observation) {
    			Observation = observation;
    		}
     
    		public Date getDateSaisie() {
    			return dateSaisie;
    		}
     
    		public void setDateSaisie(Date dateSaisie) {
    			this.dateSaisie = dateSaisie;
    		}
    }
    code java

    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
     
    package com.sil.gpc.domains;
     
    import java.io.Serializable;
    import java.sql.Date;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.persistence.CascadeType;
    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.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
     
    @SuppressWarnings("serial")
    @Entity
    public class Echeance implements Serializable {
     
    	@Id
    	@GeneratedValue(strategy = GenerationType.AUTO)
    	private Long idEcheance;
    	private String mois;
    	private Date dateEcheance;
     
    	//Liaison avec la table paiement
    	@ManyToMany(targetEntity = OpCaisse.class,fetch = FetchType.EAGER,cascade = CascadeType.REFRESH)
    	@JoinTable(name = "PaiementEcheance",
    	joinColumns = {@JoinColumn(name="idEcheance")},
    	inverseJoinColumns = {@JoinColumn(name="numPaiement")})
    	private List<Paiement> paiementsEcheance ;
     
    	public Echeance() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
     
    	public Echeance(String mois, Date dateEcheance, Contrat contrat) {
    		super();
    		this.mois = mois;
    		this.dateEcheance = dateEcheance;
    		this.payeEcheance = payeEcheance;
    		this.contrat = contrat;
    	}
     
    	public Long getIdEcheance() {
    		return idEcheance;
    	}
     
    	public void setIdEcheance(Long idEcheance) {
    		this.idEcheance = idEcheance;
    	}
     
    	public String getMois() {
    		return mois;
    	}
     
    	public void setMois(String mois) {
    		this.mois = mois;
    	}
     
    	public Date getDateEcheance() {
    		return dateEcheance;
    	}
     
    	public void setDateEcheance(Date dateEcheance) {
    		this.dateEcheance = dateEcheance;
    	}
    }
    Actuellement, lorsque j'exécute mon projet, les tables Paiement et Echeance sont créées avec la table association PaiementEcheance.
    Mon problème est que je veux ajouter un champ Montant de type double dans la table PaiementEcheance. Comment dois-je procéder, s'il vous plait?

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    septembre 2006
    Messages
    2 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : septembre 2006
    Messages : 2 875
    Points : 4 216
    Points
    4 216
    Par défaut
    https://stackoverflow.com/questions/...olumn#32920550
    https://vladmihalcea.com/the-best-wa...and-hibernate/

    une autre technique est aussi de faire le @ManyToMany sur une Map dont la clé est l’entité intersection.

    (dans tous les scénarios il faut que l’intersection soit une @Entity)

Discussions similaires

  1. Réponses: 3
    Dernier message: 30/09/2011, 11h01
  2. [debutant] Ajouter un champ à une table.
    Par castaka dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 03/05/2005, 10h08
  3. Ajouter un champs dans un Report
    Par nora_ora dans le forum Reports
    Réponses: 7
    Dernier message: 09/12/2004, 17h24
  4. Ajouter un champs dans une table (Access 2000)
    Par Didier100 dans le forum Bases de données
    Réponses: 2
    Dernier message: 12/10/2004, 13h02
  5. ajouter un champ dynamiquement à une instance de table
    Par maniack dans le forum Bases de données
    Réponses: 2
    Dernier message: 28/02/2004, 23h58

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