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.  [ 3 posts ] 
Author Message
 Post subject: Using Dictionary Property for a 1-M Collection
PostPosted: Wed Nov 28, 2007 3:10 pm 
Regular
Regular

Joined: Thu Nov 30, 2006 10:48 am
Posts: 59
MyClass1 has a 1-M unidirectional relationship with MyClass2. I want MyClass1 to have a Dictionary property (key=ID field of MyClass2, value=MyClass2). I tried using a map but can't get it to work. Can this be done?

Classes:
public class MyClass1
{
public int ID { ... }
public IDictionary<int, MyClass2> PMyClass2 {...}
}

public class MyClass2
{
public int ID { ... }
}

tables:
tblMyClass1: ID field (autoidentity)
tblMyClass2: ID field (autoidentity); FK_MyClass1ID (foreign key to tblMyClass1)

Mapping files - the <map> element is wrong but it's a placeholder for what I conceptually want :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="MyClass1, DomainModel" table="dbo.tblMyClass1">
<map name="PMyClass2" cascade="all" outer-join="false">
<key column="FK_MyClass1ID"/>
<one-to-many class="MyClass2, Domain"/>
</map>

</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="MyClass2, DomainModel" table="dbo.tblMyClass2" lazy="true">
<class>
</hibernate-mapping>

Thanks --
Bill

_________________
metazone


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 4:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
First of all, the <map> element requires an <index> element. Secondly, using an auto-generated ID as the index of a <map> poses a problem: you cannot add a MyClass2 to the map until you save it. In other words, what do you use as a key to add an unsaved MyClass2 to the map?

You may want to consider using a <set> or <bag> instead.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 4:45 pm 
Regular
Regular

Joined: Thu Nov 30, 2006 10:48 am
Posts: 59
Thanks -- I reworked my class to use IList and have the dictionary generated from the modification of the IList. I use a <set> element for the list.

_________________
metazone


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