Permalink
Browse files

Improved query class detection.

- made sure a static field is used
- corrected naming convention for inner classes
- altered and added test cases for those scenarios
  • Loading branch information...
1 parent 2013edf commit 960bf09fb0fe1d8b726d967cde8bedebbd24864c @olivergierke olivergierke committed Jan 8, 2011
@@ -17,6 +17,7 @@
import java.io.Serializable;
import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
import java.util.List;
import javax.persistence.EntityManager;
@@ -282,16 +283,16 @@ private JPQLQuery applySorting(JPQLQuery query, Sort sort) {
Class<?> pathClass =
ClassUtils.forName(pathClassName,
QueryDslJpaRepository.class.getClassLoader());
-
- for (Field field : pathClass.getFields()) {
- if (pathClass.equals(field.getType())) {
- return (EntityPath<T>) ReflectionUtils.getField(field,
- null);
- }
+ 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);
}
- throw new IllegalStateException(String.format(
- NO_FIELD_FOUND_TEMPLATE, pathClass));
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(String.format(
NO_CLASS_FOUND_TEMPLATE, pathClassName,
@@ -301,6 +302,31 @@ private JPQLQuery applySorting(JPQLQuery query, Sort sort) {
/**
+ * 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
@@ -309,7 +335,8 @@ private JPQLQuery applySorting(JPQLQuery query, Sort sort) {
private String getQueryClassName(Class<?> domainClass) {
String simpleClassName = ClassUtils.getShortName(domainClass);
- return String.format("%s%sQ%s", domainClass.getPackage().getName(),
+ return String.format("%s.Q%s%s",
+ domainClass.getPackage().getName(),
getClassBase(simpleClassName), domainClass.getSimpleName());
}
@@ -326,10 +353,10 @@ private String getClassBase(String shortName) {
String[] parts = shortName.split("\\.");
if (parts.length < 2) {
- return ".";
+ return "";
}
- return "." + parts[0] + "$";
+ return parts[0] + "_";
}
}
}
@@ -0,0 +1,26 @@
+/*
+ * Copyright 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;
+
+/**
+ * Stub for a generated QueryDsl query class.
+ *
+ * @author Oliver Gierke
+ */
+class QSimpleEntityPathResolverUnitTests_Sample {
+
+ public QSimpleEntityPathResolverUnitTests_Sample field;
+}
@@ -21,6 +21,7 @@
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.JpaAnnotationEntityInformationUnitTests.FieldAnnotatedEntity;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.EntityPathResolver;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.SimpleEntityPathResolver;
@@ -43,6 +44,15 @@ public void createsRepositoryFromDomainClassCorrectly() throws Exception {
}
+ @Test
+ public void resolvesEntityPathForInnerClassCorrectly() throws Exception {
+
+ assertThat(
+ resolver.createPath(FieldAnnotatedEntity.class),
+ is(QJpaAnnotationEntityInformationUnitTests_FieldAnnotatedEntity.class));
+ }
+
+
@Test(expected = IllegalStateException.class)
public void rejectsFoundClassWithoutStaticFieldOfSameType()
throws Exception {
@@ -55,14 +65,10 @@ public void rejectsFoundClassWithoutStaticFieldOfSameType()
public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme()
throws Exception {
- resolver.createPath(QSample.class);
+ resolver.createPath(QSimpleEntityPathResolverUnitTests_Sample.class);
}
static class Sample {
}
-
- static class QSample {
-
- }
}

0 comments on commit 960bf09

Please sign in to comment.