Dear hibernate lovers...
I'm testing hibernate 3 and i'm lost...
I've a relation one to many bidirectional
Map1
Code:
<class name="Role">
<id name="idRole"/>
<property name="nom"/>
<property name="lire"/>
<property name="ecrire"/>
<set name="utilisateurs" inverse="true">
<key column="idRole_fk"/>
<one-to-many class="Utilisateur"/>
</set>
</class>
Map2
Code:
<class name="Utilisateur">
<id name="idUtilisateur"/>
<property name="nom"/>
<property name="prenom"/>
<property name="login"/>
<property name="password"/>
<many-to-one name="role" column="idRole_fk" not-null="true"/>
</class>
Class1
Code:
private String idUtilisateur;
private String nom;
private String prenom;
private String login;
private String password;
private Role role;
Class2
Code:
private String idRole;
private String nom;
private String lire;
private String ecrire;
private Set utilisateurs;
Test code
Code:
Utilisateur u1 = (Utilisateur) session.load(Utilisateur.class, "xxx");
Role r1 = u1.getRole();
Set s1 = r1.getUtilisateurs();
System.out.println(s1.size());
//---------------------------------------
Role r2 = (Role) session.load(Role.class, r1.getNom());
Set s2 = r2.getUtilisateurs();
System.out.println(s2.size());
//---------------------------------------
Query query = session.createQuery("select c from Role as c");
for (Iterator it = query.iterate(); it.hasNext();)
{
Role r3 = (Role) it.next();
Set s3 = r3.getUtilisateurs();
System.out.println(s3.size());
}
Console output
Code:
Hibernate: select utilisateu0_.idRole_fk as idRole6___, utilisateu0_.idUtilisateur as idUtilis1___, utilisateu0_.idUtilisateur as idUtilis1_0_, utilisateu0_.nom as nom7_0_, utilisateu0_.prenom as prenom7_0_, utilisateu0_.login as login7_0_, utilisateu0_.password as password7_0_, utilisateu0_.idRole_fk as idRole6_7_0_ from Utilisateur utilisateu0_ where utilisateu0_.idRole_fk=?
2
Hibernate: select utilisateu0_.idRole_fk as idRole6___, utilisateu0_.idUtilisateur as idUtilis1___, utilisateu0_.idUtilisateur as idUtilis1_0_, utilisateu0_.nom as nom7_0_, utilisateu0_.prenom as prenom7_0_, utilisateu0_.login as login7_0_, utilisateu0_.password as password7_0_, utilisateu0_.idRole_fk as idRole6_7_0_ from Utilisateur utilisateu0_ where utilisateu0_.idRole_fk=?
0
Hibernate: select role0_.idRole as col_0_0_ from Role role0_
2
WHY the first and second request are the same but the result not...
Is session.load() different?? and why
Tankxx for your lights...