Alex Rivera | Logout

Getting column length from Hibernate mappings?

Asked 2009-02-05T17:20:39.883
9

To validate data I am receiving I need to make sure that the length is not going to exceeded a database column length. Now all the length information is stored in the Hibernate mapping files, is there anyway to access this information programmatically?

Edit
Report

1 Answer

7

You can get to it but it's not easy. You might want to do something like below at startup and store a static cache of the values. There are a lot of special cases to deal with (inheritance, etc), but it should work for simple single-column mappings. I might have left out some instanceof and null checks.

for (Iterator iter=configuration.getClassMappings(); iter.hasNext();) {
    PersistentClass persistentClass = (PersistentClass)iter.next();
    for (Iterator iter2=persistentClass.getPropertyIterator(); iter2.hasNext();) {
       Property property = (Property)iter2.next();
       String class = persistentClass.getClassName();
       String attribute = property.getName();
       int length = ((Column)property.getColumnIterator().next()).getLength();
    }
  }
answered 2009-02-06T01:50:49.823

Your Answer