-->
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: Hashtable
PostPosted: Tue Jan 10, 2006 9:25 am 
Newbie

Joined: Sun Jan 08, 2006 2:26 pm
Posts: 15
Hi there again!

I am still confused about association mapping in nHibernate and cant get a special case fixed :-(

In my Domainmodel i got a class Customer and Employee. So Customer is holding a Hashtable of employeeComments. There are no Properties defined. There are instead some public methods to get and set the hashtable.

public class User
{
private Hashtable employeeComments;
....
public string GetEmployeeComments( Employee emp)
{
if( employeeComments == null )
{
return null;
}
else
{
return (string) employeeComments[emp];
}
}

public void SetEmployeeComments( Employee emp, string comments)
{
if( employeeComments == null )
{
employeeComments = new Hashtable();
}
else
{
//the old value has to be removed first,
//if there is no value contained for empl, then it is a no-op
employeeComments.Remove( emp );
}
employeeComments[emp] = comments;
}
.....
}

I played around with some mappings, but it didnt work out.
I thought i should use something like:

<class name="NHTest.Customer, NHTest" table="Customer">
<id name="Id" column="Id" type="Int32">
<generator class="hilo" />
</id>
<properties..../>

<set name="employeeComments" access="field" table="Comments" cascade="all-delete-orphan">
<key column="CustId"/>
<many-to-many class="NHTest.Employee, NHTest" column="EmployeeId"/>
</set>
<many-to-one class="NHTest.Adress, NHTest"....


And the creationcode of some objects:

Adress ad = new Adress(..)...
Employee emp = new Employee("Lei", "Kathi", System.DateTime.Now);
//session.Save(emp);
Customer cust = new Customer();
cust.Name = "Bla";
cust.Surname ="Chup";
cust.CustomerNr=1001;
cust.Adress=ad;
cust.SetEmployeeComments(emp, "Lazy Dog");
session.Save(cust);

At session.Save(cust) i got an exception:

An unhandled exception of type 'NHibernate.ADOException' occurred in nhibernate.dll

Additional information: Could not save object


Still i would need an element of something like <element="comments" type="String" in the set. How could i implement this. A mix of value and entities inside an set isnt allowed? I think i got somethin fundamentally wrong and i am really confused now. Any help would be really great!

Thx

the newbie Chris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 12:04 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
I believe the <map> collection mapping is for a IDictionary (Hashtable) collection, but I've never used it. You probably need to check the Hibernate (Java) documentation, as it's not well documented for NHibernate (yet).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 1:34 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
You should use a ternary association map like the examples here. Since you only have one comment per employee per customer then using composite elements shouldn't be necessary.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 4:09 pm 
Newbie

Joined: Sun Jan 08, 2006 2:26 pm
Posts: 15
Thx for this link again! Now i will check the HibernateDoc first!

Finally i´ve got a kind of "workaround"...

I´ve added an empty Set().
Code:

public IDictionary EmployeeComments      {
         get
         {
            return employeeComments;         }
         set
         {
         }


and this mapping:
Code:
<map name="EmployeeComments" lazy="true" >
      <key column="CustomerId"/>
      <index-many-to-many column="EmployeeId" class="NHTest.Employee, NHTest"/>
      <element column="Comment" type="String"/>
</map>


Regards

Chris


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.