-->
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: Complicated mapping question
PostPosted: Thu May 19, 2005 5:24 am 
Hi,
I have the following domain objects that I would like to map but can't figure out how.

There exist 3 classes that represent composite structures which hold information.
Following the composite pattern I have implemented:

InfoComponent: an abstract class which has propertied of id, name (the component class)
AtomicInfo: derives from InfoComponent, and can hold an object (the leaf class)
CompositeInfo: derives from InfoComponent and has a collection of InfoComponents. Serves as a "folder" where information or other folders as stored (the composite class)


Now, consider an instance of this composite and let's call it a "file". A file organises info in groups, so I can have a file that has a folder "Family status" which contains a AtomicInfo called "Married" which holds a boolean.

So far so good. Problem is that "people" entities have different "files" which share the same structure but have different values in the structure.

In my non-ORmapped db I use the folllowing table structures:
Table CompositeInfo has id, name and disciminator (to distinguish if a component is a leaf or composite)
Table ParentChild has the parent id, and child id columns that describe which components a composite holds.
Table PersonValues has a person id, a composite id, and the value that the information has for that particular leaf for that particular person.

Despite my efforts haven't got around in mapping this. How would you do it?
Thanks a lot, and excuse the length of this message:)


Top
  
 
 Post subject:
PostPosted: Thu May 19, 2005 2:50 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I think mapping InfoComponent to a class with its subclasses mapped using <subclass> should work. Then to map a collection of components and their values you could use a <map> with its index mapped using <index-many-to-many> to an InfoComponent. Did you try that? What were the problems?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 19, 2005 9:34 pm 
Regular
Regular

Joined: Thu May 12, 2005 10:12 am
Posts: 71
Location: Buenos Aires, Argentina
Hi, in case your still wondering about how to solve this, I ran a small sample to help you. I didn't quite understand what the PersonValues table do but I can give you an example of how to map yur composite example.

Here goes the composite pattern with m-m relationship between parent and childs:



Code:
<hibernate-mapping
   xmlns="urn:nhibernate-mapping-2.0"
   namespace="NHForumHelp.Model.ComponentClass"
   assembly="NHForumHelp.Model">

   <class
      name="InfoComponent"
      table="CompositeInfo"
   >
       <id
         name="ID"
         column="id"
         type="Int32"
         length="4"> 
         <generator
            class="native"
         /> 
      </id>
      
      <discriminator
         column="discrim"
         type="string"
      />
      
      <property
         name="Name"
         column="name"
         type="String"
         length="50"
      />
      
      <subclass
         discriminator-value="L" 
         name="AtomicInfo">
      </subclass>
      
      <subclass
         discriminator-value="C" 
         name="CompositeInfo">
         <bag name="Components" table="ParentChild">
            <key
               column="parentID"
            />
            <many-to-many
               class="InfoComponent"
               column="childID"
            />
         </bag>
      </subclass>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 4:19 am 
Thanks a lot for the reply, I'll try it out and let you know...


Top
  
 
 Post subject: Still no luck
PostPosted: Fri May 20, 2005 11:18 am 
Well, so far not luck. Let's forget about persons and concentrate on the composite.
The closest I got was with the following map:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="InfoLib" assembly="InfoLib">
   <class name="InfoComponent" table="CompositeInfo">
      <id name="Id" column="id" type="Int32" unsaved-value="0">
         <generator class="native" />
      </id>
      <discriminator column="discr" type="string" length="1" />
      <property name="Name" column="name" type="String" length="50" />
      <property name="Description" column="descr" type="String" length="50" />
      <subclass discriminator-value="A" name="AtomicInfo"></subclass>
      <subclass discriminator-value="C" name="CompositeInfo">
         <map name="Components" table="parentchild" inverse="true" cascade="save-update" >
            <key>
               <column name="Id" />
            </key>
            <index-many-to-many class="CompositeInfo" column="parentID" />
            <many-to-many class="InfoComponent" column="childID" />
         </map>
      </subclass>
   </class>
</hibernate-mapping>



This gets me as far as inserting the composites but not updating the relationship table (composite x contains infocomponent)

Being a newbien in hibernate I really got this by playing around. So I don't have an understanding of why I need the inverse="true". Also I'm not sure about the correctness of the index-many-to-many and many-to-many.

Any further help will be really appreciated.
Thanks,
gg

PS. If you want I can strip down the code and include it here... I didn't cause I don't think it will really help...


Top
  
 
 Post subject:
PostPosted: Fri May 20, 2005 1:30 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Inverse basically says that the contained entity is responsible for the foreign key value. This means the contained entity should have the reference to its container (the inverse reference) and that reference is what is taken into account. Since your InfoComponents probably don't have pointers back to the composites they are contained in, you should try inverse="false", then the association should be updated.


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.