Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Development/JPA/QueryDownCast"

(Accessing un-mapped attributes using QueryKey)
Line 79: Line 79:
  
 
<source lang="java">
 
<source lang="java">
 +
// Configure the use of a customizer on the entity class
 +
@Customizer(ACustomizer.class)
 +
public abstract class A {
 +
 +
 +
// The customizer adds the direct query keys
 +
public class ACustomizer implements DescriptorCustomizer {
 +
 +
public void customize(ClassDescriptor descriptor) throws Exception {
 +
descriptor.addDirectQueryKey("bValue", "BVALUE");
 +
descriptor.addDirectMapping("cValue", "CVALUE");
 +
}
 +
 +
}
 
</source>
 
</source>
 +
 +
Now you can write your query:
 +
 +
<source lang="java">
 +
ReadAllQuery raq = new ReadAllQuery(A.class);
 +
ExpressionBuilder eb = raq.getExpressionBuilder();
 +
raq.setSelectionCriteria(eb.get("aValue").like("A%").and(eb.get("bValue").like("bValue")).and(eb.get("cValue").like("CVALUE")));
 +
 +
// Wrap in JPA Query
 +
Query query = JpaHelper.createQuery(raq, em);
 +
 +
// Execute Query
 +
List<A> results = query.getResultList();
 +
</source>
 +
 +
The resulting SQL appears as:
 +
 +
<pre>
 +
SELECT ID, INH_TYPE, AVALUE, CVALUE, BVALUE FROM DOWNCAST_SIMPLE_A WHERE (((AVALUE LIKE ?) AND (BVALUE LIKE ?)) AND (CVALUE LIKE ?))
 +
</pre>
  
 
== Solution Design ==
 
== Solution Design ==
  
 
TBD
 
TBD

Revision as of 10:59, 18 December 2008

Enhancement: Query Down Cast

This page captures the requirements, design, and existing functionality for bug ? to enable EclipseLink JPA/ORM developers to define queries on inheritance hierarchies with down casting to specific classes.

Requirements

Work-Arounds

Since this feature will take significant development effort the following suggest work-around options are provided to assist users who require this functionality. If having this is a show-stopper for your project please vote for and add your feedback to bug ?.

Single Class Results

While this may seem obvious it often important to point out all potential solutions. If the query you are executing will only be returning a single type from the inheritance hierarchy then it is important that that be the target type of the query. This will allow you to access all mapped attributes in this class and its mapped parent classes.

Accessing un-mapped attributes using QueryKey

When your inheritance hierarchy leverages a common table for multiple mapped classes in the hierarchy it is possible to query for attributes that are not visible in the class you are querying for through the use of query keys.

Example

In this example we'll map a simple inheritance hierarchy of class A having two subclasses B and C. Each class will have its own value and they will all be mapped to a single table.

@Entity
@Table(name="DOWNCAST_SIMPLE_A")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="INH_TYPE",discriminatorType=DiscriminatorType.CHAR)
public abstract class A {
	@Id
	private int id;
 
	private String aValue;
 
	// accessor methods
}
 
@Entity
@DiscriminatorValue("B")
public class B  extends A{
 
	private String bValue;
 
	// accessor methods
}
 
@Entity
@DiscriminatorValue("C")
public class C extends A {
 
	private String cValue;
 
	// accessor methods
}

Based on this mapped entity model the generated schema looks like:

CREATE TABLE DOWNCAST_SIMPLE_A (
		ID NUMBER(10) NOT NULL, 
		INH_TYPE VARCHAR2(31) NULL, 
		AVALUE VARCHAR2(255) NULL, 
		BVALUE VARCHAR2(255) NULL, 
		CVALUE VARCHAR2(255) NULL,
	 PRIMARY KEY (ID))

Now to build a heterogenous query for A using bValue and cValue I need to define query keys on A to make this fields visible.

// Configure the use of a customizer on the entity class
@Customizer(ACustomizer.class)
public abstract class A {
 
 
// The customizer adds the direct query keys
public class ACustomizer implements DescriptorCustomizer {
 
	public void customize(ClassDescriptor descriptor) throws Exception {
		descriptor.addDirectQueryKey("bValue", "BVALUE");
		descriptor.addDirectMapping("cValue", "CVALUE");
	}
 
}

Now you can write your query:

ReadAllQuery raq = new ReadAllQuery(A.class);
ExpressionBuilder eb = raq.getExpressionBuilder();
raq.setSelectionCriteria(eb.get("aValue").like("A%").and(eb.get("bValue").like("bValue")).and(eb.get("cValue").like("CVALUE")));
 
// Wrap in JPA Query
Query query = JpaHelper.createQuery(raq, em);
 
// Execute Query
List<A> results = query.getResultList();

The resulting SQL appears as:

SELECT ID, INH_TYPE, AVALUE, CVALUE, BVALUE FROM DOWNCAST_SIMPLE_A WHERE (((AVALUE LIKE ?) AND (BVALUE LIKE ?)) AND (CVALUE LIKE ?))

Solution Design

TBD

Back to the top