hibernate 通过类属性名得到数据库表字段名

因为项目的变态需求,想使用hibernate,同时又要使用SP而不用hibernate的自动生成的sql。因此做的过程中就面临这样的问题,如何通过POJO类的属性名得到数据库表的字段名,我们知道,这两者的映射关系是维护在xml文件中的。

下面的代码可以做到
[code lang="java"]
String columnName(String name) {
PersistentClass mapping = configuration.getClassMapping(ExtendedPerson.class.getName());
Property property = mapping.getProperty(name);
if(property.isComposite()){
Component comp = (Component) property.getValue();
property = comp.getProperty(StringHelper.unroot(name));
assert ! property.isComposite(); //go only one level down
}
Iterator<?> columnIterator = property.getColumnIterator();
Column col = (Column) columnIterator.next();
assert ! columnIterator.hasNext();
return col.getName();
}
[/code]

如果getProperty不好用的话,可以试下getRecursiveProperty这个方法。
参考:Get column name of property mapped with Hibernate

项目是能过Spring来加载Hibernate配置的,类似下面的。
[code lang="xml"]
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
[/code]

如何从Spring来获取hibernate的Configuration呢?
代码如下
[code lang="java"]
public String getColumnName(String sessionFactoryBeanId, String persistentClassName, String propertyName)
{
if (log.isInfoEnabled()) {
log.info("ENTER");
}
String columnName = null;
try{
BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance(CFG_BEANS);
BeanFactoryReference bf = bfl.useBeanFactory("application.services");
applicationContext = (ApplicationContext)bf.getFactory();
LocalSessionFactoryBean localSessionFactory = (LocalSessionFactoryBean)applicationContext.getBean("&" + sessionFactoryBeanId);
Property property = localSessionFactory.getConfiguration().getClassMapping(persistentClassName).getRecursiveProperty(propertyName);
columnName = ((Column)property.getColumnIterator().next()).getName();
}
catch (Exception e) {
log.info("mapping for propertyname to column exception");
}
if (log.isInfoEnabled()) {
log.info("EXIT");
}

return columnName;
}
[/code]

sessionFactoryBeanId传进来的值就是配置文件中的beanID,即”sessionFactory”, 记得在这个串前面加上&符号,否则getBean方法得到的不是真正的LocalSessionFactoryBean,而是由Spring生成的sessionBean。
spring的官方解释:Customizing instantiation logic with a FactoryBean

参考:http://emarketingecuador.com/naty/shopizer/sm-core/src/com/salesmanager/core/util/SpringUtil.java
How can I get the Hibernate Configuration object from Spring?

2 thoughts on “hibernate 通过类属性名得到数据库表字段名

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>