Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting column length from Hibernate mappings?

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?

like image 936
James McMahon Avatar asked Feb 05 '09 17:02

James McMahon


1 Answers

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();
    }
  }
like image 76
Brian Deterling Avatar answered Sep 30 '22 18:09

Brian Deterling