Last active
August 29, 2015 14:20
-
-
Save thjanssen/f6890a1379e318438795 to your computer and use it in GitHub Desktop.
Result Set Mapping: Hibernate specific mappings (http://www.thoughts-on-java.org/result-set-mapping-hibernate-specific-mappings)
This file contains hidden or 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<BookValue> results = ((Session)this.em.getDelegate()).createSQLQuery("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") | |
.addScalar("id", StandardBasicTypes.LONG).addScalar("title").addScalar("version", StandardBasicTypes.LONG).addScalar("authorName") | |
.setResultTransformer(new AliasToBeanResultTransformer(BookValue.class)).list(); | |
results.stream().forEach((book) -> { | |
System.out.println("Book: ID [" + book.getId() + "] title [" + book.getTitle() + "] authorName [" + book.getAuthorName() + "]"); | |
}); |
This file contains hidden or 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
@PersistenceContext | |
private EntityManager em; | |
... | |
public void queryWithAuthorBookCountHibernateMapping() { | |
Session session = (Session)this.em.getDelegate(); | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment