Exception or null expected when wrong subclass is selected

Description

Let's imagine you have three classes, Fruit, Apple and Orange. Apple and Orage are Fruits (subclasses). We have an Apple with an Id of 1 and an Orange with an Id of 2.
If you try to load id 1 with .Load(typeof(Orange), 1) you will get an Orange instead of null or some casting exception, and some fields of that Orange will contain null values...

Environment

None

Attachments

1

Activity

Show:

Former user 
June 2, 2005 at 2:57 PM

This should have been fixed together with NH-295.

Former user 
May 12, 2005 at 10:29 AM

This should be fixed by now, need to check it and write a test.

Peter Smulovics 
September 23, 2004 at 5:49 AM

There is an exception called WrongClassException, which:
"Thrown when ISession.Load() selects a row with the given primary key (identifier value) but the row's discriminator value specifies a different subclass from the one requested"
This is not thrown by the java code, and not thrown by the .NET code - this may mean, that there is a bug in the java code as well.

MikeM 
September 23, 2004 at 5:20 AM

Peter, here's the java code I used to test your scenario with hibernate. I'm pretty sure it duplicates exactly what your psuedo code has (minus diff with variable names).

Mike

Peter Smulovics 
September 23, 2004 at 4:27 AM

Maybe you misunderstood the problem, I try to give a more understandable example (it's just pseudocode, maybe it doesn't compile):

class Fruit {int id; public int Id {get {return id} set {id=value}}

class Orange : Fruit { int variable; public int Variable {get {return variable} set {variable = value;}}

class Apple : Fruit { string variable; public string Variable {get {return variable} set {variable = value;}}

I have a mapping for it with the two subclasses.

Orange o = new Orange; o.Variable = 3; session.Save(o);
Apple a = Session.Load(typeof(Apple), o.Id);

At the end, I have an apple 'a' - but I shouldn't. And I cannot determine, what will a.Variable contain... sometimes it throws exception, sometimes it tries to convert the data...
Same applies, if the Orange's and Apple's variable does not have the same name...

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created July 26, 2004 at 5:55 AM
Updated June 2, 2005 at 2:57 PM
Resolved June 2, 2005 at 2:57 PM
Who's Looking?