Skip to content

Commit

Permalink
* changes to make isFetchTypeLazy correctly check a getter method for
Browse files Browse the repository at this point in the history
* JPA "relationship" annotations even if supplied a setter
  • Loading branch information
delocalizer committed Jul 7, 2015
1 parent 689b388 commit 11367fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ public boolean isHandled(final Method theMethod) {

@SuppressWarnings("unchecked")
private static <T> T getProxyOrDbObject(Object theAccessor, Class<T> theClass, Object theKey, DataSource theSource) throws Exception {
if (BeanReflectUtil.isFetchTypeLazy(theAccessor)) {
if (BeanReflectUtil.isFetchTypeLazy(theAccessor, theClass)) {
Proxy<T> aProxy = new Proxy<T>(theClass, asPrimaryKey(theKey), theSource);

ProxyFactory aFactory = new ProxyFactory();
Expand Down
24 changes: 23 additions & 1 deletion core/main/src/com/clarkparsia/empire/util/BeanReflectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -753,12 +753,34 @@ public static boolean isPrimitive(Class theObj) {
* @param theAccessor the accessor
* @return true if the accessor is marked with {@link FetchType#LAZY}, false otherwise.
*/
public static boolean isFetchTypeLazy(Object theAccessor) {
public static boolean isFetchTypeLazy(Object theAccessor, Class theClass) {
FetchType aFetchType = null;

if (theAccessor instanceof AccessibleObject) {
AccessibleObject aObject = (AccessibleObject) theAccessor;

if (aObject instanceof Method) {

Method aMethod = (Method)aObject;
// if we got a setter, actually we should check that the corresponding
// *getter* was annotated with one of the JPA "relationship" annotations
if (aMethod.getName().startsWith("set")
&& (aMethod.getGenericReturnType().equals(Void.class) || aMethod.getGenericReturnType().toString().equals("void")
&& aMethod.getParameterTypes().length == 1)) {

for (String prefix : new String[]{"get", "is"}) {
String aGetterName = aMethod.getName().replaceFirst("set", prefix);
try {
aObject = (AccessibleObject)(theClass.getMethod(aGetterName));
break;
}
catch (NoSuchMethodException e) {
// let it go
}
}
}
}

if (aObject.getAnnotation(OneToMany.class) != null) {
aFetchType = aObject.getAnnotation(OneToMany.class).fetch();
}
Expand Down

0 comments on commit 11367fe

Please sign in to comment.