Using sql column names in hibernate createSQlquery result

后端 未结 3 2044
误落风尘
误落风尘 2020-12-05 10:42

I have a couple of sql views with composite primary keys that I want to query, and since Hibernate makes it a pain to work with composite keyes, I\'m using createSQLQ

3条回答
  •  情深已故
    2020-12-05 11:00

    Your question is ambiguous - in the first paragraph you want to refer to columns by index and in the second, by sql name. Since by index is easy, I'll assume by name.

    First of all, you can use the doWork method to access the underlying JDBC connection and handle it as you would with pure JDBC:

    session.doWork(new Work() {
      public void execute(Connection connection) throws SQLException {
        connection.prepareStatement(...
      }
    });
    

    Or, you can use query.getReturnAliases which returns a String[] of the column names. For effciency, I'd probably build a Map of alias to index and then you can do something like result[map.get("column name")].

    But really, Hibernate handles composite keys pretty easily when using xml mappings (haven't tried with annotations). It's a little more work up front and there are a few issues with complex relationships (mainly when foreign key names/spans don't match), but once you create the id class and map it, you can stick with HQL/Criteria and get all the benefits of lazy loading, simple joins, dirty checking, etc.

提交回复
热议问题