Last active
June 15, 2016 22:44
-
-
Save thjanssen/cb1061a30b17e2288223 to your computer and use it in GitHub Desktop.
Result Set Mapping: The Basics (http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Author> results = this.em.createNativeQuery("SELECT a.id, a.firstName, a.lastName, a.version FROM Author a", Author.class).getResultList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SqlResultSetMapping( | |
name = "AuthorMapping", | |
entities = @EntityResult( | |
entityClass = Author.class, | |
fields = { | |
@FieldResult(name = "id", column = "authorId"), | |
@FieldResult(name = "firstName", column = "firstName"), | |
@FieldResult(name = "lastName", column = "lastName"), | |
@FieldResult(name = "version", column = "version")})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<sql-result-set-mapping name="AuthorMappingXml"> | |
<entity-result entity-class="org.thoughts.on.java.jpa.model.Author"> | |
<field-result name="id" column="authorId"/> | |
<field-result name="firstName" column="firstName"/> | |
<field-result name="lastName" column="lastName"/> | |
<field-result name="version" column="version"/> | |
</entity-result> | |
</sql-result-set-mapping> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT a.id as authorId, a.firstName, a.lastName, a.version FROM Author a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Object[]> results = this.em.createNativeQuery("SELECT a.id, a.firstName, a.lastName, a.version FROM Author a").getResultList(); | |
results.stream().forEach((record) -> { | |
Long id = ((BigInteger) record[0]).longValue(); | |
String firstName = (String) record[1]; | |
String lastName = (String) record[2]; | |
Integer version = (Integer) record[3]; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Author> results = this.em.createNativeQuery("SELECT a.id as authorId, a.firstName, a.lastName, a.version FROM Author a", "AuthorMapping").getResultList(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment