-->
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: Free cookie for a prompt answer: Cascade and inheritence.
PostPosted: Mon Feb 18, 2008 7:43 pm 
Newbie

Joined: Mon Feb 18, 2008 7:15 pm
Posts: 2
I know somewhere I've overlooked something simple, but my eyes grow weary and I thought I would throw this out to the general populous, to mock me as to the simplicity of this problem.

So mocking aside:

Content is a generic content class, containing some simple objects to handle chunks of web related content (blog post, image in Flickr, etc). There are different contents for different types of sources. In this case, FlickrContent relates to content from Flickr (surprisingly) and inherits from Content.

Source contains a IList of Content. There are different sources for different types of data. So FlickrSource relates to a source from Flickr which still contains a List of Content.

The bit causing all the trouble is this:
Code:
                    Content searchContent = new FlickrContent(photoDoc);
                    searchContent.Source = this;

                    if (!Content.Contains(searchContent))
                        Content.Add(searchContent);


Content is of type Content. Apologies for the slightly ambiguous property naming, it's a headdesk moment that will be rectified on the first code clean-up.

The mapping file for content looks like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="PlanetoidAPI.Objects" assembly="PlanetoidAPI">

  <class name="PlanetoidAPI.Objects.Content" table="PL_CONTENT">
    <id name="Id">
      <column name="PL_CN_ID" sql-type="int" not-null="true" />
      <generator class="identity" />
    </id>

    <property ... />

    <many-to-one name="Source" class="PlanetoidAPI.Objects.Source" column="PL_CN_SC_ID" not-null="true" />
  </class>
</hibernate-mapping>


and the mapping file for source looks like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="PlanetoidAPI.Objects" assembly="PlanetoidAPI">

  <class name="PlanetoidAPI.Objects.Source" table="PL_SOURCE">

    <!-- A 32 hex character is our surrogate key. It's automatically
            generated by NHibernate with the UUID pattern. -->
    <id name="Id">
      <column name="PL_SC_ID" sql-type="int" not-null="true" />
      <generator class="identity" />
    </id>
   
    <discriminator column="PL_SC_TYPE" type="string" />
   
    ...

    <bag name="Content" inverse="true" cascade="all">
      <key column="PL_CN_SC_ID"/>
      <one-to-many class="PlanetoidAPI.Objects.Content"/>
    </bag>

    <subclass name="RssSource" discriminator-value="Rss" />
    <subclass name="FlickrSource" discriminator-value="Flickr" />
   
  </class>

</hibernate-mapping>


So what we've got here folks is a pretty straightforward parent child bi-directional relationship. Everything is fine and dandy if you want to add objects of type Content to the Content collection, but if I attempt to add an object of type FlickrContent to the Content collection (that really is a terrible name) and then update that back to the database, we get:

Unknown entity class: PlanetoidAPI.Objects.FlickrContent

which in one sense is fair do, FlickrContent is not explicitly specified, but shouldn't it fall back to the mapping for type Content instead? If I am completely misreading this, is there a way to force this behaviour? I'm trying to avoid having to add every derivative of Content as some kind of subclass.

Many Thanks,

Kian Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 12:23 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Is your subject off, or was there something in there about cascades that I missed?

At any rate, you've hit the nail on the head: you can't persist a subclass without mapping it.

At least, I don't think you can. Try this: add a discriminator column to your Content mapping, and let it default to class name. It might just pick it up, but I have an inkling that it will not.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 4:30 am 
Newbie

Joined: Mon Feb 18, 2008 7:15 pm
Posts: 2
Well, the problem appeared to be when the update was cascading to the Content collection, and falling over with FlickrContent.

I'll do explicit mapping for now, but if anybody else has any more ideas, I would be welcome to hear them.

Thanks.


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.