persistence@glassfish.java.net

Re: JPQL and Inheritance

From: James Sutherland <jamesssss_at_yahoo.com>
Date: Mon, 22 Oct 2007 07:45:33 -0700 (PDT)

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.
>
> @Entity(name = "ABSTRACT_ENTITY")
> @Inheritance(strategy = InheritanceType.JOINED)
> @DiscriminatorColumn(name = "ABSTRACT_ENTITY_TYPE", discriminatorType =
> DiscriminatorType.STRING)
> public abstract class AbstractEntity {
> . . .
> }
>
> @Entity(name = "ENTITY_SUB_A")
> @DiscriminatorValue(value = "discrimatorValueA")
> public class SubEntityA {
> . . .
> }
>
> @Entity(name = "ENTITY_SUB_B")
> @DiscriminatorValue(value = "discrimatorValueB")
> public class SubEntityB {
> . . .
> }
>
> Essentially what I want to do is:
> "SELECT DISTINCT e FROM ABSTRACT_ENTITY e WHERE e.ABSTRACT_ENTITY_TYPE
> =
> 'discrimatorValueA' OR e.ABSTRACT_ENTITY_TYPE = 'discrimatorValueD' ..."
> OR
> "SELECT DISTINCT e FROM ABSTRACT_ENTITY e WHERE e.ABSTRACT_ENTITY_TYPE
> <>
> 'discrimatorValueA' ..."
> (The specific WHERE clause will be variable; it may contain multiple
> conditional expressions, etc.)
>
> Since the discriminator column is only related to the physical schema, and
> is not declared as a property of the logical entity, querying for
> abstractEnity.ABSTRACT_ENTITY_TYPE is not possible in JPQL. I can create
> the query using native SQL, but I'd prefer to remain independent of native
> queries; or a property could be added to the subclass to uniquely identify
> it (essentially mimicking the discriminatorColumn), but this results in
> unnecessary duplication, leaking an unnecessary property which is only
> needed for JPA.
>
> Are there any other suggestions on how to restrict JPQL query results
> based
> on subclass?
>
>


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.oracle.com/technology/products/ias/toplink/ Oracle TopLink , 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  https://glassfish.dev.java.net/javaee5/persistence/ TopLink
Essentials 
Wiki:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence , 
http://wiki.eclipse.org/EclipseLink EclipseLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink , 
http://www.nabble.com/java.net---glassfish-persistence-f13455.html Glassfish
Persistence 
-- 
View this message in context: http://www.nabble.com/JPQL-and-Inheritance-tf4656271.html#a13342345
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.