These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Many-to-many not inserting
PostPosted: Sat Jan 20, 2007 4:03 pm 
Newbie

Joined: Sat Jan 20, 2007 3:35 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 20, 2007 6:11 pm 
Newbie

Joined: Thu Nov 16, 2006 1:39 am
Posts: 13
Hi

I never worked with idbag, but maybe you should add inverse=true in your
second hbm file.

<idbag name="AssignedEmployees" table="Employee_Project" inverse="true" lazy="false">

Hope this will help


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 20, 2007 10:42 pm 
Newbie

Joined: Sat Jan 20, 2007 3:35 pm
Posts: 2
I tried adding inverse="true" but that doesn't appear to be a part of the idbag element...

Thanks for the suggestion, though!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 3:44 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Search the forum, this same question was asked and answered about a week ago. You need to call Flush().


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.