reset password
Author Message
cysun
Posts: 2935
Posted 15:55 Nov 21, 2011 |

In the lectures notes I pointed to the wiki page search as an example, but it turned out that the wiki page search example didn't work. I'm very very sorry about that.

To make the example work you'll need to add the following to the <sql-query> in NamedQueries.hbm.xml:

<return class="csns.util.WikiPageSearchResult" />

Also WikiPageSearchResult needs to be annotated as an @Entity instead of just @Embeddable. Because an @Entity class needs an id field, you'll need to add an id field and change the query a little bit to return the wiki page id as well.

The problem is basically how to map the columns returned by a native SQL query to an object. There are a few ways to do that:

a) The easiest way is when the result class happens to be an entity class (like mailing list message search in CSNS2). In this case the mapping is already known so there's no additional work necessary. My mistake is assuming that @Embeddable works the same as @Entity for this purpose, but apparently not.

b) If you use Hibernate without JPA, you can use something called ResultTransformer, e.g.

SQLQuery sql = (SQLQuery) session.getNamedQuery( "wiki.page.search" );
sql.setResultTransformer( Transformers.aliasToBean( WikiPageSearchResult.class ) );

Some other JPA implementations may also have utility classes for this purpose, e.g. EclipseLink. Unfortunately there's no easy way to do native query result mapping to non-entity class in JPA yet. According to the author of the EclipseLink blog post, JPA 2.1 may address this problem