Please, I'm wonder because I can't solve it.
I type you role.hbm.xml, user.hbm.xml, user.cs, role.cs, UserFactory.cs and RoleFactory.cs.
I think that when I perform
Code:
rol = (PersistentClasses.Role)this.session.Load(typeof(PersistentClasses.Role),key);
NHibernate should obtain or initialize Users property as a collection of Users, but it isn't how I think. After I've performed Load function I try to access to Users property of rol, but how Users is null, .NET throws an Exception.
When I load an User, your Role property is initialitzed correctly, but Users property of Role isn't initialized!!!
Thanks in advance
Main thread -->Code:
PersistentClasses.RolFactory roleFactory = new PersistentClasses.RolFactory();
PersistentClasses.Role rol = new PersistentClasses.Role(60,"NHibernate");
roleFactory.saveRol(rol);
PersistentClasses.Role rol2 = new PersistentClasses.Role(61,"NHibernate2");
roleFactory.saveRol(rol2);
PersistentClasses.UserFactory userFactory = new PersistentClasses.UserFactory();
PersistentClasses.User usuari = new PersistentClasses.User("Jordi","password","Jordi",rol);
userFactory.saveUser(usuari);
usuari.Rol = rol2;
userFactory.updateUser(usuari);
PersistentClasses.User useri = userFactory.getUser("Jordi");
PersistentClasses.Role role = roleFactory.getRole(61);
-----------------All is Ok to here, but when I access to role.Users crashes because it's null--------------------
foreach (PersistentClasses.User user in role.Users)
{
this.listBoxControl1.Items.Add(user.Name);
}
Role.csCode:
public class Role
{
private byte rol;
private string descripcio;
private System.Collections.IDictionary users;
public Role()
{
this.rol = 0;
this.descripcio = "";
this.users = null;
}
public Role(byte rol, string descripcio)
{
this.rol = rol;
this.descripcio = descripcio;
}
public string Descripcio
{
get { return this.descripcio; }
set { this.descripcio = value; }
}
public byte Rol
{
get { return this.rol; }
set { this.rol = value; }
}
public System.Collections.IDictionary Users
{
get { return this.users; }
set { this.users = value; }
}
}
User.cs-->Code:
public class User
{
private string login;
private string password;
private string name;
private PersistentClasses.Role rol;
public User()
{
this.login = "";
this.password = "";
this.name = "";
this.rol = null;
}
public User(string login, string password, string nom, PersistentClasses.Role rol)
{
this.login = login;
this.password = password;
this.name = nom;
this.rol = rol;
}
public string Login
{
get { return this.login; }
set { this.login = value; }
}
public PersistentClasses.Role Rol
{
get { return this.rol; }
set { this.rol = value; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public string Password
{
get { return this.password; }
set { this.password = value; }
}
}
Role.hbm.xmlCode:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="PersistentClasses.Role, PersistentClasses" table="ROLS">
<id name="Rol" column="ROL" type="Byte">
<generator class="assigned" />
</id>
<property name="Descripcio" column="DESCRIPCIO" type="String" length="16"/>
<set name="Users" inverse="true" lazy="true" table="USUARIS">
<key column="ROL"/>
<one-to-many class="PersistentClasses.User, PersistentClasses"/>
</set>
</class>
</hibernate-mapping>
User.hbm.xml-->Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="PersistentClasses.User, PersistentClasses" table="USUARIS">
<id name="Login" column="LOGIN" type="String" length="8">
<generator class="assigned" />
</id>
<property name="Password" column="PASSWORD" type="String" length="16"/>
<property name="Name" column="NOM" type="String" length="32"/>
<many-to-one name="Rol" column="ROL" unique="true" not-null="true" class="PersistentClasses.Role,PersistentClasses"/>
<!--<property name="Rol" column="ROL" type="Int16"/> -->
</class>
</hibernate-mapping>
RoleFactory.cs-->Code:
private NHibernate.Cfg.Configuration configuration;
private NHibernate.ISessionFactory sessionFactory;
private NHibernate.ISession session;
public RolFactory()
{
this.configuration = new NHibernate.Cfg.Configuration();
System.Collections.IDictionary props = new System.Collections.Hashtable();
props["hibernate.connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
props["hibernate.dialect" ] = "NHibernate.Dialect.MsSql2000Dialect";
props["hibernate.connection.driver_class" ] = "NHibernate.Driver.SqlClientDriver" ;
props["hibernate.connection.connection_string"] = "Server=PORTATIL;initial catalog=NHibernate;User ID=CABRE;Password=CABRE";
foreach( System.Collections.DictionaryEntry de in props )
{
this.configuration.SetProperty( de.Key.ToString(), de.Value.ToString() );
}
this.configuration.AddAssembly("PersistentClasses");
this.configuration.AddClass(typeof(PersistentClasses.Role));
this.sessionFactory = this.configuration.BuildSessionFactory();
this.session = this.sessionFactory.OpenSession();
}
public void saveRol(PersistentClasses.Role rol)
{
try
{
NHibernate.ITransaction transaction = this.session.BeginTransaction();
this.session.Save(rol);
transaction.Commit();
this.session.Flush();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
}
public PersistentClasses.Role getRole(byte key)
{
NHibernate.ITransaction transaction = this.session.BeginTransaction();
PersistentClasses.Role rol = new PersistentClasses.Role();
try
{
rol = (PersistentClasses.Role)this.session.Load(typeof(PersistentClasses.Role),key);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
transaction.Commit();
return rol;
}
UserFactory.cs-->Code:
public class UserFactory
{
private NHibernate.Cfg.Configuration configuration;
private NHibernate.ISessionFactory sessionFactory;
private NHibernate.ISession session;
public UserFactory()
{
this.configuration = new NHibernate.Cfg.Configuration();
System.Collections.IDictionary props = new System.Collections.Hashtable();
props["hibernate.connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
props["hibernate.dialect" ] = "NHibernate.Dialect.MsSql2000Dialect";
props["hibernate.connection.driver_class" ] = "NHibernate.Driver.SqlClientDriver" ;
props["hibernate.connection.connection_string"] = "Server=PORTATIL;initial catalog=NHibernate;User ID=CABRE;Password=CABRE" ;
foreach( System.Collections.DictionaryEntry de in props )
{
this.configuration.SetProperty( de.Key.ToString(), de.Value.ToString() );
}
this.configuration.AddAssembly("PersistentClasses");
this.configuration.AddClass(typeof(PersistentClasses.User));
this.configuration.AddClass(typeof(PersistentClasses.Role));
this.sessionFactory = this.configuration.BuildSessionFactory();
this.session = this.sessionFactory.OpenSession();
}
public void Dispose()
{
this.session.Dispose();
this.sessionFactory.Close();
}
public void saveUser(PersistentClasses.User user)
{
try
{
NHibernate.ITransaction transaction = this.session.BeginTransaction();
this.session.Save(user);
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
}
public PersistentClasses.User getUser(string login)
{
PersistentClasses.User user = new User();
//user.Login = login;
try
{
NHibernate.ITransaction transaction = this.session.BeginTransaction();
user = (PersistentClasses.User)this.session.Get(typeof(PersistentClasses.User),login);
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
return user;
}
public void updateUser(PersistentClasses.User user)
{
try
{
NHibernate.ITransaction transaction = this.session.BeginTransaction();
this.session.Update(user);
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
}
}