Hibernate - Get classes that referenced a given entity
Tech-Today

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 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;
}




- Hibernate Search Faceting
With the document available from Hibernate Search (https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting), we should be able to implement a faceting example that returns the faceting field and its count. In the example...

- How To Create A Custom Bean Validation In Javaee6
Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint validator. For example: the bean validation article from  oracle javaee6 documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html....

- Validation-summary-errors Div Showing Even If There Is No Error Binded To A Property
I'm working on a change password form when I encountered this strange behavior from mvc3. I created a simple form and change password model with 3 fields (old, new, confirm password). Then I extended ValidationAttribute to validate the password's...

- Rss Feed Reader Implementation In C#
Included in this page: 1.) Rss feed model (which you can customized according to your needs) 2.) Rss feed reader My Objective is to read an rss feed of a news site like google and yahoo: For example we want to read the RSS feed of yahoo news in the Philippines:...

- How To Add A Customize Product Admin Fields In Zencart By Editing/adding Extra Codes
Problem: How to add a product custom field named length. This to consider (modifications in): 1.) database 2.) scripts ----------------------------------- 1.) Database - a custom column should be add to the products table. Execute this line (for column...



Tech-Today








.