You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Robert Zeigler <ro...@scazdl.org> on 2009/04/24 01:17:11 UTC

abstract entities, inheritance, etc.

So, I was playing around a bit with the "is abstract" flag on abstract  
entities.
I confess that it didn't quite work the way I thought it would.
For one, the superclass was abstract, but not the subclass.  I went  
ahead and made the subclass abstract.  Which raises the second issue.   
I have two obj entities entities extending the "abstract" superclass,  
using a single discriminator column.  I went to query the base class  
(ala: select e from Entry e), and the query failed, due to the  
inability to instantiate an instance of Entry.  Evidently, cayenne is  
trying to instantiate all of the subclasses instances as instances of  
the superclass.  Shouldn't cayenne be instantiating the subclasses?  
Put another way, what is the technical reason that cayenne /isn't/  
instantiating the subclasses as instances of the subclasses? Given the  
potential of polymorphism, it seems like instantiating as the  
superclass, even if the superclass is concrete, is incorrect behavior?  
Just trying to clarify, here.  I'll be digging through the code to try  
to understand the specifics of how cayenne handles inheritance, but,  
any pointers appreciated.

Robert

Re: abstract entities, inheritance, etc.

Posted by Andrus Adamchik <an...@objectstyle.org>.
>
> that raises the question of whether we support enum-mapped  
> properties as the discriminators, and if so, what's the proper  
> syntax for specifying the enum constant in the qualifier field?

We should. This comes down to adding enum literals support in the  
"exp" package. Maybe:

xyz = org.example.EnumClass.ENUM_NAME

This is also seems to be the JPA (EJBQL) syntax for enum literals.

Andrus

On Apr 24, 2009, at 9:51 AM, Robert Zeigler wrote:

> This was with a SelectQuery already.
>
> Just verified that you're right: this is a problem with my qualifiers.
>
> The column type is varchar.  I was trying to map the object property  
> as an enum.  But I wasn't sure how to specify the qualifier, so I  
> tried as:
>
> entryType = 'ENUM_CONST_NAME'
>
> Which failed.  When I changed the java type of entryType to string,  
> cayenne properly instantiated the subclasses.
> So I suppose that raises the question of whether we support enum- 
> mapped properties as the discriminators, and if so, what's the  
> proper syntax for specifying the enum constant in the qualifier field?
>
> Robert
>
> On Apr 24, 2009, at 4/241:23 AM , Andrus Adamchik wrote:
>
>>
>> On Apr 24, 2009, at 5:35 AM, Robert Zeigler wrote:
>>
>>> No, it's a bit more complicated.  The template is one portion of  
>>> the fix, but even if the entity is marked abstract (in both the  
>>> mapping and the java class), cayenne will try to instantiate it in  
>>> the aforementioned query on superclass case.  So a complete  
>>> solution will require that case to resolve and instantiate the  
>>> subclass types, rather than attempting to instantiate the  
>>> superclass type.
>>>
>>> Robert
>>
>> Hi Robert,
>>
>> The whole logic for instantiating an object of a given class is  
>> based on the combination of entity qualifiers in the hierarchy  
>> (including on the base class).  So unless this is a bug specific to  
>> EJBQLQuery (could you try the same thing with SelectQuery?), maybe  
>> the condition on a given result set row yielded a base class somehow?
>>
>> Could you post the stack trace and entity qualifiers?
>>
>> Thanks,
>> Andrus
>>
>
>


Re: abstract entities, inheritance, etc.

Posted by Robert Zeigler <ro...@roxanemy.com>.
This was with a SelectQuery already.

Just verified that you're right: this is a problem with my qualifiers.

The column type is varchar.  I was trying to map the object property  
as an enum.  But I wasn't sure how to specify the qualifier, so I  
tried as:

entryType = 'ENUM_CONST_NAME'

Which failed.  When I changed the java type of entryType to string,  
cayenne properly instantiated the subclasses.
So I suppose that raises the question of whether we support enum- 
mapped properties as the discriminators, and if so, what's the proper  
syntax for specifying the enum constant in the qualifier field?

Robert

On Apr 24, 2009, at 4/241:23 AM , Andrus Adamchik wrote:

>
> On Apr 24, 2009, at 5:35 AM, Robert Zeigler wrote:
>
>> No, it's a bit more complicated.  The template is one portion of  
>> the fix, but even if the entity is marked abstract (in both the  
>> mapping and the java class), cayenne will try to instantiate it in  
>> the aforementioned query on superclass case.  So a complete  
>> solution will require that case to resolve and instantiate the  
>> subclass types, rather than attempting to instantiate the  
>> superclass type.
>>
>> Robert
>
> Hi Robert,
>
> The whole logic for instantiating an object of a given class is  
> based on the combination of entity qualifiers in the hierarchy  
> (including on the base class).  So unless this is a bug specific to  
> EJBQLQuery (could you try the same thing with SelectQuery?), maybe  
> the condition on a given result set row yielded a base class somehow?
>
> Could you post the stack trace and entity qualifiers?
>
> Thanks,
> Andrus
>


Re: abstract entities, inheritance, etc.

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Apr 24, 2009, at 5:35 AM, Robert Zeigler wrote:

> No, it's a bit more complicated.  The template is one portion of the  
> fix, but even if the entity is marked abstract (in both the mapping  
> and the java class), cayenne will try to instantiate it in the  
> aforementioned query on superclass case.  So a complete solution  
> will require that case to resolve and instantiate the subclass  
> types, rather than attempting to instantiate the superclass type.
>
> Robert

Hi Robert,

The whole logic for instantiating an object of a given class is based  
on the combination of entity qualifiers in the hierarchy (including on  
the base class).  So unless this is a bug specific to EJBQLQuery  
(could you try the same thing with SelectQuery?), maybe the condition  
on a given result set row yielded a base class somehow?

Could you post the stack trace and entity qualifiers?

Thanks,
Andrus


Re: abstract entities, inheritance, etc.

Posted by Robert Zeigler <ro...@roxanemy.com>.
No, it's a bit more complicated.  The template is one portion of the  
fix, but even if the entity is marked abstract (in both the mapping  
and the java class), cayenne will try to instantiate it in the  
aforementioned query on superclass case.  So a complete solution will  
require that case to resolve and instantiate the subclass types,  
rather than attempting to instantiate the superclass type.

Robert

On Apr 23, 2009, at 4/238:53 PM , Aristedes Maniatis wrote:

>
> On 24/04/2009, at 9:53 AM, Robert Zeigler wrote:
>
>> I expected the Entry subclass of _Entry, and superclass of the  
>> other two concrete classes to be abstract.  Instead, only the  
>> cayenne-"managed" superclass was abstract.
>> It seems like there's very little point to that: nobody is going to  
>> try to directly instantiate _Entry, anyway.  And I can't put  
>> abstract methods that subclasses should implement into _Entry,  
>> since they'll be lost on class regeneration.  I guess it's a matter  
>> of expectations: by checking "isabstract", I expect cayenne to  
>> treat that object entity as abstract... and not try to instantiate  
>> it. :)
>
> Is this fix as simple as changing the DOtemplate for the subclass?  
> Now I understand your point and I think you are right that it is  
> broken, but the fix should be one line in the template.
>
> To some degree it might make sense for the _Entry superclass to be  
> abstract always to avoid people shooting themselves in the foot and  
> using the wrong class by mistake.
>
> Ari
>
>
>
> -------------------------->
> ish
> http://www.ish.com.au
> Level 1, 30 Wilson Street Newtown 2042 Australia
> phone +61 2 9550 5001   fax +61 2 9550 4001
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>
>


Re: abstract entities, inheritance, etc.

Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 24/04/2009, at 9:53 AM, Robert Zeigler wrote:

> I expected the Entry subclass of _Entry, and superclass of the other  
> two concrete classes to be abstract.  Instead, only the  
> cayenne-"managed" superclass was abstract.
> It seems like there's very little point to that: nobody is going to  
> try to directly instantiate _Entry, anyway.  And I can't put  
> abstract methods that subclasses should implement into _Entry, since  
> they'll be lost on class regeneration.  I guess it's a matter of  
> expectations: by checking "isabstract", I expect cayenne to treat  
> that object entity as abstract... and not try to instantiate it. :)

Is this fix as simple as changing the DOtemplate for the subclass? Now  
I understand your point and I think you are right that it is broken,  
but the fix should be one line in the template.

To some degree it might make sense for the _Entry superclass to be  
abstract always to avoid people shooting themselves in the foot and  
using the wrong class by mistake.

Ari



-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A



Re: abstract entities, inheritance, etc.

Posted by Robert Zeigler <ro...@roxanemy.com>.
On Apr 23, 2009, at 4/236:32 PM , Aristedes Maniatis wrote:

>
> On 24/04/2009, at 9:17 AM, Robert Zeigler wrote:
>
>> So, I was playing around a bit with the "is abstract" flag on  
>> abstract entities.
>> I confess that it didn't quite work the way I thought it would.
>> For one, the superclass was abstract, but not the subclass.
>
> That is the common situation when you want horizontal inheritance. [1]

Right, which is why I'm surprised at the current generated classes,  
but I need to be more specific.

I have:

                             Entry (abstract)
ProjectNote (concrete)  ProtocolRun(concrete)

So I checked the "isabstract" for Entity. The result of class  
generation was:

in package xxx.auto:
public abstract class _Entry{}
public class _ProjectNote{}
public class _ProtocolRun{}

in package xxx:
public class Entry{} //<-- this is what I found surprising
public class ProjectNote{}
public class ProtocolRun{}

I expected the Entry subclass of _Entry, and superclass of the other  
two concrete classes to be abstract.  Instead, only the  
cayenne-"managed" superclass was abstract.
It seems like there's very little point to that: nobody is going to  
try to directly instantiate _Entry, anyway.  And I can't put abstract  
methods that subclasses should implement into _Entry, since they'll be  
lost on class regeneration.  I guess it's a matter of expectations: by  
checking "isabstract", I expect cayenne to treat that object entity as  
abstract... and not try to instantiate it. :)

>
>> I went ahead and made the subclass abstract.  Which raises the  
>> second issue.  I have two obj entities entities extending the  
>> "abstract" superclass, using a single discriminator column.  I went  
>> to query the base class (ala: select e from Entry e), and the query  
>> failed, due to the inability to instantiate an instance of Entry.   
>> Evidently, cayenne is trying to instantiate all of the subclasses  
>> instances as instances of the superclass.  Shouldn't cayenne be  
>> instantiating the subclasses?
>
> EOF has a switch for this called something like 'deep object  
> resolution'. That is, when you query Artist.class, should you get  
> back a list of Painter.class, Photographer.class, etc object (as you  
> are expecting), or get back a list of Artist objects. Each can be  
> useful in different situations, particularly since just returning  
> Artist objects means less work for the database (fewer JOINs).
>
> Obviously if your query was for Painter.class then you will only get  
> Painter objects back.
>
>> Put another way, what is the technical reason that cayenne /isn't/  
>> instantiating the subclasses as instances of the subclasses? Given  
>> the potential of polymorphism, it seems like instantiating as the  
>> superclass, even if the superclass is concrete, is incorrect  
>> behavior? Just trying to clarify, here.  I'll be digging through  
>> the code to try to understand the specifics of how cayenne handles  
>> inheritance, but, any pointers appreciated.
>
> I think ultimately we need both behaviours.
>

Fair enough that, for performance reasons, you might want only the  
superclass.
But then... if you're using horizontal inheritance ("table per  
concrete class"), for example, you already need to fetch from both  
tables if you perform a query for the superclass; I'm trying to think  
of an instance when you would be performing a superclass query and / 
not/ have the type information at hand... ok, vertical inheritance,  
you might not have the type information. :)  In terms of additional  
joins to fetch additional information... we already have lazy loading  
of relationships; it seems like it should be possible to lazy-load the  
attributes, as well, at least for queries on the superclass, saving  
the hassle of additional joins... I think the only place where you  
would have additional joins is vertical inheritance, anyway... and, I  
suppose, if a subclass has a flattened property not present in the  
parent class.

Anyway, not opposed to keeping the option of the current behavior, but  
definitely +1 for resolving to the subclass level.  It'll be necessary  
to properly support horizontal inheritance.

Robert

Re: abstract entities, inheritance, etc.

Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 24/04/2009, at 9:17 AM, Robert Zeigler wrote:

> So, I was playing around a bit with the "is abstract" flag on  
> abstract entities.
> I confess that it didn't quite work the way I thought it would.
> For one, the superclass was abstract, but not the subclass.

That is the common situation when you want horizontal inheritance. [1]

>  I went ahead and made the subclass abstract.  Which raises the  
> second issue.  I have two obj entities entities extending the  
> "abstract" superclass, using a single discriminator column.  I went  
> to query the base class (ala: select e from Entry e), and the query  
> failed, due to the inability to instantiate an instance of Entry.   
> Evidently, cayenne is trying to instantiate all of the subclasses  
> instances as instances of the superclass.  Shouldn't cayenne be  
> instantiating the subclasses?

EOF has a switch for this called something like 'deep object  
resolution'. That is, when you query Artist.class, should you get back  
a list of Painter.class, Photographer.class, etc object (as you are  
expecting), or get back a list of Artist objects. Each can be useful  
in different situations, particularly since just returning Artist  
objects means less work for the database (fewer JOINs).

Obviously if your query was for Painter.class then you will only get  
Painter objects back.

> Put another way, what is the technical reason that cayenne /isn't/  
> instantiating the subclasses as instances of the subclasses? Given  
> the potential of polymorphism, it seems like instantiating as the  
> superclass, even if the superclass is concrete, is incorrect  
> behavior? Just trying to clarify, here.  I'll be digging through the  
> code to try to understand the specifics of how cayenne handles  
> inheritance, but, any pointers appreciated.

I think ultimately we need both behaviours.


> Robert



Ari


[1] http://cayenne.apache.org/doc/inheritance-overview.html

-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A



Re: abstract entities, inheritance, etc.

Posted by Robert Zeigler <ro...@roxanemy.com>.
Hm, good question.  THis was long enough ago that I don't remember. I'll see if I can dig up the relevant code to see what I did.

Robert

On Apr 8, 2012, at 4/88:44 AM , Michael Gentry wrote:

> Hi Robert,
> 
> To resurrect a really old thread, I was wondering if you ever found a
> solution or workaround to this problem?  I was helping another project
> at work on Friday that hit the same issue.
> 
> Thanks,
> 
> mrg
> 
> 
> On Thu, Apr 23, 2009 at 7:17 PM, Robert Zeigler <ro...@scazdl.org> wrote:
>> So, I was playing around a bit with the "is abstract" flag on abstract
>> entities.
>> I confess that it didn't quite work the way I thought it would.
>> For one, the superclass was abstract, but not the subclass.  I went ahead
>> and made the subclass abstract.  Which raises the second issue.  I have two
>> obj entities entities extending the "abstract" superclass, using a single
>> discriminator column.  I went to query the base class (ala: select e from
>> Entry e), and the query failed, due to the inability to instantiate an
>> instance of Entry.  Evidently, cayenne is trying to instantiate all of the
>> subclasses instances as instances of the superclass.  Shouldn't cayenne be
>> instantiating the subclasses? Put another way, what is the technical reason
>> that cayenne /isn't/ instantiating the subclasses as instances of the
>> subclasses? Given the potential of polymorphism, it seems like instantiating
>> as the superclass, even if the superclass is concrete, is incorrect
>> behavior? Just trying to clarify, here.  I'll be digging through the code to
>> try to understand the specifics of how cayenne handles inheritance, but, any
>> pointers appreciated.
>> 
>> Robert


Re: abstract entities, inheritance, etc.

Posted by Michael Gentry <mg...@masslight.net>.
Hi Robert,

To resurrect a really old thread, I was wondering if you ever found a
solution or workaround to this problem?  I was helping another project
at work on Friday that hit the same issue.

Thanks,

mrg


On Thu, Apr 23, 2009 at 7:17 PM, Robert Zeigler <ro...@scazdl.org> wrote:
> So, I was playing around a bit with the "is abstract" flag on abstract
> entities.
> I confess that it didn't quite work the way I thought it would.
> For one, the superclass was abstract, but not the subclass.  I went ahead
> and made the subclass abstract.  Which raises the second issue.  I have two
> obj entities entities extending the "abstract" superclass, using a single
> discriminator column.  I went to query the base class (ala: select e from
> Entry e), and the query failed, due to the inability to instantiate an
> instance of Entry.  Evidently, cayenne is trying to instantiate all of the
> subclasses instances as instances of the superclass.  Shouldn't cayenne be
> instantiating the subclasses? Put another way, what is the technical reason
> that cayenne /isn't/ instantiating the subclasses as instances of the
> subclasses? Given the potential of polymorphism, it seems like instantiating
> as the superclass, even if the superclass is concrete, is incorrect
> behavior? Just trying to clarify, here.  I'll be digging through the code to
> try to understand the specifics of how cayenne handles inheritance, but, any
> pointers appreciated.
>
> Robert