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 :

Table d'association : update ne la rempli pas


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    40
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 40
    Par défaut Table d'association : update ne la rempli pas
    Bonjour,

    J'ai ces 3 tables là :
    FONCTION
    id_fonction integer not null,
    lib varchar,
    primary key id_fonction;

    ROLE
    id_role integer not null,
    lib varchar,
    primary key id_role;

    FONCTION_ROLE
    id_fonction_role integer not null,
    id_fonction integer not null,
    id_role integer not null,
    primary key (id_fonction_role);

    Je les ai mappé en Hibernate 3.2 ainsi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <hibernate-mapping package="org.myorg.vo" default-lazy="false">
    	<class name="FonctionVO"
    		table="FONCTION" dynamic-update="true" lazy="false">
    		<id name="id" column="FONCTION_ID">
    			<generator class="increment" />
    		</id>
    		<property name="lib" column="FONCTION_LIB" />
     
    		<set name="roles" table="FONCTION_ROLE" inverse="true" lazy="false" >
    		    <key column="FONCTION_ID" update="true"/>
    		    <many-to-many column="ROLE_ID" class="RoleVO" />
    		</set>
    	</class>
    </hibernate-mapping>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <hibernate-mapping package="org.myorg.vo" default-lazy="false">
    	<class name="RoleVO"
    		table="ROLE" dynamic-update="true" lazy="false">
    		<id name="id" column="ROLE_ID">
    			<generator class="increment" />
    		</id>
    		<property name="lib" column="ROLE_LIB" />
     
    		<set name="fonctions" table="FONCTION_ROLE" inverse="true" lazy="false">
    		    <key column="ROLE_ID" update="true"/>
    		    <many-to-many column="FONCTION_ID" class="FonctionVO"/>
    		</set>
    	</class>
    </hibernate-mapping>
    Mes classes 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
    public class FonctionVO {
    	private long id;
    	private String lib;
    	private Set<RoleVO> roles = new HashSet<RoleVO>();
     
    	public long getId() {
    		return id;
    	}
    	public void setId(long id) {
    		this.id = id;
    	}
    	public String getLib() {
    		return lib;
    	}
    	public void setLib(String lib) {
    		this.lib = lib;
    	}
    	public Set<RoleVO> getRoles() {
    		return roles;
    	}
    	public void setRoles(Set<RoleVO> roles) {
    		this.roles = roles;
    	}
    	public void addRole(RoleVO role) {
    	    role.getFonctions().add(this);
    	    this.getRoles().add(role);
    	}
     
    }
    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
    public class RoleVO {
    	private long id;
    	private String lib;
    	private Set<FonctionVO> fonctions = new HashSet<FonctionVO>();
     
    	public long getId() {
    		return id;
    	}
    	public void setId(long id) {
    		this.id = id;
    	}
    	public String getLib() {
    		return lib;
    	}
    	public void setLib(String lib) {
    		this.lib = lib;
    	}
    	public Set<FonctionVO> getFonctions() {
    		return fonctions;
    	}
    	public void setFonctions(Set<FonctionVO> fonctions) {
    		this.fonctions = fonctions;
    	}
    }
    Déjà, est ce que cela vous parait optimisé ? Je veux pouvoir lister les roles d'une fonction, et les fonctions ayant tel ou tel role. Et je veux pouvoir ajouter des roles à une fonction.

    Pour ajouter des role sà une fonction, je fais ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    FonctionVO fctOld = fonctionService.getById(idFonction);
     
        	    	if(rolesSelected != null) {
        	    		for (Iterator iter = rolesSelected.iterator(); iter.hasNext();) {
    						String checkboxId = (String) iter.next();
     
    						RoleVO roleTmp = roleService.getById(new Long(checkboxId));
    						fctOld.addRole(roleTmp);
    					}
        	    	}
     
        			fonctionService.update(fctOld);
    Sauf que Hibernate ne me sauvegarde que la fonction, il ne m'insère pas les role_id ajoutés dans la table FONCTION_ROLE.

    Pourquoi donc ?

    Merci pour vos lumières.

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    40
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 40
    Par défaut
    Ok j'ai trouvé ma 1ere erreur. J'avais mis 2 inverse="true".

    J'ai modifié le fonction.hmb.xml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <hibernate-mapping package="org.myorg.vo" default-lazy="false">
    	<class name="FonctionVO"
    		table="FONCTION" dynamic-update="true" lazy="false">
    		<id name="id" column="FONCTION_ID">
    			<generator class="increment" />
    		</id>
    		<property name="lib" column="FONCTION_LIB" />
     
    		<set name="roles" table="FONCTION_ROLE" lazy="false" >
    		    <key column="FONCTION_ID" update="true"/>
    		    <many-to-many column="ROLE_ID" class="RoleVO" />
    		</set>
    	</class>
    </hibernate-mapping>
    And now I have the following error :

    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
    Hibernate: update SCHEMACRM.FONCTION set FONCTION_LIB=?, CREATOR_ID=?, CREATION_DATE=?, LAST_EDITOR_ID=?, LAST_EDIT_DATE=?, VALIDE=? where FONCTION_ID=?
    Hibernate: insert into SCHEMACRM.FONCTION_ROLE (FONCTION_ID, ROLE_ID) values (?, ?)
    2007-08-14 11:56:01,073 [http-8080-Processor25] ERROR org.hibernate.util.JDBCExceptionReporter  - DB2 SQL error: SQLCODE: -407, SQLSTATE: 23502, SQLERRMC: TBSPACEID=2, TABLEID=129, COLNO=0
    2007-08-14 11:56:01,073 [http-8080-Processor25] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize database state with session
    org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [com.abw.valueobject.FonctionVO.roles#1]
    	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    	at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1394)
    	at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:56)
    	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
    	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
    	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
    	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    	at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:561)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:611)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:581)
    	at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:307)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
    	at $Proxy30.update(Unknown Source)
    	at com.myorg.coordination.administration.AdminFonctionsModifAction.saveRole(AdminFonctionsModifAction.java:130)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    (...)
    org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    	at java.lang.Thread.run(Thread.java:595)
    Caused by: com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -407, SQLSTATE: 23502, SQLERRMC: TBSPACEID=2, TABLEID=129, COLNO=0
    	at com.ibm.db2.jcc.b.zc.d(zc.java:1351)
    	at com.ibm.db2.jcc.a.db.l(db.java:366)
    	at com.ibm.db2.jcc.a.db.a(db.java:64)
    	at com.ibm.db2.jcc.a.r.a(r.java:48)
    	at com.ibm.db2.jcc.a.tb.c(tb.java:266)
    	at com.ibm.db2.jcc.b.ad.Z(ad.java:1666)
    	at com.ibm.db2.jcc.b.ad.d(ad.java:2224)
    	at com.ibm.db2.jcc.b.ad.V(ad.java:521)
    	at com.ibm.db2.jcc.b.ad.executeUpdate(ad.java:504)
    	at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
    	at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
    	at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1367)
    	... 94 more
    Comme si Hibernate essayait d'insérer le role, alors qu'il existe déjà. Moi je veux juste qu'il insert dans FONCTION_ROLE.

    Qqun aurait une idée ?

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    40
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 40
    Par défaut
    Personne ?

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    40
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 40
    Par défaut
    Bon ben j'ai trouvé

    Dans une association bidirectionnelle en many-to-many, la clé primaire de la table d'association DOIT être les 2 autres clés primaires.

    Donc la seule modif à faire était :

    FONCTION_ROLE
    id_fonction integer not null,
    id_role integer not null,
    primary key (id_fonction, id_role);

    au lieu de

    FONCTION_ROLE
    id_fonction_role integer not null,
    id_fonction integer not null,
    id_role integer not null,
    primary key (id_fonction_role);

    Si ça peut aider qqun.

  5. #5
    Invité de passage
    Inscrit en
    Décembre 2006
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 1
    Par défaut
    Ah bon dieu oui ça aide !

    Si je n'avais pas lu ceci je n'aurais jamais cru que retirer un inverse="true" d'un coté de la relation many-to-many aurait changer quelque chose à mon problème.

    (pour info ma table de jointure n'était jamais mise à jour malgré le cascade="all" associé dans le <name> de la relation many-to-many)

    Merci à toi, ougha

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

Discussions similaires

  1. Réponses: 4
    Dernier message: 24/04/2015, 11h03
  2. Réponses: 0
    Dernier message: 24/08/2009, 10h00
  3. Créer une table d'association ou pas?
    Par contremaitre dans le forum Langage SQL
    Réponses: 1
    Dernier message: 25/06/2008, 11h52
  4. [MySQL] UPDATE qui ne fonctionne pas
    Par philippef dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 13/09/2005, 14h35
  5. Réponses: 17
    Dernier message: 03/12/2004, 14h33

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