You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Bruno René Santos <br...@holos.pt> on 2011/03/06 11:03:41 UTC

Querying on Inherited Objects

Hello all,

I have an object structure with a Child dataobject which inherits from a 
Parent which is linked to a db_entity. I am trying to use the Parent on 
a GUI and the Child on another GUI. The Child overrides some methods 
from the Parent (but the db table is the same) and so is mandatory that 
on each GUI their respective searches yeld Child objects or Parent 
objects. On the GUI that uses the Child everything goes according plan, 
the performQueries return lists of Children. But on the Parent GUI I call

context.performQuery(new SelectQuery(Parent.class))

but the method returns also a list of Children, which are incompatible 
with the parent GUI. Is there any way that this query returns Parents 
instead of Children? I was trying these inheritance so that i could 
reuse all db fields and relationships from the parent on the child.
-- 
Bruno René Santos | brunorene@holos.pt <ma...@holos.pt> | 
Gestor de Projectos | Analista | Programador | Investigador

Holos - Soluções Avançadas em Tecnologias de Informação S.A.
Parque de Ciência e Tecnologia de Almada/Setúbal . Edifício Madan Parque
Rua dos Inventores . Quinta da Torre . 2825 - 182 Caparica . Portugal
Phone: +351 210 438 686 . Fax: +351 210 438 687 . Web: www.holos.pt


This email and any files transmitted with it are confidential and 
intended solely for the use of the individual or entity to whom they are 
addressed. If you are not the intended recipient or the person 
responsible for delivering the email to the intended recipient, be 
advised that you have received this email in error and that any use, 
dissemination, forwarding, printing, or copying of this email is 
strictly prohibited. If you have received this email in error please 
notify Bruno René Santos by telephone on +351 210 438 686


Re: Querying on Inherited Objects

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 6/03/11 11:18 PM, Bruno René Santos wrote:
> Both parent and child are the same on the database level.

Maybe you need to explain further what this means. Are you using single table inheritance? Do you have a disciminator column?

Ari

-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Querying on Inherited Objects

Posted by Bruno René Santos <br...@holos.pt>.
Hi Marcin,

What you said makes sense. When i query for a parent I will get all 
children also. At this moment I solved the problem by using two distinct 
ObjEntities that point to a single DBEntity. Its working

Thanx a lot
Bruno

Em 07-03-2011 22:24, Marcin Skladaniec escreveu:
> Hi Bruno
>
> All squares are rectangles, but some rectangles are squares, the query 
> for rectangles will also give you squares. This is a valid result from 
> cayenne's perspective.
>
> What you looking for is to query for Rectangle but not Square. I see 
> three ways of doing that:
> - query for Parent, query for Child, subtract results from each other. 
> (performQuery(Rectangle).removeAll(performQuery(Square))
> - create another subclass of Parent, mutually exclusive with Child, 
> and query that. (performQuery(NotSquare))
> - expose the qualifier field in cayenne modeler, query with the 
> qualifier. (performQuery(Rectangle, 
> ExpressionFactory.noMatch(polygonType, square))
>
>
> With regards
> Marcin
>
>
>
> On 6/03/11 11:18 PM, Bruno René Santos wrote:
>> Both parent and child are the same on the database level. But each 
>> object is used differently on the GUI level. As both GUIs must 
>> cohexist temporarily I thought I could do a child object from the 
>> current used object and change some logic on it. So i got something 
>> like this
>>
>> Child extends _Child
>>
>> _Child extends Parent
>>
>> Parent extends _Parent
>>
>> _Parent extends CayenneDataObject
>>
>> The only issue here (at least I think...) is that a performQuery(new 
>> SelectQuery(Child.class)) will correctly return a list of Child but a 
>> performQuery(new SelectQuery(Parent.class)) will also return a List 
>> of Child... Any way to return a List of Parent?
>>
>> Thanx
>> Bruno
>>
>> Em 06-03-2011 10:15, Aristedes Maniatis escreveu:
>>> On 6/03/11 9:03 PM, Bruno René Santos wrote:
>>>> I have an object structure with a Child dataobject which inherits 
>>>> from a Parent which is linked to a db_entity. I am trying to use 
>>>> the Parent on a GUI and the Child on another GUI. The Child 
>>>> overrides some methods from the Parent (but the db table is the 
>>>> same) and so is mandatory that on each GUI their respective 
>>>> searches yeld Child objects or Parent objects. On the GUI that uses 
>>>> the Child everything goes according plan, the performQueries return 
>>>> lists of Children. But on the Parent GUI I call
>>>>
>>>> context.performQuery(new SelectQuery(Parent.class))
>>>>
>>>> but the method returns also a list of Children, which are 
>>>> incompatible with the parent GUI. Is there any way that this query 
>>>> returns Parents instead of Children? I was trying these inheritance 
>>>> so that i could reuse all db fields and relationships from the 
>>>> parent on the child.
>>>
>>> What does the discriminator column say about these objects? Are they 
>>> actually Child entities in the database or Parent? They can't be 
>>> both at the same time.
>>>
>>> This isn't like plain-old Java inheritance where you can just cast 
>>> from one to the other as needed. If you could, what would happen if 
>>> you queried for a list of Child objects, casting them as Parents, 
>>> changed some Parent attribute and committed the context? Would they 
>>> now become Parents in the database?
>>>
>>> Ari
>>>
>>>
>>
>>
>
>
>
>


-- 
Bruno René Santos | brunorene@holos.pt <ma...@holos.pt> | 
Gestor de Projectos | Analista | Programador | Investigador

Holos - Soluções Avançadas em Tecnologias de Informação S.A.
Parque de Ciência e Tecnologia de Almada/Setúbal . Edifício Madan Parque
Rua dos Inventores . Quinta da Torre . 2825 - 182 Caparica . Portugal
Phone: +351 210 438 686 . Fax: +351 210 438 687 . Web: www.holos.pt


This email and any files transmitted with it are confidential and 
intended solely for the use of the individual or entity to whom they are 
addressed. If you are not the intended recipient or the person 
responsible for delivering the email to the intended recipient, be 
advised that you have received this email in error and that any use, 
dissemination, forwarding, printing, or copying of this email is 
strictly prohibited. If you have received this email in error please 
notify Bruno René Santos by telephone on +351 210 438 686


Re: Querying on Inherited Objects

Posted by Marcin Skladaniec <ma...@ish.com.au>.
Hi Bruno

All squares are rectangles, but some rectangles are squares, the query 
for rectangles will also give you squares. This is a valid result from 
cayenne's perspective.

What you looking for is to query for Rectangle but not Square. I see 
three ways of doing that:
- query for Parent, query for Child, subtract results from each other. 
(performQuery(Rectangle).removeAll(performQuery(Square))
- create another subclass of Parent, mutually exclusive with Child, and 
query that. (performQuery(NotSquare))
- expose the qualifier field in cayenne modeler, query with the 
qualifier. (performQuery(Rectangle, 
ExpressionFactory.noMatch(polygonType, square))


With regards
Marcin



On 6/03/11 11:18 PM, Bruno René Santos wrote:
> Both parent and child are the same on the database level. But each 
> object is used differently on the GUI level. As both GUIs must 
> cohexist temporarily I thought I could do a child object from the 
> current used object and change some logic on it. So i got something 
> like this
>
> Child extends _Child
>
> _Child extends Parent
>
> Parent extends _Parent
>
> _Parent extends CayenneDataObject
>
> The only issue here (at least I think...) is that a performQuery(new 
> SelectQuery(Child.class)) will correctly return a list of Child but a 
> performQuery(new SelectQuery(Parent.class)) will also return a List of 
> Child... Any way to return a List of Parent?
>
> Thanx
> Bruno
>
> Em 06-03-2011 10:15, Aristedes Maniatis escreveu:
>> On 6/03/11 9:03 PM, Bruno René Santos wrote:
>>> I have an object structure with a Child dataobject which inherits 
>>> from a Parent which is linked to a db_entity. I am trying to use the 
>>> Parent on a GUI and the Child on another GUI. The Child overrides 
>>> some methods from the Parent (but the db table is the same) and so 
>>> is mandatory that on each GUI their respective searches yeld Child 
>>> objects or Parent objects. On the GUI that uses the Child everything 
>>> goes according plan, the performQueries return lists of Children. 
>>> But on the Parent GUI I call
>>>
>>> context.performQuery(new SelectQuery(Parent.class))
>>>
>>> but the method returns also a list of Children, which are 
>>> incompatible with the parent GUI. Is there any way that this query 
>>> returns Parents instead of Children? I was trying these inheritance 
>>> so that i could reuse all db fields and relationships from the 
>>> parent on the child.
>>
>> What does the discriminator column say about these objects? Are they 
>> actually Child entities in the database or Parent? They can't be both 
>> at the same time.
>>
>> This isn't like plain-old Java inheritance where you can just cast 
>> from one to the other as needed. If you could, what would happen if 
>> you queried for a list of Child objects, casting them as Parents, 
>> changed some Parent attribute and committed the context? Would they 
>> now become Parents in the database?
>>
>> Ari
>>
>>
>
>



Re: Querying on Inherited Objects

Posted by Bruno René Santos <br...@holos.pt>.
Both parent and child are the same on the database level. But each 
object is used differently on the GUI level. As both GUIs must cohexist 
temporarily I thought I could do a child object from the current used 
object and change some logic on it. So i got something like this

Child extends _Child

_Child extends Parent

Parent extends _Parent

_Parent extends CayenneDataObject

The only issue here (at least I think...) is that a performQuery(new 
SelectQuery(Child.class)) will correctly return a list of Child but a 
performQuery(new SelectQuery(Parent.class)) will also return a List of 
Child... Any way to return a List of Parent?

Thanx
Bruno

Em 06-03-2011 10:15, Aristedes Maniatis escreveu:
> On 6/03/11 9:03 PM, Bruno René Santos wrote:
>> I have an object structure with a Child dataobject which inherits 
>> from a Parent which is linked to a db_entity. I am trying to use the 
>> Parent on a GUI and the Child on another GUI. The Child overrides 
>> some methods from the Parent (but the db table is the same) and so is 
>> mandatory that on each GUI their respective searches yeld Child 
>> objects or Parent objects. On the GUI that uses the Child everything 
>> goes according plan, the performQueries return lists of Children. But 
>> on the Parent GUI I call
>>
>> context.performQuery(new SelectQuery(Parent.class))
>>
>> but the method returns also a list of Children, which are 
>> incompatible with the parent GUI. Is there any way that this query 
>> returns Parents instead of Children? I was trying these inheritance 
>> so that i could reuse all db fields and relationships from the parent 
>> on the child.
>
> What does the discriminator column say about these objects? Are they 
> actually Child entities in the database or Parent? They can't be both 
> at the same time.
>
> This isn't like plain-old Java inheritance where you can just cast 
> from one to the other as needed. If you could, what would happen if 
> you queried for a list of Child objects, casting them as Parents, 
> changed some Parent attribute and committed the context? Would they 
> now become Parents in the database?
>
> Ari
>
>


-- 
Bruno René Santos | brunorene@holos.pt <ma...@holos.pt> | 
Gestor de Projectos | Analista | Programador | Investigador

Holos - Soluções Avançadas em Tecnologias de Informação S.A.
Parque de Ciência e Tecnologia de Almada/Setúbal . Edifício Madan Parque
Rua dos Inventores . Quinta da Torre . 2825 - 182 Caparica . Portugal
Phone: +351 210 438 686 . Fax: +351 210 438 687 . Web: www.holos.pt


This email and any files transmitted with it are confidential and 
intended solely for the use of the individual or entity to whom they are 
addressed. If you are not the intended recipient or the person 
responsible for delivering the email to the intended recipient, be 
advised that you have received this email in error and that any use, 
dissemination, forwarding, printing, or copying of this email is 
strictly prohibited. If you have received this email in error please 
notify Bruno René Santos by telephone on +351 210 438 686


Re: Querying on Inherited Objects

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 6/03/11 9:03 PM, Bruno René Santos wrote:
> I have an object structure with a Child dataobject which inherits from a Parent which is linked to a db_entity. I am trying to use the Parent on a GUI and the Child on another GUI. The Child overrides some methods from the Parent (but the db table is the same) and so is mandatory that on each GUI their respective searches yeld Child objects or Parent objects. On the GUI that uses the Child everything goes according plan, the performQueries return lists of Children. But on the Parent GUI I call
>
> context.performQuery(new SelectQuery(Parent.class))
>
> but the method returns also a list of Children, which are incompatible with the parent GUI. Is there any way that this query returns Parents instead of Children? I was trying these inheritance so that i could reuse all db fields and relationships from the parent on the child.

What does the discriminator column say about these objects? Are they actually Child entities in the database or Parent? They can't be both at the same time.

This isn't like plain-old Java inheritance where you can just cast from one to the other as needed. If you could, what would happen if you queried for a list of Child objects, casting them as Parents, changed some Parent attribute and committed the context? Would they now become Parents in the database?

Ari


-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A