DATACMNS-32 - Removed Querydsl support classes and use the ones from Spring Data Commons Core.

This commit is contained in:
Oliver Gierke
2011-04-20 20:32:21 +02:00
parent 21492c061a
commit 97859f5dee
3 changed files with 4 additions and 205 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.jpa.repository.support;
import static org.springframework.data.querydsl.QueryDslUtils.*;
import java.io.Serializable;
import javax.persistence.EntityManager;
@@ -28,7 +30,6 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.support.RepositoryFactorySupport;
import org.springframework.data.repository.support.RepositoryMetadata;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
@@ -38,10 +39,6 @@ import org.springframework.util.ClassUtils;
*/
public class JpaRepositoryFactory extends RepositoryFactorySupport {
private static final boolean QUERY_DSL_PRESENT = ClassUtils.isPresent(
"com.mysema.query.types.Predicate",
JpaRepositoryFactory.class.getClassLoader());
private final EntityManager entityManager;
private final QueryExtractor extractor;

View File

@@ -16,8 +16,6 @@
package org.springframework.data.jpa.repository.support;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import javax.persistence.EntityManager;
@@ -28,8 +26,8 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.repository.QueryDslPredicateExecutor;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
@@ -223,126 +221,4 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends
order.isAscending() ? com.mysema.query.types.Order.ASC
: com.mysema.query.types.Order.DESC, property);
}
/**
* Strategy interface to abstract the ways to translate an plain domain
* class into a {@link EntityPath}.
*
* @author Oliver Gierke
*/
public static interface EntityPathResolver {
<T> EntityPath<T> createPath(Class<T> domainClass);
}
/**
* Simple implementation of {@link EntityPathResolver} to lookup a query
* class by reflection and using the static field of the same type.
*
* @author Oliver Gierke
*/
static enum SimpleEntityPathResolver implements EntityPathResolver {
INSTANCE;
private static final String NO_CLASS_FOUND_TEMPLATE =
"Did not find a query class %s for domain class %s!";
private static final String NO_FIELD_FOUND_TEMPLATE =
"Did not find a static field of the same type in %s!";
/**
* Creates an {@link EntityPath} instance for the given domain class.
* Tries to lookup a class matching the naming convention (prepend Q to
* the simple name of the class, same package) and find a static field
* of the same type in it.
*
* @param domainClass
* @return
*/
@SuppressWarnings("unchecked")
public <T> EntityPath<T> createPath(Class<T> domainClass) {
String pathClassName = getQueryClassName(domainClass);
try {
Class<?> pathClass =
ClassUtils.forName(pathClassName,
QueryDslJpaRepository.class.getClassLoader());
Field field = getStaticFieldOfType(pathClass);
if (field == null) {
throw new IllegalStateException(String.format(
NO_FIELD_FOUND_TEMPLATE, pathClass));
} else {
return (EntityPath<T>) ReflectionUtils
.getField(field, null);
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(String.format(
NO_CLASS_FOUND_TEMPLATE, pathClassName,
domainClass.getName()), e);
}
}
/**
* Returns the first static field of the given type inside the given
* type.
*
* @param type
* @return
*/
private Field getStaticFieldOfType(Class<?> type) {
for (Field field : type.getDeclaredFields()) {
boolean isStatic = Modifier.isStatic(field.getModifiers());
boolean hasSameType = type.equals(field.getType());
if (isStatic && hasSameType) {
return field;
}
}
Class<?> superclass = type.getSuperclass();
return Object.class.equals(superclass) ? null
: getStaticFieldOfType(superclass);
}
/**
* Returns the name of the query class for the given domain class.
*
* @param domainClass
* @return
*/
private String getQueryClassName(Class<?> domainClass) {
String simpleClassName = ClassUtils.getShortName(domainClass);
return String.format("%s.Q%s%s",
domainClass.getPackage().getName(),
getClassBase(simpleClassName), domainClass.getSimpleName());
}
/**
* Analyzes the short class name and potentially returns the outer
* class.
*
* @param shortName
* @return
*/
private String getClassBase(String shortName) {
String[] parts = shortName.split("\\.");
if (parts.length < 2) {
return "";
}
return parts[0] + "_";
}
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2008-2011 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 static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.jpa.domain.sample.QUser;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.EntityPathResolver;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.SimpleEntityPathResolver;
import org.springframework.data.jpa.repository.util.JpaClassUtilsUnitTests.NamedUser;
import org.springframework.data.jpa.repository.util.QJpaClassUtilsUnitTests_NamedUser;
/**
* Unit test for {@link SimpleEntityPathResolver}.
*
* @author Oliver Gierke
*/
public class SimpleEntityPathResolverUnitTests {
EntityPathResolver resolver =
QueryDslJpaRepository.SimpleEntityPathResolver.INSTANCE;
@Test
public void createsRepositoryFromDomainClassCorrectly() throws Exception {
assertThat(resolver.createPath(User.class), is(QUser.class));
}
@Test
public void resolvesEntityPathForInnerClassCorrectly() throws Exception {
assertThat(resolver.createPath(NamedUser.class),
is(QJpaClassUtilsUnitTests_NamedUser.class));
}
@Test(expected = IllegalStateException.class)
public void rejectsFoundClassWithoutStaticFieldOfSameType()
throws Exception {
resolver.createPath(Sample.class);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme()
throws Exception {
resolver.createPath(QSimpleEntityPathResolverUnitTests_Sample.class);
}
static class Sample {
}
}