Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Using QueryKeys in EclipseLink 2.1.0

Hi,

I was looking at the new feature in EclipseLink 2.1.0 that allows query-keys in JPQL-queries. Will this feature be expanded to JPA-criteria and EclipseLink criteria, too?

Say, you have to entities: "Person" and "TaxEntity", both are unrelated but have an attribute "taxNumber" in common. A one-to-one-query-key will define a "virtual" relationship between both entities. (I know that this could probably be mapped by a "real" relationship - it is just to exemplify what I mean.)

With EclipseLink 2.1.0 you can formulate JPQL-queries like this:
SELECT p FROM Person p WHERE p.correspondingTaxEntity.incomeTaxClass = '5'

or even like this:
SELECT p FROM Person p LEFT JOIN p.correspondingTaxEntity e WHERE e.incomeTaxClass = '5'

However, if you try the same with JPA-criteria you will get an error, as the attribute "correspondingTaxEntity" is not known within the meta-model:

java.lang.IllegalArgumentException: The attribute [correspondingTaxEntity] from the 
managed type [EntityTypeImpl@31820984:Person [ javaType: class model.Person descriptor: 
RelationalDescriptor(model.Person --> [DatabaseTable(TEST.PERSON)]), mappings: 3]] 
is not present.

There is also something weird with EclipseLink-criteria (or me misunderstanding the API):

This one works:
_expression_ exp = eb.get("correspondingTaxEntity").get("incomeTaxClass").equal("5");

But this one does not (despite that the JPQL-query did work as expected):
_expression_ exp = eb.get("correspondingTaxEntity").getAllowingNull("incomeTaxClass").equal("5");
org.eclipse.persistence.exceptions.QueryException: An outer join (getAllowingNull or 
anyOfAllowingNone) is only valid for OneToOne, OneToMany, ManyToMany, AggregateCollection and 
DirectCollection Mappings, and cannot be used for the mapping 
[org.eclipse.persistence.mappings.DirectToFieldMapping[correspondingTaxEntity-->TEST.TAXENTITY.INCOMETAXCLASS]].

The query-key was declared like this:

DatabaseField foreignKeyField = personDescriptor.getMappingForAttributeName("taxNumber").getField();
DatabaseField primaryKeyField = taxEntityDescriptor.getPrimaryKeyFields().get(0); // taxNumber=pk
OneToOneQueryKey taxEntityKey = new OneToOneQueryKey();
taxEntityKey.setName("correspondingTaxEntity");
ExpressionBuilder eb = new ExpressionBuilder();
_expression_ _expression_ = eb.getField(primaryKeyField).equal(eb.getParameter(foreignKeyField));
taxEntityKey.setJoinCriteria(_expression_);
taxEntityKey.setReferenceClass(TaxEntity.class);
personDescriptor.addQueryKey(taxEntityKey);


Kind regards,
Frank



View this message in context: Using QueryKeys in EclipseLink 2.1.0
Sent from the EclipseLink - Users mailing list archive at Nabble.com.

Back to the top