-->
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.  [ 6 posts ] 
Author Message
 Post subject: Saving collections of an object
PostPosted: Thu Jun 02, 2005 4:33 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I have an employee object which has a list of EmployeeProject objects. This object indicates which employee is involved in which project. It has an Employee property and a Project property.
The user can select a project in a listbox and add it to another listbox with involved projects. The project is added to the employee like this:
Code:
private void btnAdd_Click(object sender, System.EventArgs e)
{
   ListItem item = lbxProjects.SelectedItem;
   if (item != null)
   {
           EmployeeProject proj = new EmployeeProject();
      proj.Employee = _employee;
      proj.Project= (Project)MyUtils.GetObjectById(typeof(Project), Int32.Parse(item.Value));
      _employee.Projects.Add(proj);

      lbxEmployeeProjects.Items.Add(item);
      item.Selected = false;
   }
}


But when I save the employee, the EmployeeProject objects are not saved to the database. I guess my mapping file isn't correct yet for the EmployeeProject collection:
Code:
<bag name="Projects" table="employeeproject" lazy="true">
   <key column="employeeid"/>
   <one-to-many class="MyApp.Core.Domain.EmployeeProject, MyApp.Core"/>
</bag>

What do I need to do to save the EmployeeProject objects?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 5:53 pm 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
You have to add a cascade attribute to your bag collection and remove the table attribute if the relation is a one-to-many.
Code:
<bag name="Projects" lazy="true" cascade="all">

BTW, if your EmployeeProject class does nothing more than connection Employees and Projects you might consider removing this class and add an IList of Projects directly to the Employee. This is a many-to-many relation and looks as follows:

Code:
<bag name="Projects" table="employeeprojects" cascade="none" lazy="true">
    <key column="employeeid" />
    <many-to-many class="MyApp.Core.Domain.Project, MyApp.Core" column="projectid"  />
</bag>

_________________
Cuyahoga


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 5:43 am 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
Thanks Martijn,

I will try this tonight. But what does the 'lazy' element?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 6:02 am 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
The lazy element means that the collections is only fetched from the database when accessed. See also http://nhibernate.sourceforge.net/h2.0. ... tions-s1-7 .

_________________
Cuyahoga


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 4:04 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
The last time I solved my problem by removing the EmployeeProject class and defining a many-to-many relation between employees and projects.
I'm now in the situation that I have to make a separate class. Instances of this class don't get saved into the database. This is the bag in the mapping file of the Employee class:
Code:
<bag name="EmployeeProjects" cascade="all" lazy="true">
  <key column="employeeid"/>         
  <one-to-many class="MyApp.Core.Domain.EmployeeProject, MyApp.Core"/>
</bag>


What am I doing wrong?

Regards,
JackBigMac


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 1:42 am 
Beginner
Beginner

Joined: Thu May 12, 2005 2:14 am
Posts: 33
Location: Sweden, Sk
Hi

Im not sure this will solve it but try adding inverse=true to your mapping files for the bag.

<bag name="Projects" table="employeeproject" lazy="true" inverse="true">


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.