Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Last active March 15, 2017 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/22ce2b57a14a10057b63ef601aaf95c6 to your computer and use it in GitHub Desktop.
Save thjanssen/22ce2b57a14a10057b63ef601aaf95c6 to your computer and use it in GitHub Desktop.
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
private LocalDate publishingDate;
}
Query q = em.createQuery(“SELECT b.title, count(r.id) FROM Book b INNER JOIN Review r ON r.fkBook = b.id GROUP BY b.title”);
Object[] r = (Object[]) q.getSingleResult();
05:44:59,939 DEBUG [org.hibernate.SQL] -
select
book0_.title as col_0_0_,
count(review1_.id) as col_1_0_
from
Book book0_
inner join
Review review1_
on (
review1_.fkBook=book0_.id
)
group by
book0_.title
MultiIdentifierLoadAccess<Book> multi = session.byMultipleIds(Book.class);
List<Book> books = multi.multiLoad(1L, 2L, 3L);
05:44:33,872 DEBUG [org.hibernate.SQL] -
select
book0_.id as id1_0_0_,
book0_.publishingDate as publishi2_0_0_,
book0_.title as title3_0_0_,
book0_.version as version4_0_0_
from
Book book0_
where
book0_.id in (
?,?,?
)
16:15:33,545 DEBUG [org.hibernate.SQL] – insert into Book (price, publishingDate, title, version, id) values (?, ?, ?, ?, ?)
16:15:33,550 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] – binding parameter [1] as [DOUBLE] – [null]
16:15:33,551 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] – binding parameter [2] as [DATE] – [2017-04-04]
16:15:33,552 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] – binding parameter [3] as [VARCHAR] – [Hibernate Tips]
16:15:33,553 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] – binding parameter [4] as [INTEGER] – [0]
16:15:33,554 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] – binding parameter [5] as [BIGINT] – [1]
Stream<Object[]> books = session.createNativeQuery("SELECT b.title, b.publishingDate FROM book b").stream();
books.map(b -> new BookValue((String)b[0], (Date)b[1]))
.map(b -> b.getTitle() + " was published on " + b.getPublishingDate())
.forEach(m -> log.info(m));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment