Hibernate,执行本机SQL查询并获取在get()上的列名称

huangapple go评论61阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年10月2日 01:49:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64160726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定