DATAJPA-181 - Improve JPAQuery instantiation for Querydsl support.

Upgraded to Querydsl 2.3.3. We now choose the appropriate JPQLTemplate depending on the used persistence provider inside QueryDslRepositorySupport as well as QueryDslJpaRepository.
This commit is contained in:
Oliver Gierke
2012-04-11 07:20:34 +02:00
parent 046e7f3b09
commit c49e17fc0b
4 changed files with 60 additions and 8 deletions

View File

@@ -60,7 +60,7 @@
<openjpa.version>2.1.1</openjpa.version>
<eclipselink.version>2.3.2</eclipselink.version>
<aspectj.version>1.6.12</aspectj.version>
<querydsl.version>2.3.0</querydsl.version>
<querydsl.version>2.3.3</querydsl.version>
<junit.version>4.8.1</junit.version>
<jpa.version>2.0.0</jpa.version>
<slf4j.version>1.6.1</slf4j.version>

View File

@@ -30,7 +30,6 @@ import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.Expression;
import com.mysema.query.types.OrderSpecifier;
@@ -51,6 +50,7 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
private final EntityManager em;
private final EntityPath<T> path;
private final PathBuilder<T> builder;
private final PersistenceProvider provider;
/**
* Creates a new {@link QueryDslJpaRepository} from the given domain class and {@link EntityManager}. This will use
@@ -79,6 +79,7 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
this.em = entityManager;
this.path = resolver.createPath(entityMetadata.getJavaType());
this.builder = new PathBuilder<T>(path.getType(), path.getMetadata());
this.provider = PersistenceProvider.fromEntityManager(entityManager);
}
/*
@@ -148,8 +149,7 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
* @return
*/
protected JPQLQuery createQuery(Predicate... predicate) {
return new JPAQuery(em).from(path).where(predicate);
return QuerydslUtils.createQueryInstance(em, provider).from(path).where(predicate);
}
/**

View File

@@ -26,7 +26,6 @@ import com.mysema.query.dml.DeleteClause;
import com.mysema.query.dml.UpdateClause;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPADeleteClause;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.jpa.impl.JPAUpdateClause;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.path.PathBuilder;
@@ -40,8 +39,9 @@ import com.mysema.query.types.path.PathBuilderFactory;
@Repository
public abstract class QueryDslRepositorySupport {
private EntityManager entityManager;
private PathBuilderFactory builderFactory = new PathBuilderFactory();
private EntityManager entityManager;
private PersistenceProvider provider;
/**
* Setter to inject {@link EntityManager}.
@@ -53,6 +53,7 @@ public abstract class QueryDslRepositorySupport {
Assert.notNull(entityManager);
this.entityManager = entityManager;
this.provider = PersistenceProvider.fromEntityManager(entityManager);
}
/**
@@ -78,8 +79,7 @@ public abstract class QueryDslRepositorySupport {
* @return
*/
protected JPQLQuery from(EntityPath<?>... paths) {
return new JPAQuery(entityManager).from(paths);
return QuerydslUtils.createQueryInstance(entityManager, provider).from(paths);
}
/**

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.support;
import javax.persistence.EntityManager;
import com.mysema.query.jpa.EclipseLinkTemplates;
import com.mysema.query.jpa.HQLTemplates;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.OpenJPATemplates;
import com.mysema.query.jpa.impl.JPAQuery;
/**
* Utility methods for Querydsl.
*
* @author Oliver Gierke
*/
class QuerydslUtils {
/**
* Creates the {@link JPQLQuery} instance based on the given {@link EntityManager} and {@link PersistenceProvider}.
*
* @return
*/
public static JPQLQuery createQueryInstance(EntityManager em, PersistenceProvider provider) {
switch (provider) {
case ECLIPSELINK:
return new JPAQuery(em, EclipseLinkTemplates.DEFAULT);
case HIBERNATE:
return new JPAQuery(em, HQLTemplates.DEFAULT);
case OPEN_JPA:
return new JPAQuery(em, OpenJPATemplates.DEFAULT);
case GENERIC_JPA:
default:
return new JPAQuery(em);
}
}
}