从Hibernate映射获取列长度? [英] Getting column length from Hibernate mappings?

查看:109
本文介绍了从Hibernate映射获取列长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要验证我收到的数据,我需要确保长度不会超过数据库列长度。现在所有的长度信息都存储在Hibernate映射文件中,有没有以编程方式访问这些信息?

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?

推荐答案

但它不容易。您可能希望在启动时执行下面的操作,并存储值的静态缓存。有很多特殊情况处理(继承等),但它应该适用于简单的单列映射。我可能已经省略了一些instanceof和null检查。

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

这篇关于从Hibernate映射获取列长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆