I'm using NHibernate 1.0.3.0
Maybe I'm crazy, but I cannot get this to work...
Basically, I have a many-to-many relationship between employees and projects; the idea being that an employee can be assigned many projects. When I assign a project to an employee, the many-to-many does not get inserted.
Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" auto-import="true" default-access="field.camelcase">
<class name="Entities.Employee, Entities" table="Employees" optimistic-lock="version" dynamic-update="true">
<id name="Identifier" column="EmployeeId" type="Int64" unsaved-value="0">
<generator class="identity" />
</id>
<version name="LastModifiedDateAsTicks" column="LastModifiedDateTicks" type="TimeSpan" unsaved-value="null" />
<property name="CreatedDateAsTicks" column="CreatedDateTicks" type="Int64" not-null="true" />
<property name="FirstName" column="FirstName" type="String" not-null="true" />
<property name="LastName" column="LastName" type="String" not-null="true" />
<property name="UserName" column="UserName" type="String" not-null="true" />
<idbag name="AssignedProjects" table="Employee_Project" lazy="false">
<collection-id column="EmployeeProjectId" type="Guid">
<generator class="guid.comb"/>
</collection-id>
<key column="EmployeeId"/>
<many-to-many column="ProjectId" class="Entities.Project, Entities" fetch="join" />
</idbag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" auto-import="true" default-access="field.camelcase">
<class name="Entities.Project, Entities" table="Projects" optimistic-lock="version" dynamic-update="true">
<id name="Identifier" column="ProjectId" type="Int64" unsaved-value="0">
<generator class="identity" />
</id>
<version name="LastModifiedDateAsTicks" column="LastModifiedDateTicks" type="TimeSpan" unsaved-value="null" />
<property name="CreatedDateAsTicks" column="CreatedDateTicks" type="Int64" not-null="true" />
<property name="Name" column="Name" type="String" not-null="true" />
<many-to-one name="ParentProject" class="Entities.Project, Entities" column="ParentProjectId" not-null="false" />
<idbag name="AssignedEmployees" table="Employee_Project" lazy="false">
<collection-id column="EmployeeProjectId" type="Guid">
<generator class="guid.comb"/>
</collection-id>
<key column="ProjectId"/>
<many-to-many column="EmployeeId" class="Employee, Entities" fetch="join" />
</idbag>
<bag name="ChildProjects" cascade="all-delete-orphan" inverse="true">
<key column="ProjectId" />
<one-to-many class="Entities.Project, Entities" />
</bag>
</class>
</hibernate-mapping>
This is the code I use:
Employee johnStanfield = new Employee();
johnStanfield.FirstName = "John";
johnStanfield.LastName = "Stanfield";
johnStanfield.UserName = "jstanfield";
johnStanfield.CreatedDate = DateTime.Now;
johnStanfield.LastModifiedDate = DateTime.Now;
HibernateSessionFactory.CurrentSession.Save(johnStanfield);
// johnstanfield is created successfully (but AssignedProjects is null)
johnStanfield = HibernateSessionFactory.CurrentSession.Get(johnStanfield.Identifier);
// johnstanfield.AssignedProjects is now an identifier bag instead of null
Project myProject = new Project();
myProject.Name = "My Project";
HibernateSessionFactory.CurrentSession.Save(myProject);
// myProject is created successfully
johnStanfield.AssignedProjects.Add(myProject);
johnStanfield.FirstName = "Johnz";
HibernateSessionFactory.CurrentSession.Update(johnStanfield);
// no record inserted into Employee_Project
// johnstanfield's first name is Johnz in the database
// with show_sql = "true" I do not see any insert statement.
No exceptions occur.
I'm using SQL Server 2000, here is the script:
create table Employees
(
EmployeeId int identity(1,1) not null,
FirstName varchar(100) not null,
LastName varchar(100) not null,
UserName varchar(100) not null,
CreatedDateTicks bigint not null,
LastModifiedDateTicks bigint not null,
constraint EmployeesPK primary key (EmployeeId)
)
go
create table Projects
(
ProjectId int identity(1,1) not null,
ParentProjectId int null,
Name varchar(100) not null,
CreatedDateTicks bigint not null,
LastModifiedDateTicks bigint not null,
constraint ProjectsPK primary key (ProjectId),
constraint ProjectProjectIdFK foreign key (ProjectId) references Projects(ProjectId)
)
go
create table Employee_Project
(
EmployeeProjectId uniqueidentifier not null,
EmployeeId int not null,
ProjectId int not null,
constraint Employee_ProjectPK primary key (EmployeeProjectId, EmployeeId, ProjectId)
)
go
|