英文:
Hibernate, making a native sql query and getting column names for the things on get()
问题
以下是翻译好的内容:
给定这个例子:
NativeQuery query = query();
try (ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY)) {
int i = -1; while (scroll.next()) {
++i; // 做一些操作
}
}
当我执行scroll.next()
时,我可以使用scroll.get(i)
;
然而,我无法找到一种获取通常在SELECT语句中获得的列名的方法。通常在ResultSet
上,可以通过i
获取columnName
。
在那个类中有一个getResultSet()
方法,但它是私有的,本来可以这样做:
scroll.getResultSet().getMetaData().getColumnName(i)
但现在不再可能,而且即使可能也不够优雅。
英文:
Given this example:
NativeQuery query = query();
try ( ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY) ) {
int i = -1; while ( scroll.next() ) {
++i; // DO SOMETHING
}
}
When I do scroll.next() i can do scroll.get(i)
;
However, I can not see a way to get the column name that you normally get on a select. Normally you would on a ResultSet
get the columName
for i
.
On that class is getResultSet() but is private which would have allowed:
scroll.getResultSet().getMetaData().getColumnName(i)
But is now not possible, and not pretty even if it was.
答案1
得分: 2
如果您使用本机查询,为什么不直接使用JDBC API?
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
}
});
就像您在这里的类似问题中提到的:https://stackoverflow.com/questions/64161381/hibernate-get-underlying-sql-resutlset-nativequery-is-very-limiting/64167706
英文:
If you work with a native query, why don't you use the JDBC API directly?
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
}
});
Like suggested in your similar question here: https://stackoverflow.com/questions/64161381/hibernate-get-underlying-sql-resutlset-nativequery-is-very-limiting/64167706
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论