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:
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:
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:
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:
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:
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:
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:
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é