-->
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: One to many with a container object
PostPosted: Thu Nov 06, 2008 2:14 pm 
Newbie

Joined: Fri Aug 22, 2008 4:07 pm
Posts: 9
I am having trouble figuring out how to create a mapping document for my particular situation.

Before I go into the problem, I am using NHibernate 1.2.1 with Spring.NET 1.1.1.

The problem:

I have two tables, that have a simple one to many relationship. The tables are as follows:


Code:
InteractiveBooks:
=================
InteractiveBookID (PK)
InteractiveBookName
Public
Active
PrintableURL
UseStyle
...

InteractiveBookSections:
========================
InteractiveBookSectionID (PK)
InteractiveBookID (FK)
BookSectionName
OutlineLevel
Sequence
...


Pretty simple. The issue is on the code-side (this is something that I cannot change because it is this way throughout our system, and this change would be a huge project).

The classes that contain the Interactive Book and Section information are as follows (in pseudo code without the details):


Code:
class oInteractiveBook
{
    private oInteractiveBookSections bookSections;
    // ... All info from InteractiveBooks table ...
}

class oInteractiveBookSections
{
    private Collection<oInteractiveBookSection> bookSectionCollection;
}

class oInteractiveBookSection
{
    // ... All info from InteractiveBookSections table ...
}



One attempted solution:

I tried creating the hbm.xml files as such:


Code:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Make sure to set the build action for this file to "Embedded Resource" -->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="field.camelcase">
  <class name="oInteractiveBook, BusinessTypes" table="InteractiveBooks" lazy="false">
   
    <id name="MlInteractiveBookID" column="InteractiveBookID" type="long">
      <generator class="native"/>
    </id>

    <property name="MsInteractiveBookName" column="InteractiveBookName" type="String(100)"/>
    <property name="MbPublic" column="Public" type="Boolean"/>

    ...

    <bag name="MoBookSections" table="InteractiveBookSections" lazy="false">
      <key column="InteractiveBookID"/>
      <one-to-many class="oInteractiveBookSection, BusinessTypes"/>
    </bag>


  </class>
</hibernate-mapping>


- and -


Code:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Make sure to set the build action for this file to "Embedded Resource" -->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="field.camelcase">
  <class name="oInteractiveBookSection, BusinessTypes" table="InteractiveBookSections" lazy="false">
   
    <id name="MlInteractiveBookSectionID" column="InteractiveBookSectionID" type="long">
      <generator class="native"/>
    </id>

    <property name="MlInteractiveBookID" column="InteractiveBookID" type="long"/>

    ...

    <property name="msDescription" column="Description" type="String(300)"/>

  </class>
</hibernate-mapping>


However, this does not work. I get the following error:


Code:
NHibernate.PropertyNotFoundException : Could not find field 'moBookSections' in class 'oInteractiveBook'


Obviously NHibernate needs to populate the oInteractiveBookSections object here too, and I am not telling it how to with these mapping files. The problem is, I cannot figure out how. Anyone have any idea what I should do?

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2008 3:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try this:

Code:
<component name="BookSections" class="...oInteractiveBookSections ">
    <bag name="MoBookSections" table="InteractiveBookSections" lazy="false">
      <key column="InteractiveBookID"/>
      <one-to-many class="oInteractiveBookSection, BusinessTypes"/>
    </bag>
</component>


But for the collection, you need to change the field/property to ICollection<oInteractiveBookSection> because hibernate uses its own list/collection implementation.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Worked
PostPosted: Fri Nov 07, 2008 11:39 am 
Newbie

Joined: Fri Aug 22, 2008 4:07 pm
Posts: 9
That worked great thanks. I don't know why I didn't think of combining component and bag like that.


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.