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: Simple seeming Subclass Problem
PostPosted: Sun Sep 10, 2006 3:02 pm 
Newbie

Joined: Wed Sep 06, 2006 9:39 am
Posts: 5
Location: London
Hi,

I have a Category class with a couple of collections of Properties associated with it; Read Only and Default. I've saved all these properties in one table and differentiated between them using a discriminator.

I think I should be able to define a couple of bags on Category and just specify which class they should come from and NHibernate would give me back my collections based on the discriminator.

However I have had to define where clauses on my bags. So yes it works; I'm not hugely worried about it, but it'd be really good to know if I've done my mapping incorrectly.

Thanks in advance,

Brett.

Hibernate version: 1.2 Beta

Mapping documents:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo">
  <class name="Domain.DefaultProperty, Core"
         table="nrp_CategoryDefault" discriminator-value="0" >
    <id name="ID" type="Int32" column="CategoryDefaultID" unsaved-value="0">
      <generator class="identity" />
    </id>
    <discriminator column="ReadOnly" type="Char" />
    <many-to-one name="ForCategory" column="CategoryID" class="Domain.Category, Core" />
    <many-to-one name="OfType" column="PropertyID" class="Domain.Property, Core" />
    <property name="Value" column="PropertyValue" />
    <subclass name="Domain.ReadOnlyDefaultProperty, Core" discriminator-value="1">
    </subclass>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo">
  <class name="Domain.Category, Core" table="nrp_Category">
    <id name="ID" type="Int32" column="CategoryID" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="Name" column="CategoryName" type="string" />
    <bag name="DefaultProperties" inverse="true" lazy="true" where="ReadOnly='0'">
      <key column="CategoryID" />
      <one-to-many class="Domain.DefaultProperty, Core" />
    </bag>
    <bag name="ReadOnlyProperties" inverse="true" lazy="true"  where="ReadOnly='1'">
      <key column="CategoryID" />
      <one-to-many class="Domain.ReadOnlyDefaultProperty, Core" />
    </bag>
  </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs: None
No exceptions.
Name and version of the database you are using: SQL Server 2000

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 10:55 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
My first thot on this is that this is due to the polymorphism NHIbernate recognizes since ReadOnlyDefaultProperty *is* a DefaultProperty. You probably were ending up with ALL the entities inside the DefaultProperties collection, but getting only the ReadOnlyDefaultProperty entities inside your ReadOnlyProperties collection right?
Can you just define the 'class' element on a common supertype and then declare both DefaultProperty and ReadOnlyDefaultProperty as subclasses? I know this changes the Domain a bit, but that is the only I can think of around this.

MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 13, 2006 10:35 am 
Newbie

Joined: Wed Sep 06, 2006 9:39 am
Posts: 5
Location: London
OK - I think those are really good thoughts. I tried changing my mapping to use a base class and defined DefaultProperty as a subclass exactly the same as ReadOnlyDefaultProperty, only with a different discriminator.

When I remove my where clauses from the bags on category I get the following error:

2006-09-13 15:27:10,143 [TestRunnerThread] ERROR NHibernate.LazyInitializationException [(null)] &lt;(null)&gt; - cannot access loading collection
NHibernate.LazyInitializationException: cannot access loading collection

(Copied from my log4net log file which I guess I haven't configured quite right either).

I'm going to stick with my where clauses because they work.


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.