Hibernate - Get classes that referenced a given entity

There are times when we want to list the entities that referenced (via foreign keys) a given entity x. For example, when deleting entity x t...

There are times when we want to list the entities that referenced (via foreign keys) a given entity x. For example, when deleting entity x that is being referenced, it will throw a generic ConstraintViolationException. Oftentimes, we need to display this classes. This is how we do it:

/**
* Map of classes and classes and fields that contains the referenced class.
*/
private static Map<Class, Map<Class, List<Field>>> classReferences = new HashMap<>();

/**
* Determines a generic type of a field. For example: List<String> field should return String).
*/
public static Class getFieldGenericsType(Field field) {
if (field.getGenericType() instanceof ParameterizedType) {
ParameterizedType aType = (ParameterizedType) field.getGenericType();
Type[] fieldArgTypes = aType.getActualTypeArguments();

for (Type fieldArgType : fieldArgTypes) {
Class fieldArgClass = (Class) fieldArgType;
return fieldArgClass;
}
}

return null;
}

/**
* Gets all the declared fields of a given class.
**/
public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
fields.addAll(Arrays.asList(type.getDeclaredFields()));

if (type.getSuperclass() != null) {
fields = getAllFields(fields, type.getSuperclass());
}

return fields;
}

/**
* Gets all the classes that referenced a given class.
*/
public static Map<Class, List<Field>> getReferencedClassesAndFieldsOfType(Class fieldClass) {

if (classReferences.containsKey(fieldClass)) {
return classReferences.get(fieldClass);
}

Class superClass = fieldClass.getSuperclass();

Map<Class, List<Field>> matchedFields = new HashMap<>();

Reflections reflections = new Reflections("com.broodcamp.model");
// gets all our entity classes in our project
Set<Class<? extends BaseEntity>> classes = reflections.getSubTypesOf(BaseEntity.class);

// loop thru
for (Class<? extends BaseEntity> clazz : classes) {
// we are not interested with either interface or abstract
if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
continue;
}

// gets all the fields of a given class
List<Field> fields = getAllFields(new ArrayList<Field>(), clazz);

// loops thru the fields
for (Field field : fields) {

// we are not interested with transient field
if (field.isAnnotationPresent(Transient.class)) {
continue;
}

// filter the field or parametized field of type fieldClass
// this means it refer to our referenced class
if (field.getType() == fieldClass || (Collection.class.isAssignableFrom(field.getType()) && getFieldGenericsType(field) == fieldClass) || (superClass != null
&& (field.getType() == superClass || (Collection.class.isAssignableFrom(field.getType()) && getFieldGenericsType(field) == superClass)))) {

// add to map
if (!matchedFields.containsKey(clazz)) {
matchedFields.put(clazz, new ArrayList<>());
}
matchedFields.get(clazz).add(field);
}
}
}
classReferences.put(fieldClass, matchedFields);

return matchedFields;
}

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: Hibernate - Get classes that referenced a given entity
Hibernate - Get classes that referenced a given entity
toztech
https://toztech.blogspot.com/2017/12/hibernate-get-classes-that-referenced.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2017/12/hibernate-get-classes-that-referenced.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content