Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created April 19, 2015 06:19
Show Gist options
  • Save thjanssen/7fa350671de484a0127a to your computer and use it in GitHub Desktop.
Save thjanssen/7fa350671de484a0127a to your computer and use it in GitHub Desktop.
Result Set Mapping: Constructor Result Mappings (http://www.thoughts-on-java.org/2015/04/result-set-mapping-constructor.html)
SELECT b.id, b.title, b.version, a.firstName || a.lastName as authorName FROM Book b JOIN Author a ON b.author_id = a.id
@SqlResultSetMapping(
name = "BookValueMapping",
classes = @ConstructorResult(
targetClass = BookValue.class,
columns = {
@ColumnResult(name = "id", type = Long.class),
@ColumnResult(name = "title"),
@ColumnResult(name = "version", type = Long.class),
@ColumnResult(name = "authorName")}))
<sql-result-set-mapping name="BookValueMappingXml">
<constructor-result target-class="org.thoughts.on.java.jpa.value.BookValue">
<column name="id" class="java.lang.Long"/>
<column name="title"/>
<column name="version" class="java.lang.Long"/>
<column name="authorName"/>
</constructor-result>
</sql-result-set-mapping>
List<BookValue> results = this.em.createNativeQuery("SELECT b.id, b.title, b.version, a.firstName || a.lastName as authorName FROM Book b JOIN Author a ON b.author_id = a.id", "BookValueMapping").getResultList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment