Hibernate and java 8 lambda's
Asked Answered
M

2

11

Since the introduction of Java 8, is Hibernate waiting some changes? specially

  1. Is there any way to write queries with lambdas in Hibernate? (i.e. like a .net Linq-to-SQL style)

  2. If not, when it's coming (If it's planned to come).

for example something like these:

User u1 = dbo.Users.firstOrDefault(f -> f.userId = 10);

List<User> users = dbo.Users.selectMany(w -> w.userId > 5);
Montpellier answered 3/7, 2014 at 20:41 Comment(2)
ORM libraries usually want to let the database do the queries. But there is no way of translating compiled Java 8 lambdas into SQL queries.Endlong
I'm also looking for the same thing. Afaik, Hibernate's criteria only allows to manipulate fields by their name as String, right? That's a main thing I'd love to see improvedJacobba
D
8

I thought it could not be done too. But I saw Jinq http://www.jinq.org/ They do it for raw SQL queries.

database.customerStream().where(
customer -> customer.getName().equals("Alice"));

So I think it just depends on someone implement the same kind of logic being used in Jinq for JPA entities, and generate JPQL queries.

UPDATE: They do it for JPA too. http://www.jinq.org/docs/gettingstartedjpa.html

Diagnostic answered 30/9, 2014 at 1:49 Comment(2)
getting a 403 on your link to JinqZirconia
Fixed the 403 you mentionedDiagnostic
Q
1

I created a library that allows you to write JPA Criteria API queries as lambda expressions - easy to read and write, type safe and much shorter than regular JPA Criteria API.

JPA has its Criteria API, which is rather hard to write, since there are several objects you must work with (Root, CriteriaQuery ...). This library takes away the boilerplate part and still allows you to use all the power of the standardized JPA Criteria API.

Example:

List<E> entities =  findWhereOrdered(
  (cb, root) -> new PredicateAndOrder(
    cb.equal(root.get(Vendor_.name), name),
    List.of(OrderBy.asc(root.get(Vendor_.id)))));
Query answered 1/8, 2023 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.