-->
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.  [ 12 posts ] 
Author Message
 Post subject: Mapping question - joined subclasses
PostPosted: Tue Apr 25, 2006 6:02 pm 
Newbie

Joined: Tue Apr 25, 2006 5:20 pm
Posts: 17
I am new to nHibernate.

I have an existing schema in which I am trying to development a object model for. My situation is this:

I have a 3 tables: Table=Party, Table2=Person, Table3=Organization
Person maps 1:1 with Party and Organization maps 1:1 with Party. A party can be both a Person and an Organization. I haven't decided if this is important. I haven't determined a time where I would need both classes at the same time.

Party never exists on its own. Party controls the generation of the PK via an Identity column.

How would I go about mapping something like this?

Pardon my ignorance :)
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 9:41 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
http://www.hibernate.org/hib_docs/nhibe ... edsubclass

not knowing anything about the comonalities between the three classes your mapping file would look something like:
Code:
<class name="AbstractParty">
  <id name="id" column="PartyID">
    <generator class="identity" />
  </id>

  <property name="Name" column="PartyName" />

  <joined-subclass name="Person" table="Person">
    <key column="PersonID" />
    <property name="Age" column="Age" />
    <bag name="Pets" inverse=true />
      <key column="PersonID" />
      <one-to-many class="Pet"
    </bag>
  </joined-subclass>

  <joined-subclass name="Organization" table="Organization">
    <key column="OrganizationID" />
    <property name="DateFounded" column="DateFounded" />
    <bag name="ClosetSkeletons" inverse=true />
      <key column="OrganizationID" />
      <one-to-many class="ClosetSkeleton"
    </bag>
  </joined-subclass>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 11:15 pm 
Newbie

Joined: Tue Apr 25, 2006 5:20 pm
Posts: 17
Thanks. I think thats similar to what I created first try.

Essentially all 3 tables use PartyID as their Primary Key.

Second part of my questions is how would I go about getting Hibernate to return objects of type Person or Organization?

Thanks,

Christian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 11:30 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Quote:
Essentially all 3 tables use PartyID as their Primary Key.


yes, PK/FK constraint with the FK being a PK in the subclass tables.

Quote:
how would I go about getting Hibernate to return objects of type Person or Organization?


Code:
public IList GetAllOrganizations() {
  return this.session.CreateCriteria(typeof(Organization));
}


that's the easy way. but you should probably create some generic method such that you could call something like:

Code:
MyHibernateSessionWrapper.GetAll(typeof(Organization))


because you could use the same method for:

Code:
MyHibernateSessionWrapper.GetAll(typeof(Person))


or, if you wanted a polymorphic collection:

Code:
MyHibernateSessionWrapper.GetAll(typeof(Party));


fun. for a good look at how someone else has done these things, check out the cuyahoga application. it is listed in the "Example Apps" documentation for NH.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 11:18 am 
Newbie

Joined: Tue Apr 25, 2006 5:20 pm
Posts: 17
Thanks devonl. I have a mapping file similar to what you presented and Party as an abstract class.

How would I go about getting a list of People with a certain last name?

The following line throws an ADOException:
Code:
Dim people As IList = session.Find("from Person as pn where pn.LastName = ? or pn.LastName = ?", _
                                        New Object() {"jones", "smith"}, _
                                        New IType() {NHibernateUtil.String, NHibernateUtil.String})


Am I doing something incorrectly?

edit: here is the actual error:

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

Additional information: Could not execute query)


Christian


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 10:18 am 
Newbie

Joined: Tue Apr 25, 2006 5:20 pm
Posts: 17
I still haven't fixed this. I have 3 clasess Party, organization and person. Party is abstract and both person and organization inherit from it. here is my mapping file. party.hbm.xml ( i should name it the parent class correct?)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="Demo" assembly="Demo">
<class name="Party" table="Party" >
   <id name="Id" column="PartyId" type="Int32">
      <generator class="identity"/>
   </id>
   <property name="ActiveDate" column="FromDate" type="Date"/>
   <property name="EndDate" column="ThruDate" type="Date"/>
   <property name="Language" column="Language" type="Date"/>
    <joined-subclass name="Person" table="Person" >
      <key column="PartyID"/>
         <property name="FirstName" type="String"/>
         <property name="LastName" type="String"/>
     </joined-subclass>
   <joined-subclass name="Organization" table="Organization" >
      <key column="PartyID"/>
         <property name="Name" type="String"/>
   </joined-subclass>
   </class>
</hibernate-mapping>


I am getting the error in the previous post.

Thanks,

Christain


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 1:04 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Christian,

Sorry for the long disconnect, I was out on business all last week. Unfortunately, I don't know VB well enough to diagnose your specific code, however, if I were to do this in C# I would use the session.CreateQuery() method:

Code:
public IList GetSomePeople (string lastName1, string lastName2) {
    return session.CreateQuery("from Person pn where pn.LastName = :lastName1 or pn.LaseName = :lastName2")
        .SetEntity("lastName1", lastName1)
        .SetEntity("lastName2", lastName2)
        .AddOrder(Order.Asc("LastName"))
        .List();
}


Hopefully this will help you, or perhaps someone with better VB skills can take a look.

Quote:
here is my mapping file. party.hbm.xml ( i should name it the parent class correct?)

Yes, your mapping file looks right and you should name it according to the abstract (base) class.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 1:29 pm 
Newbie

Joined: Tue Apr 25, 2006 5:20 pm
Posts: 17
C# is my "native" language. VB is what they use at my current job.

Thanks, I'll try this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 1:37 pm 
Newbie

Joined: Tue Apr 25, 2006 5:20 pm
Posts: 17
Tried what you suggested and I am getting the identical error i got previously. ANy ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 1:46 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
hmmm,

probably the best thing to do at this point would be to enable debugging and take a look at the sql query NH is generating. If you're not familiar with this, check out the doco (http://www.hibernate.org/hib_docs/nhibe ... on-logging)

An example of my config. I have multiple loggers for various namespaces:

Code:
<log4net>
   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="C:\\VFOLogs\\log.txt" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyy.MM.dd" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout,log4net">
         <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
   </appender>
   <!--
      1. ALL
      2. DEBUG
      3. INFO
      4. WARN
      5. ERROR
      6. FATAL
      7. OFF
   -->
   <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
   </root>
   <logger name="AMA.Core">
      <level value="ERROR" />
   </logger>
   <logger name="NHibernate">
      <level value="DEBUG" />
   </logger>
   <logger name="VFO.Core">
      <level value="ERROR" />
   </logger>
   <logger name="VFO.Web">
      <level value="ERROR" />
   </logger>
</log4net>


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 2:08 pm 
Newbie

Joined: Tue Apr 25, 2006 5:20 pm
Posts: 17
EDIT: I have it working using the original Find instead of CreateQuery. Most likely jsut a syntax problem porting your code to vb.net


Last edited by ChristianD on Mon May 08, 2006 2:43 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 2:38 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
make sure the ASP.NET machine account has modify, and write permissions on that \temp folder.

Also, for kicks, please post your class files for the three classes in question if you can. i'm thinking we might have a class -> mapping issue. keep working on that debug feature :)

-devon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.