Java 在 Hibernate 中获取表列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19418500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:59:50  来源:igfitidea点击:

Get table column names in Hibernate

javajsonhibernate

提问by abhinav singh

I am using hibernate Criteriaclass to get all records of table :

我正在使用休眠Criteria类来获取表的所有记录:

Criteria criteria = session.createCriteria(AppTaskConfig.class)

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

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

回答by StanislavL

What are column names of entity? Entity could use some of the table's columns and don't have fields for other.

实体的列名是什么?实体可以使用表的某些列,而没有其他字段。

Entity could extend another Entity class so fields of the Entity class are represented by columns from different tables.

Entity 可以扩展另一个 Entity 类,因此 Entity 类的字段由来自不同表的列表示。

You can run native SQl query and see table columns.

您可以运行本机 SQL 查询并查看表列。

回答by Debojit Saikia

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

要获得column names,首先您需要properties使用org.hibernate.metadata.ClassMetadata以下方法找到实体的:

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

where propertyNamesis an array of Strings representing the property names of AppTaskConfig.

其中propertyNames是表示 的属性名称的字符串数组AppTaskConfig

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

现在使用 Hibernateorg.hibernate.cfg.Configuration对象,您可以找到以下项的列名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();
}

回答by kanchan shegar

String[] columnNames = getSessionFactory().getClassMetadata(Employee.class).getPropertyNames();

  org.hibernate.type.Type[] columnTypes = getSessionFactory().getClassMetadata(Employee.class).getPropertyTypes();

try this this is work properly...

试试这个,这是正常工作...

回答by carlosyague

You can use Reflection API to access to declared JPA annotations of a field/getter like Column o JoinColumn.

您可以使用反射 API 来访问字段/getter 的声明 JPA 注释,例如 Column o JoinColumn。

import javax.persistence.Column;
import javax.persistence.JoinColumn;

// get declared field
Field field = persistentClass.getDeclaredField("property");

// get declared method
Method method = persistentClass.getMethod("get"+property.substring(0,1).toUpper()+property.substring(1)));

// access to declared annotations or field or method to find Column or JoinColumn

回答by jpozorio

An easier way to get column name with hibernate 5

使用 hibernate 5 获取列名的更简单方法

final AbstractEntityPersister classMetadata = (AbstractEntityPersister) sessionFactory.getClassMetadata(clazz)
final String[] names = classMetadata.getPropertyColumnNames(property)

With hibernate 4, I do just like @Debojit Saikia

使用 hibernate 4,我就像@Debojit Saikia