-->
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.  [ 5 posts ] 
Author Message
 Post subject: Subclass problem
PostPosted: Tue Sep 06, 2005 3:07 am 
Newbie

Joined: Mon Jun 27, 2005 5:12 am
Posts: 10
Hi

I using 0.9.0.0 build of NHibernate and I'm getting the following error when attempting to retrieve a list of 'File' type objects. The subclassed, 'Image', type objects are in contast retrieved correctly.

Here's my mapping file:

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="HardBopRecords.Models.Files.File,HardBopRecords.Models"
      table="`File`"
      discriminator-value="Unknown">
      <meta attribute="class-description"></meta>
      <jcs-cache usage="read-write"/>
      <id name="_FileId" column="FileId" type="Guid" unsaved-value="{00000000-0000-0000-0000-000000000000}">
         <meta attribute="field-description"></meta>
         <generator class="guid.comb"/>
      </id>
      <discriminator column="Type" />
      <property name="_Name" column="Name" type="String" length="255">
         <meta attribute="field-description"></meta>
      </property>
      <property name="_Description" column="Description" type="String" length="512">
         <meta attribute="field-description"></meta>
      </property>
      <property name="_Size" column="Size" type="Int32">
         <meta attribute="field-description"></meta>
      </property>
      <property name="_Path" column="Path" type="String" length="1024">
         <meta attribute="field-description"></meta>
      </property>
      <many-to-one name="_FileCategory" class="HardBopRecords.Models.FileCategorys.FileCategory,HardBopRecords.Models" column="FileCategoryId"  />
      <subclass name="HardBopRecords.Models.Images.Image,HardBopRecords.Models" discriminator-value="Image"/>
   </class>
</hibernate-mapping>



I profiled the generated sql and noticed that the sql is correct for the subclassed, Image, type in that I get the following:

Code:
SELECT this.FileId as FileId1_, this.Size as Size1_, this.Name as Name1_, this.Description as Descript4_1_, this.Path as Path1_, this.FileCategoryId as FileCate7_1_, filecate1_.FileCategoryId as FileCate1_0_, filecate1_.Name as Name0_, filecate1_.Description as Descript3_0_, filecate1_.CanBeDeleted as CanBeDel4_0_ FROM [File] this left outer join [FileCategory] filecate1_ on this.FileCategoryId=filecate1_.FileCategoryId WHERE 1=1 and this.Type='Image'


notice the ...and this.Type='Image' on the end

However, the other type, File, results in the following sql:

Code:
select top 10 this.fileid as fileid1_, this.type as type1_, this.size as size1_, this.name as name1_, this.description as descript4_1_, this.path as path1_, this.filecategoryid as filecate7_1_, filecate1_.filecategoryid as filecate1_0_, filecate1_.name as name0_, filecate1_.description as descript3_0_, filecate1_.canbedeleted as canbedel4_0_ from [file] this left outer join [filecategory] filecate1_ on this.filecategoryid=filecate1_.filecategoryid where 1=1


notice the absence of the descrimintor where clause.

I am doing something wrong, or is this a bug, anyone have any ideas?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 7:58 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
I don't see anything wrong here...

Don't forget that "Image" IS a "File" so when you ask NHibernate to load all the files, it will also load the images.

Do you get an exception somewhere ?

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 9:00 am 
Newbie

Joined: Mon Jun 27, 2005 5:12 am
Posts: 10
ahh, my mistake.

What I intended to do was use a single table to store two classes, using the discriminator to differentiate between them. I didn't intend that the File type would be the base class, there is no relationship between the classes in my domain objects. I was getting the following exception:

Code:
Object with id: 5677f3cb-3b83-4fa7-b520-9691011b0ecb was not of the specified sublcass: HardBopRecords.Models.Files.File (Discriminator: File)



Is it possible to map a single table to two unrelated entity types using a disciminator?


Top
 Profile  
 
 Post subject: Hmmm
PostPosted: Tue Sep 06, 2005 12:40 pm 
Newbie

Joined: Mon Jun 27, 2005 5:12 am
Posts: 10
Ok, I've converted my Image concrete class to be derived from my File concrete class.

Now, as before, retrieving a list of Image class objects works fine, but I still have the same issue as described above with retrieving a list of File objects from the database, that is:

Code:
Object with id: 5677f3cb-3b83-4fa7-b520-9691011b0ecb was not of the specified sublcass: HardBopRecords.Models.Files.File (Discriminator: File)


Where the object with id: 5677f3cb-3b83-4fa7-b520-9691011b0ecb is an Image object, with "Image" as its discriminator column type.

I understand that as File is the base class I'm getting both Image (subclass) and File (base class) objects returned from the query. So, how do write my Hql so that hibernate correctly creates Image and File objects?


Top
 Profile  
 
 Post subject: Found the cause of my problem
PostPosted: Wed Sep 07, 2005 4:39 am 
Newbie

Joined: Mon Jun 27, 2005 5:12 am
Posts: 10
Ok, after a bit of debugging I've got to the route of my problem.

I had a class called File and a subclass called Image.

Both File and Image are described in my hbm.xml definitions as follows:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="HardBopRecords.Models.Files.File,HardBopRecords.Models"
      table="`File`"
      >
      <!-- In the above dont bother specifying descrimintaor value as defaults to class name-->
      <meta attribute="class-description"></meta>
      <jcs-cache usage="read-write"/>
      <id name="_FileId" column="FileId" type="Guid" unsaved-value="{00000000-0000-0000-0000-000000000000}">
         <meta attribute="field-description"></meta>
         <generator class="guid.comb"/>
      </id>
      <discriminator column="Type" type="string"/>
      <property name="_Name" column="Name" type="String" length="255">
         <meta attribute="field-description"></meta>
      </property>
      <property name="_Description" column="Description" type="String" length="512">
         <meta attribute="field-description"></meta>
      </property>
      <property name="_Size" column="Size" type="Int32">
         <meta attribute="field-description"></meta>
      </property>
      <property name="_Path" column="Path" type="String" length="1024">
         <meta attribute="field-description"></meta>
      </property>
      <many-to-one name="_FileCategory" class="HardBopRecords.Models.FileCategorys.FileCategory,HardBopRecords.Models" column="FileCategoryId"  />
      <subclass name="HardBopRecords.Models.Images.Image,HardBopRecords.Models" discriminator-value="Image"/>
   </class>
</hibernate-mapping>



What I had negelected to mention was that the File class itself has a base class, FileBase, which I had not supplied a mapping for (as it's never going to be accessed through hibernate and is a by product of my technique of auto generating / updating classes using MyGeneration).

I have now added a mapping file for this base class and changed the above mapping file to identify File as a subclass. Everthing now works fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.