persistence@glassfish.java.net

Re: JPQL and Inheritance

From: christopher delahunt <christopher.delahunt_at_oracle.com>
Date: Thu, 10 Jul 2008 13:26:34 -0400

Hello,

Can you post the full exception stack trace, and the query that you are
using the "type" query key in? The exception is complaining about the
query key, so the property tag must be working or the customize method
would not
be adding it.

Best Regards,
Chris

griccardi wrote:
> I am trying unsuccessfully to use the quoted example from Mr. Sutherland. I
> am otherwise using with toplink essentials with great success.
> Unfortunately, the full class names are missing, the method
> addQueryKey(String, String) is not part of toplink essentials, and the
> details of the representation in persistence.xml is unclear to me.
>
> What I found on the Web about toplink essentials allowed me to create the
> following class and persistence.xml property. My inheritance base class is
> "a.BaseObject" and the discriminator field is "objectTypeId". I found no
> information on how to create a QueryKey and so made up something.
>
> package a;
> import oracle.toplink.essentials.descriptors.ClassDescriptor;
> import
> oracle.toplink.essentials.tools.sessionconfiguration.DescriptorCustomizer;
> import oracle.toplink.essentials.querykeys.*;
>
> public class MDescriptorCustomizer implements DescriptorCustomizer {
> public void customize(ClassDescriptor descriptor) throws Exception {
> DirectQueryKey queryKey = new DirectQueryKey(); // error very likely here!
> queryKey.setName("type");
> queryKey.setFieldName("a.BaseObject.objectTypeId");
> descriptor.addQueryKey(queryKey);
> }
> }
>
> to my persistence.xml file I add:
>
> <property name="toplink.descriptor.customizer.BaseObject"
> value="a.MyDescriptorCustomizer"/>
>
> The error I see on my query is " unknown state or association field
> [objectTypeId] of class [a.BaseObject]."
>
> I suspect the <property ... /> is incorrect, although a change in the value
> to make it an invalid class name does cause an error.
>
> Please steer me onto the correct path,
>
> Greg Riccardi
>
>
> James Sutherland wrote:
>
>> I do not think there is support for this in the JPA/JPQL spec, however in
>> TopLink Essentials you can use a DescriptorCustomizer to add a QueryKey to
>> your TopLink descriptor for the type field. You will then be able to
>> query on this query key in JPQL.
>>
>> i.e.
>> public class MyDescriptorCustomizer implements DescriptorCustomizer {
>> public void customize(ClassDescriptor descriptor) {
>> descriptor.addQueryKey("type",
>> "ABSTRACT_ENTITY.ABSTRACT_ENTITY_TYPE");
>> }
>> }
>>
>> You can add the customizer in your persistence.xml.
>>
>>
>> Shelley wrote:
>>
>>> I have an abstract entity class which defines a joined inheritance type,
>>> using a discriminator column. I would like to create a JPQL query which
>>> allows for dynamic inclusion/exclusion of specific entity subtypes.
>>>
>>>
>>>
>>>
>>
>
>