简体   繁体   English

Hibernate - 如何在不映射到特定类的情况下执行命名本机查询

[英]Hibernate - How to execute named native query without mapping to a specific class

I have a very complicated query that doesn't convert to HQL, so I must use SQL. 我有一个非常复杂的查询,不会转换为HQL,所以我必须使用SQL。 I put my query into a NamedNativeQuery annotation. 我将查询放入NamedNativeQuery注释中。 It "should" return a list of two values ( List<Object[2]> ). 它“应该”返回两个值的列表( List<Object[2]> )。 When I try to run, the session fails to load the query because I haven't defined a mapping or result class. 当我尝试运行时,会话无法加载查询,因为我没有定义映射或结果类。 I cannot create a simple class to map these values to because the annotations have to go in a specific DAO project and the code that is using the queries exists in its own project. 我无法创建一个简单的类来映射这些值,因为注释必须放在特定的DAO项目中,并且使用查询的代码存在于自己的项目中。 (The DAO project is more general-purpose and the entities in that project map directly to tables in our database. I cannot just create a small class in this project to map to my query result because it wouldn't fit the schema of this project) (DAO项目更通用,项目中的实体直接映射到我们数据库中的表。我不能在这个项目中创建一个小类来映射到我的查询结果,因为它不适合这个项目的模式)

Is there a way for me to map the query results to a more generic class, or even better is there some way for me to just get a List<Object[]> back without having to map at all to anything particular? 有没有办法让我将查询结果映射到更通用的类,或者甚至更好的是我有一些方法可以让我只需要一个List<Object[]>而无需映射任何特定的东西? This is extremely frustrating that Hibernate has to work in this particular way when all I want to do is execute a query and get the result back as an array. 当我想要做的就是执行查询并将结果作为数组返回时,Hibernate必须以这种特殊方式工作,这是非常令人沮丧的。

This is the code that I use for storing the result of a SQL query in a List<Object[]> . 这是我用于在List<Object[]>存储SQL查询结果的代码。 Maybe you can adapt it to what you need: 也许你可以根据自己的需要调整它:

    public List<Object[]> executeSelectQuery(String sqlQuery) {

       List<Object[]> cleanedResults = new ArrayList<Object[]>();

       SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sqlQuery);
       List<Object[]> hibernateResults = query.list();

       // Hibernate does not return always a List<Object[]>, but a list of strings or integers, so it is necessary to check the returned values

       if (!hibernateResults.isEmpty()) {
           if (hibernateResults.get(0) instanceof Object[]) {
               cleanedResults = hibernateResults;
           } else {
               Object[] row;
               // Use a 'for' because 'foreach' sometimes has casting exceptions converting to object
               for (int i = 0; i < hibernateResults.size(); i++) {
                  row = new Object[1];
                  row[0] = hibernateResults.get(i);
                  cleanedResults.add(row);
               }
           }
        }

        return cleanedResults;
    }

A NamedNativeQuery supports an addition annotation to allow you map the result to a POJO: NamedNativeQuery支持附加注释,允许您将结果映射到POJO:

@SqlResultSetMapping(name="MyResult.Mapping", entities = {
    @EntityResult(entityClass=MyResult.class, fields = {
        @FieldResult(name="email", column="email"),
        @FieldResult(name="name", column="name")
    })
})

Then add resultSetMapping="MyResult.Mapping" to your NamedNativeQuery annotation. 然后将resultSetMapping="MyResult.Mapping"添加到NamedNativeQuery注释中。

I ended up not using NamedNativeQueries because they weren't working for me. 我最终没有使用NamedNativeQueries,因为它们不适合我。 Instead, I just used session.createSQLQuery with constant Strings, and it worked. 相反,我只使用带有常量字符串的session.createSQLQuery ,它起作用了。

I probably didn't phrase my question properly. 我可能没有正确地说出我的问题。 I could not use a @SqlResultSetMapping because I didn't have any classes to map to based on the structure of the code I am working with. 我无法使用@SqlResultSetMapping因为根据我正在使用的代码的结构,我没有要映射到的任何类。 My query has to come out as a List<Object[]> result, and there is no way to do that with NamedNativeQueries because Hibernate doesn't support it. 我的查询必须List<Object[]>结果出现,并且没有办法使用NamedNativeQueries,因为Hibernate不支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM