Hello,

I am a newbie on nhibernate and i am trying to mapping many-to-many
relationship between 2 tables (users and groups) and a third (user_group).
My current NHibernate version is NHibernate-2.1.2.GA-bin.

I could make user ilist<group> work but i couldn't make inverse. NHibernate
does not compile.

Here is my tables:

CREATE TABLE tb_users
(
id BIGINT IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL,
login VARCHAR(30) NOT NULL UNIQUE,
password VARCHAR(100) NOT NULL,
cpf VARCHAR(30) NOT NULL UNIQUE,
expiratedDate DATETIME DEFAULT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
user_status int REFERENCES tb_user_status(id) NOT NULL
);

CREATE TABLE tb_groups
(
id INT IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
active BIT NOT NULL
);

CREATE TABLE tb_users_group
(
id_user BIGINT REFERENCES tb_users(id) NOT NULL,
id_group INT REFERENCES tb_groups(id) NOT NULL,
PRIMARY KEY(id_user, id_group)
);

Here is my mapping files:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="AccessControl"
                   namespace="AccessControl.TransportObject">

  <class name="Group" table="tb_groups"  optimistic-lock="dirty"
dynamic-update="true" dynamic-insert="true" lazy="true">
    <id name="Id" column="id" type="int" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Name">
      <column name="name" sql-type="string" length="100" not-null="true" />
    </property>
    <property name="Active">
      <column name="active" sql-type="bit" />
    </property>

<!-- when this likes are not commented nhibernate cannot compile -->
    <bag name="Users" table="tb_users_group" lazy="true">
      <key column="id_group" />
      <many-to-many class="Users, AccessControl.TransportObject"
column="id_user" />
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="AccessControl"
                   namespace="AccessControl.TransportObject">

  <class name="User" table="tb_users"  optimistic-lock="dirty"
dynamic-update="true" dynamic-insert="true" lazy="true">
    <id name="Id" column="id" type="long" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Name">
      <column name="name" sql-type="string" length="100" not-null="true" />
    </property>
    <property name="Login">
      <column name="login" sql-type="string" length="30" not-null="true"
unique="true" />
    </property>
    <property name="PasswordHash">
      <column name="password" sql-type="string" length="100" not-null="true"
/>
    </property>
    <property name="CPFString">
      <column name="cpf" sql-type="string" length="30" not-null="true"
unique="true" />
    </property>
    <property name="ExpiratedDate">
      <column name="expiratedDate" sql-type="datetime" />
    </property>
    <property name="EMailString">
      <column name="email" sql-type="string" length="100" not-null="true"
unique="true" />
    </property>
    <many-to-one name="Status" class="UserStatus" column="user_status"
not-null="true" />
<!-- this one works fine -->
    <bag name="Groups" table="tb_users_group" cascade="all" lazy="true">
      <key column="id_user" foreign-key="id" />
      <many-to-many class="AccessControl.TransportObject.Group"
column="id_group" foreign-key="id" />
    </bag>
  </class>
</hibernate-mapping>

Transport Objects:

public class Group
{
public Group()
{
this.Users = new List<User>();
}

public virtual int Id { set; get; }
public virtual string Name { set; get; }
public virtual bool Active { set; get; }
public virtual IList<User> Users { set; get; }
}

public class User
{
public User()
{
this.CPF = new CPF();
this.EMail = new EMail();
this.Groups = new List<Group>();
}

public virtual Int64 Id { set; get; }
public virtual string Name { set; get; }
public virtual string Login { set; get; }

public virtual string PasswordHash { set; get; }
public virtual string PasswordRaw
{
set
{
this.PasswordHash = SecurityFacade.ToHash(value, HashProvider.SHA1);
}
}

public virtual CPF CPF { set; get; }
public virtual string CPFString
{
set
{
CPF.Value = value;
}
get
{
return CPF.ToString();
}
}

public virtual EMail EMail { set; get; }
public virtual string EMailString
{
set
{
EMail.Value = value;
}
get
{
return EMail.ToString();
}
}

public virtual DateTime ExpiratedDate { set; get; }
public virtual UserStatus Status { set; get; }
public virtual IList<Group> Groups { set; get; }
}

NHibernate error is:

Also, sometimes I have only this exception when my mappings cannot compile
"Exception:Caught: "Could not load file or assembly
'NHibernate.XmlSerializers, Version=2.1.2.4000, Culture=neutral,
PublicKeyToken=aa95f207798dfdb4' or one of its dependencies. O sistema não
pode encontrar o arquivo especificado." (System.IO.FileNotFoundException)
A System.IO.FileNotFoundException was caught: "Could not load file or
assembly 'NHibernate.XmlSerializers, Version=2.1.2.4000, Culture=neutral,
PublicKeyToken=aa95f207798dfdb4' or one of its dependencies. O sistema não
pode encontrar o arquivo especificado.""

Sometimes it does an exception about dictionary index missing.

Someone have a tip to solve this issue?

Thank you.

Rúben Lício Reis
+55 11 67041245

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to