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 de relation 1-N avec hibernate 3


Sujet :

Hibernate Java

  1. #1
    Inactif
    Inscrit en
    Mars 2005
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 42
    Points : 31
    Points
    31
    Par défaut Problème de relation 1-N avec hibernate 3
    Bonjour,
    J'essaie de faire une exemple de 2 table (User qui contient N*Roles)

    Je l'ai développé comme suit:

    class Roles
    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
    package app ;
     
    public class Roles  {
        private Long id ; 
        private String name ;
        private Long iduserR ;
     
        public Roles() {
            this.id = null ;
            this.name = null ;
            this.iduserR = null ;
     
        }
     
        public Roles(String name,Long iduser) {
            this.id = null ; 
            this.name = name ;
            this.iduserR = iduser ;
     
        }
     
        void   setId (Long id) { this.id = id ;}
        public Long   getId () {return id ;}
     
        public void   setName (String name) { this.name = name ;}
        public String getName ()    { return name ;}
     
        public void   setIduserR (Long iduserR) { this.iduserR = iduserR ;}
        public Long getIduserR ()    { return iduserR ;}
    }
    class User
    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
    package app ;
     
    import java.util.Set;
     
    public class User {
        private Long id ; 
        private String name ;
        private String login ;
        private String email ;
        private Set<Roles> role1;
     
        public User() {
            this.id = null ;
            this.name = null ;
            this.login= null;
            this.email= null;
            this.role1 = null;
        }
     
        public User(String name,String email,String login,Set<Roles> role1) {
            this.id = null ; 
            this.name = name ;
            this.login=login;
            this.email=email;
            this.role1=role1;
        }
     
        void   setId (Long id) { this.id = id ;}
        public Long   getId () {return id ;}
     
        void   setLogin(String login) { this.login = login ;}
        public String   getLogin () {return login ;}
     
     
     
        public void   setName(String name) { this.name = name ;}
        public String getName ()    { return name ;}
     
        public void   setEmail(String email) { this.email = email ;}
        public String   getEmail () {return email ; }
     
        public void   setRole1(Set<Roles> Role1) { this.role1 = Role1 ;}
        public Set<Roles> getRole1 ()    { return role1 ;}
     
     
    }
    Roles.hbm.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
    15
    16
    17
    18
    19
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     
    <hibernate-mapping package="app" >
      <class name="Roles" table="role">
     
       <id name="id" type="long" column="id">
          <generator class="native">
          </generator>
        </id>
     
        <property name="name" column="name" length="20"/>
     
         <many-to-one name="iduserR" class="User" column="iduser" />
     
      </class>
    </hibernate-mapping>
    user.hbm.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
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     
    <hibernate-mapping package="app" >
      <class name="User" table="user">
     
       <id name="id" type="long" column="id">
          <generator class="native">
          </generator>
        </id>
     
        <property name="login" column="login" length="20"/>
        <property name="name" column="name" length="20"/>
        <property name="email" column="email" length="20"/>
     
     	<set name="role1" inverse="true" cascade="all">
                    <key column="id"/>
                    <one-to-many class="Roles"/>
            </set>
     
      </class>
    </hibernate-mapping>
    Ma class Main
    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 Main{
     
    	 @SuppressWarnings("unchecked")
    	public static void main(String[] args)
    		throws HibernateException {
    		 System.out.println("1");
     
    		 Session session = HibernateUtil.getSessionFactory().openSession(); 
    		 Transaction tx = session.beginTransaction();
     
    		 /********** ROLES ******************/
     
    		 List user2modifie=null;
    		 user2modifie = session.createQuery("from Roles as role where role.name like '%rol1%' ").list();
    		 for ( Iterator iter = user2modifie.iterator(); iter.hasNext(); ) {
    			 Roles contact22 = (Roles) iter.next();
    	         System.out.println( contact22.getName() );
    	        }
     
    		 tx.commit();
    	        session.close();
     
    	 }
    	}
    MA BD
    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
    CREATE TABLE  `testhib`.`user` (
      `id` int(10) unsigned NOT NULL auto_increment,
      `login` varchar(45) NOT NULL,
      `name` varchar(45) NOT NULL,
      `email` varchar(45) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;
     
    CREATE TABLE  `testhib`.`role` (
      `id` int(10) unsigned NOT NULL auto_increment,
      `name` varchar(45) NOT NULL,
      `iduser` int(10) unsigned default NULL,
      PRIMARY KEY  (`id`),
      KEY `FK_role_1` USING BTREE (`iduser`),
      CONSTRAINT `FK_role_1` FOREIGN KEY (`iduser`) REFERENCES `user` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
    MAIS Quand je test , voila ce que ca me sort
    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
    1
    10:21:08,227 ERROR BasicPropertyAccessor:94 - IllegalArgumentException in class: app.Roles, setter method of property: iduserR
    10:21:08,227 ERROR BasicPropertyAccessor:98 - expected type: java.lang.Long, actual value: app.User$$EnhancerByCGLIB$$6b9b5976
    Exception in thread "main" org.hibernate.PropertyAccessException:
    IllegalArgumentException occurred while calling setter of app.Roles.iduserR
    	at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:104)
    	at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
    	at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
    	at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3499)
    	at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
    	at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    	at org.hibernate.loader.Loader.doList(Loader.java:2144)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
    	at org.hibernate.loader.Loader.list(Loader.java:2023)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:393)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    	at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    	at app.Exec.main(Exec.java:28)
    Caused by: java.lang.IllegalArgumentException: argument type mismatch
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
    	... 16 more

    Merci de m'aider je suis bloqué

  2. #2
    Membre confirmé Avatar de JoloKossovar
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    532
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 532
    Points : 576
    Points
    576
    Par défaut
    Il y a une incohérence, dans ton fichier de mapping hibernate tu specifie que iduser va etre de type User alors que dans ta classe Roles, idUser est un Long.

  3. #3
    Inactif
    Inscrit en
    Mars 2005
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 42
    Points : 31
    Points
    31
    Par défaut
    Merci beaucoup tu ma sauvé


  4. #4
    Membre confirmé Avatar de JoloKossovar
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    532
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 532
    Points : 576
    Points
    576
    Par défaut
    de rien n oublies pas le tag résolu.

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

Discussions similaires

  1. Problème de chargement des données avec hibernate
    Par nasnet dans le forum Langages de programmation
    Réponses: 0
    Dernier message: 02/07/2013, 17h46
  2. [1.x] Problème de relation 1-1 avec Doctrine
    Par Niki59 dans le forum Symfony
    Réponses: 6
    Dernier message: 05/04/2011, 14h58
  3. probléme d'exécution de requete avec hibernate
    Par yazen dans le forum Persistance des données
    Réponses: 3
    Dernier message: 17/06/2008, 11h36
  4. Problème de connection avec Hibernate Synchronizer
    Par ouzzine dans le forum Hibernate
    Réponses: 8
    Dernier message: 30/11/2006, 13h51
  5. [Hibernate] Problème avec Hibernate et Eclipse 3
    Par theseuby dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 30/03/2006, 21h31

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