在Hibernate中获取表列名称 [英] Get table column names in Hibernate

查看:361
本文介绍了在Hibernate中获取表列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用hibernate Criteria 类来获取表的所有记录:

I am using hibernate Criteria class to get all records of table :

Criteria criteria = session.createCriteria(AppTaskConfig.class)

我想获得列名也需要将结果集转换为JSON格式。

I would like to get column names also as I need to convert result set into JSON format.

推荐答案

获取列名称

To get the column names, first you need to find the properties of the entity using org.hibernate.metadata.ClassMetadata:

ClassMetadata classMetadata = sessionFactory.getClassMetadata(AppTaskConfig.class);
String[] propertyNames = classMetadata.getPropertyNames();

其中 propertyNames 是一个String数组 AppTaskConfig 的属性名称。

where propertyNames is an array of Strings representing the property names of AppTaskConfig.

现在使用Hibernate org.hibernate.cfg.Configuration 对象可以找到属性的列名称

Now using Hibernate org.hibernate.cfg.Configuration object you can find the column names of the properties:

for (String property : propertyNames) {
    Configuration configuration = sessionFactoryBean.getConfiguration();
    PersistentClass persistentClass = configuration
                    .getClassMapping(Details.class.getName());
    String columnName = ((Column) persistentClass.getProperty(property)
                    .getColumnIterator().next()).getName();
}

这篇关于在Hibernate中获取表列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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