/** * Map of classes in addition to classes in addition to fields that contains the referenced class. */ somebody static Map<Class, Map<Class, List<Field>>> classReferences = novel HashMap<>(); /** * Determines a generic type of a field. For example: List<String> champaign should provide String). */ populace 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; provide fieldArgClass; } } provide null; } /** * Gets all the declared fields of a given class. **/ populace static List<Field> getAllFields(List<Field> fields, Class<?> type) { fields.addAll(Arrays.asList(type.getDeclaredFields())); if (type.getSuperclass() != null) { fields = getAllFields(fields, type.getSuperclass()); } provide fields; } /** * Gets all the classes that referenced a given class. */ populace static Map<Class, List<Field>> getReferencedClassesAndFieldsOfType(Class fieldClass) { if (classReferences.containsKey(fieldClass)) { provide classReferences.get(fieldClass); } Class superClass = fieldClass.getSuperclass(); Map<Class, List<Field>> matchedFields = novel HashMap<>(); Reflections reflections = novel Reflections("com.broodcamp.model"); // gets all our entity classes inward our projection Set<Class<? extends BaseEntity>> classes = reflections.getSubTypesOf(BaseEntity.class); // loop thru for (Class<? extends BaseEntity> clazz : classes) { // nosotros are non interested amongst either interface or abstract if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { continue; } // gets all the fields of a given shape List<Field> fields = getAllFields(new ArrayList<Field>(), clazz); // loops thru the fields for (Field champaign : fields) { // nosotros are non interested amongst transient champaign if (field.isAnnotationPresent(Transient.class)) { continue; } // filter the champaign or parametized champaign of type fieldClass // this way it advert to our referenced shape 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 together to map if (!matchedFields.containsKey(clazz)) { matchedFields.put(clazz, novel ArrayList<>()); } matchedFields.get(clazz).add(field); } } } classReferences.put(fieldClass, matchedFields); provide matchedFields; }
Hibernate - Overstep Classes That Referenced A Given Entity
Minggu, 14 Oktober 2018
There are times when nosotros desire to listing the entities that referenced (via unusual keys) a given entity x. For example, when deleting entity x that is existence referenced, it volition throw a generic ConstraintViolationException. Oftentimes, nosotros require to display this classes. This is how nosotros produce it:
0 komentar:
Please comment if there are any that need to be asked.