Skip to content

Commit

Permalink
Add CrudJoinType to hibernate connector
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Mar 17, 2021
1 parent 31eebd3 commit bb28cb0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.antelopesystem.crudframework.jpa.annotation

import org.hibernate.sql.JoinType

/**
* Determine join type for relation
*/
@Target(AnnotationTarget.FIELD)
annotation class CrudJoinType(
val joinType: JoinType
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.antelopesystem.crudframework.jpa.dao;

import com.antelopesystem.crudframework.jpa.annotation.CrudJoinType;
import com.antelopesystem.crudframework.jpa.model.BaseJpaEntity;
import com.antelopesystem.crudframework.model.PersistentEntity;
import com.antelopesystem.crudframework.modelfilter.*;
Expand Down Expand Up @@ -360,14 +361,15 @@ private boolean isValidSimpleFilterField(FilterField filterField) {

private void createAliases(Class clazz, Criteria criteria, List<String> aliases, String existingAlias, Set<String> flatFilterFieldNames) {
for(Field field : clazz.getDeclaredFields()) {

Class type = field.getType();
boolean isCollection = false;
if(Collection.class.isAssignableFrom(type)) {
type = (Class<?>) (((ParameterizedTypeImpl) field.getGenericType()).getActualTypeArguments())[0];
isCollection = true;
}

CrudJoinType crudJoinType = field.getDeclaredAnnotation(CrudJoinType.class);

String aliasPath;
if(existingAlias != null) {
aliasPath = existingAlias + "." + field.getName();
Expand All @@ -389,7 +391,11 @@ private void createAliases(Class clazz, Criteria criteria, List<String> aliases,
String newAlias = aliasPath.replace(".", "/");

if(flatFilterFieldNames.stream().anyMatch(name -> name.startsWith(newAlias + ".") || name.startsWith(newAlias + "/"))) {
criteria.createAlias(aliasPath, newAlias);
if(crudJoinType != null) {
criteria.createAlias(aliasPath, newAlias, crudJoinType.joinType());
} else {
criteria.createAlias(aliasPath, newAlias);
}
aliases.add(aliasPath.replace(".", "/"));
createAliases(type, criteria, aliases, aliasPath, flatFilterFieldNames);
}
Expand Down

0 comments on commit bb28cb0

Please sign in to comment.