Source:
public void validate(Object object, Errors errors) throws DAOException { String name = object.getClass().getSimpleName(); Class entityClass = object.getClass(); for (Field field : object.getClass().getDeclaredFields()) { field.setAccessible(true); if (field.getName().equalsIgnoreCase(name)) { String prala = field.getName(); Object value = null; try { value = field.get(object); } catch (IllegalArgumentException e) { log.warn("Unable to get the value of " + field.getName()); } catch (IllegalAccessException e) { log.warn("Unable to get the value of " + field.getName()); } if (value != null) { int l1 = getMaximumPropertyLength(entityClass, field.getName()); int l2 = value.toString().length(); if (l2 > l1) { errors.rejectValue(field.getName(), " can be at max [" + l1 + "] characters in length."); } } } } ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass); if(metadata != null){ String[] propNames = metadata.getPropertyNames(); for (int i = 0; i < propNames.length; i++) { String propType = metadata.getPropertyType(propNames[i]).getName(); if (propType.equalsIgnoreCase("String")) { int p1 = getMaximumPropertyLength(entityClass, propNames[i]); String str = (String) metadata.getPropertyValue(object, propNames[i]); if(str != null){ int p2 = str.length(); if (p2 > p1) { errors.rejectValue(propNames[i], " can be at max [" + p1 + "] characters in length."); } } } } } FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode(); sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL); try { for (Validator validator : getValidators(object)) { validator.validate(object, errors); } } finally { sessionFactory.getCurrentSession().setFlushMode(previousFlushMode); } }

Target:
public void validate(Object object, Errors errors) throws DAOException { Class entityClass = object.getClass(); ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass); if (metadata != null) { String[] propNames = metadata.getPropertyNames(); Object IdentifierTpye = metadata.getIdentifierType(); String identifierName = metadata.getIdentifierPropertyName(); if (IdentifierTpye instanceof StringType || IdentifierTpye instanceof TextType) { int maxLength = getMaximumPropertyLength(entityClass, identifierName); String identifierValue = (String) metadata.getIdentifier(object,(SessionImplementor) null); if (identifierValue != null) { int identifierLength = identifierValue.length(); if (identifierLength > maxLength) { errors.rejectValue(identifierName, "error.exceededMaxLengthOfField", new Object[] { maxLength }, null); } } } for (int i = 0; i < propNames.length; i++) { Type propType = metadata.getPropertyType(propNames[i]); if (propType instanceof StringType || propType instanceof TextType) { String propertyValue = (String) metadata.getPropertyValue(object, propNames[i]); if (propertyValue != null) { int maxLength = getMaximumPropertyLength(entityClass, propNames[i]); int propertyValueLength = propertyValue.length(); if (propertyValueLength > maxLength) { errors.rejectValue(propNames[i], "error.exceededMaxLengthOfField", new Object[] { maxLength }, null); } } } } } FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode(); sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL); try { for (Validator validator : getValidators(object)) { validator.validate(object, errors); } } finally { sessionFactory.getCurrentSession().setFlushMode(previousFlushMode); } }