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/JPA: ManyToMany, mise à jour dans une table de jointure.


Sujet :

Hibernate Java

  1. #1
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut Hibernate/JPA: ManyToMany, mise à jour dans une table de jointure.
    Bonjour, j'ai un problème.

    J'ai deux tables (contributor et service) mappé par une table de jointure (contributor_service).

    J'ai donc deux beans annotés:
    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
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
     
    package com.rdv.object.db;
     
    import java.io.Serializable;
    import java.util.Set;
     
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.OrderBy;
    import javax.persistence.Table;
     
     
    /**
     * Class for the table contributor.
     * @author Phil.
     *
     */
    @Entity
    @Table(name="contributor")
    public class Contributor implements Serializable{
     
    	private static final long serialVersionUID = 1L;
     
    	/**
             * The id.
             * <br>Generated value.
             * <br>name="id_contributor",length=3.
             */
    	@Id
    	@GeneratedValue
    	@Column(name="id_contributor",length=3)
    	private Integer idContributor;
     
    	/**
             * First name.
             * <br>name="first_name",nullable=false,length=64
             */
    	@Column(name="first_name",nullable=false,length=64)
    	private String firstName;
     
    	/**
             * Last name.
             * <br>name="last_name",nullable=false,length=64
             */
    	@Column(name="last_name",nullable=false,length=64)
    	private String lastName;
     
    	/**
             *The photo of the contributor (the path).
             *<br> name="photo",nullable=false,length=255,unique=true.
             */
    	@Column(name="photo",nullable=true,length=255,unique=true)
    	private String photo;
     
    	/**
             * The appointments.
             * <br>Fetch:Lazy,Mapped by contributor.
             */
    	@OneToMany(fetch=FetchType.LAZY,mappedBy="contributor")
    	@OrderBy("date ASC")
    	private Set<Appointment> appointments;
     
    	/**
             * The services.
             * <br>Fetch:Eager, mapped by contributors.
             */
    	@ManyToMany(fetch=FetchType.EAGER,mappedBy="contributors")
    	@OrderBy("name")
    	private Set<Service> services;
     
    	/**
             * The holidays.
             * <br>fetch:FetchType.EAGER, mapped by contributor.
             */
    	@OneToMany(mappedBy="contributor",fetch=FetchType.EAGER)
    	@OrderBy("date")
    	private Set<Holiday> holidays;
     
    	/**
             * The works.
             * <br>fetch:eager.
             *<br>Mapped by contributor.
             */
    	@OneToMany(fetch=FetchType.EAGER,mappedBy="contributor")
    	@OrderBy("timeFrom ASC")
    	private Set<Work> works;
     
    	/**
             * The provider.
             * <br>Fetch:lazy.
             * Join column:id_provider.
             * <br>Nullable=false.
             */
    	@ManyToOne(fetch=FetchType.LAZY)
    	@JoinColumn(name="id_provider",nullable=false)
    	private Provider provider;
     
    	//getter setter
    	/**
             * Get the id.
             * @return The ud.
             */
    	public Integer getIdContributor() {
    		return idContributor;
    	}
     
    	/**
             * Set the id.
             * @param idContributor The id.
             */
    	public void setIdContributor(Integer idContributor) {
    		this.idContributor = idContributor;
    	}
     
    	/**
             * Get the first name.
             * @return The first name.
             */
    	public String getFirstName() {
    		return firstName;
    	}
     
    	/**
             * Set the first name. 
             * @param firstName The first name.
             */
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
     
    	/**
             * Get the last name.
             * @return The last name.
             */
    	public String getLastName() {
    		return lastName;
    	}
     
    	/**
             * set the last name.
             * @param lastName The last name.
             */
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
     
    	/**
             * Get the photo.
             * @return The photo.
             */
    	public String getPhoto() {
    		return photo;
    	}
     
    	/**
             * set the photo.
             * @param photo The photo.
             */
    	public void setPhoto(String photo) {
    		this.photo = photo;
    	}
     
    	/**
             * Get the appointments.
             * @return The appointments.
             */
    	public Set<Appointment> getAppointments() {
    		return appointments;
    	}
     
    	/**
             * Set the appointments.
             * @param appointments The appointments.
             */
    	public void setAppointments(Set<Appointment> appointments) {
    		this.appointments = appointments;
    	}
     
    	/**
             * Get the services.
             * @return The services.
             */
    	public Set<Service> getServices() {
    		return services;
    	}
     
    	/**
             * Set the services.
             * @param services The services.
             */
    	public void setServices(Set<Service> services) {
    		this.services = services;
    	}
     
    	/**
             * Get the holidays.
             * @return The holidays.
             */
    	public Set<Holiday> getHolidays() {
    		return holidays;
    	}
     
    	/**
             * Set the holidays.
             * @param holidays The holidays.
             */
    	public void setHolidays(Set<Holiday> holidays) {
    		this.holidays = holidays;
    	}
     
    	/**
             * Get list of works.
             * @return List of works.
             */
    	public Set<Work> getWorks() {
    		return works;
    	}
     
    	/**
             * Set list of works.
             * @param works List of works.
             */
    	public void setWorks(Set<Work> works) {
    		this.works = works;
    	}
     
    	public Provider getProvider() {
    		return provider;
    	}
     
    	public void setProvider(Provider provider) {
    		this.provider = provider;
    	}
    }
    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
    167
    168
    169
    170
     
    package com.rdv.object.db;
     
    import java.io.Serializable;
    import java.util.Set;
     
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.OrderBy;
    import javax.persistence.Table;
     
    /**
     * Class for table service.
     * @author Phil
     *
     */
     
    @Entity
    @Table(name="service")
    public class Service implements Serializable{
     
    	private static final long serialVersionUID = 1L;
     
    	/**
             * The id.
             * <br> Generated value.
             * <br> name="id_service",length=3.
             */
    	@Id
    	@GeneratedValue
    	@Column(name="id_service",length=3)
    	private Integer idService;
     
    	/**
             * The name.
             * <br>name="name",length=255,nullable=false.
             */
    	@Column(name="name",length=255,nullable=false)
    	private String name;
     
    	/**
             * The duration.
             * <br>name="duration",length=1,nullable=false.
             */
    	@Column(name="duration",length=1,nullable=false)
    	private Integer duration;
     
    	/**
             * The contributors.
             * <br>Fetch:Lazy.
             * <br>Join table:contributor_service.
             */
    	@ManyToMany(fetch=FetchType.LAZY)
    	@JoinTable(name="contributor_service",joinColumns=@JoinColumn(name="id_service"),inverseJoinColumns=@JoinColumn(name="id_contributor"))
    	@OrderBy("lastName")
    	private Set<Contributor> contributors;
     
    	/**
             * The appointments.
             */
    	@OneToMany(fetch=FetchType.LAZY,mappedBy="service")
    	@OrderBy("date")
    	private Set<Appointment> appointments;
     
    	/**
             * The provider.
             * Join column:id_provider.
             */
    	@ManyToOne
    	@JoinColumn(name="id_provider")
    	private Provider provider;
     
    	//getter setter
     
    	/**
             * Get the id.
             * @return The id.
             */
    	public Integer getIdService() {
    		return idService;
    	}
     
    	/**
             * Set the id.
             * @param idService The id.
             */
    	public void setIdService(Integer idService) {
    		this.idService = idService;
    	}
     
    	/**
             * Get the name.
             * @return The name.
             */
    	public String getName() {
    		return name;
    	}
     
    	/**
             * Set the name.
             * @param name The name.
             */
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	/**
             * Get the duration
             * @return The duration.
             */
    	public Integer getDuration() {
    		return duration;
    	}
     
    	/**
             * Set the duration.
             * @param duration The duration.
             */
    	public void setDuration(Integer duration) {
    		this.duration = duration;
    	}
     
    	/**
             * Get the contributors.
             * @return The contributors.
             */
    	public Set<Contributor> getContributors() {
    		return contributors;
    	}
     
    	/**
             * Set the contributors.
             * @param contributors The contributors.
             */
    	public void setContributors(Set<Contributor> contributors) {
    		this.contributors = contributors;
    	}
     
    	/**
             * Get the appointments.
             * @return The appointment.
             */
    	public Set<Appointment> getAppointments() {
    		return appointments;
    	}
     
    	/**
             * set the appointments.
             * @param appointments The appointments.
             */
    	public void setAppointments(Set<Appointment> appointments) {
    		this.appointments = appointments;
    	}
     
    	public Provider getProvider() {
    		return provider;
    	}
     
    	public void setProvider(Provider provider) {
    		this.provider = provider;
    	}
    }

    Pour les mise à jour, j'ai :
    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
     
    public void updateContributor(Contributor contributor)
    			throws TechnicalExceptions {
    		// TODO Auto-generated method stub
    		Session session = null;
     
    		try{
    			session = SessionFactoryUtil.getInstance().getCurrentSession();
    			Transaction tc = session.beginTransaction();
    			try{
     
    				session.update(contributor);
     
    				tc.commit();
    			}
    			catch(HibernateException hibernateException){
    				tc.rollback();
    				throw new TechnicalExceptions(hibernateException);
    			}
    		}
    		catch(HibernateException hibernateException){
    			throw new TechnicalExceptions(hibernateException);
    		}
    		finally{
    			if(session != null){
    				if(session.isOpen()){
    					session.close();
    				}
    			}
    		}
    	}
    Voilà mon problème:
    J'arrive à mettre à jour des champs (first_name, last_name ...) mais dès que je réduis (ou augmente) la taille du Set services, la mise à jour n'est pas faite dans la base de donnée (alors que pour un champ, elle est faite).

    Il ne veut pas toucher les données dans la table de jointure (contributor_service).

    Je sais que le problème est souvent rencontré.

    Si quelqu'un a un avis....

    Merci d'avance.

  2. #2
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut
    J'ai trouvé, et c'était très con. Le propriétaire de la relation était le bean Service. On change de propriétaire et ça marche.

    Au final, on a pour Contributor
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    @ManyToMany(fetch=FetchType.EAGER)
    	@JoinTable(name="contributor_service",joinColumns=@JoinColumn(name="id_contributor"),inverseJoinColumns=@JoinColumn(name="id_service"))
    	@OrderBy("name")
    	private Set<Service> services;
    Et on a pour Service:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    @ManyToMany(fetch=FetchType.LAZY,mappedBy="services")
    	@OrderBy("lastName")
    	private Set<Contributor> contributors;

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

Discussions similaires

  1. [AC-2003] Mise à jour dans une table
    Par yfchauer dans le forum Requêtes et SQL.
    Réponses: 1
    Dernier message: 02/04/2012, 18h39
  2. Mise à jour dans une table de l'AS400
    Par Elise02 dans le forum Développement de jobs
    Réponses: 2
    Dernier message: 12/03/2009, 08h38
  3. mise à jour dans une table
    Par christeldum dans le forum Requêtes et SQL.
    Réponses: 2
    Dernier message: 20/02/2009, 09h16
  4. Réponses: 2
    Dernier message: 14/05/2007, 10h45
  5. Mise à jour dans une table
    Par manucha dans le forum Oracle
    Réponses: 4
    Dernier message: 01/03/2007, 11h11

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