Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda in Java - Could not analyze lambda code

Tags:

java

lambda

jpa

I’ve got an application with Hibernate (JPA) which I am using in combination with Jinq. I’ve got a table which lists entities and I want the user to be able to filter it. In the table there are persons listed.

@Entity
public class Person {
    private String firstName;
    private String surName;
    @Id
    private int id;
    public Person() {
    }
    public Person(final String pFirstName, final String pSurName, final int pID) {
        firstName = pFirstName;
        surName = pSurName;
        id = pID;
    }
    public int getId() {
        return id;
    }
    public void setId(final int pID) {
        id = pID;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(final String pFirstName) {
        return firstName = pFirstName;
    }
    public String getSurName() {
        return surName;
    }
    public void setSurName(final String pSurName) {
        surName = pSurName;
    }
}

I am using JavaFX for this, but this shouldn’t matter. First thing I tried was to filter the persons by their surname. For filtering, I used Jinq in combination with lambda. My filtering code looks like this:

private List<Person> getFilteredPersons(final String pSurName){
    JPAJinqStream<Person> stream = streamProvider.streamAll(Person.class);
    stream.where(person -> person.getSurName().contains(pSurName));
    List<Person> filteredList = stream.toList();
    stream.close();
    return filteredList;
}

So the object I am operating on is a normal String. I don’t think that my Person class has anything to do with that. My first thought was, that you can’t use the method boolean contains(...) in lambda because when the error showed up, it said:

Caused by: java.lang.IllegalArgumentException: Could not analyze lambda code

So my question is, is it somehow possible to use the contains-method of a String in lambdacode?

like image 975
Basti Avatar asked Jul 10 '15 06:07

Basti


People also ask

What are the two conditions required for using a lambda function in a stream?

In order to match a lambda to a single method interface, also called a "functional interface", several conditions need to be met: The functional interface has to have exactly one unimplemented method, and that method (naturally) has to be abstract.

Does lambda expression improve performance?

Oracle claims that use of lambda expressions also improve the collection libraries making it easier to iterate through, filter, and extract data from a collection. In addition, new concurrency features improve performance in multicore environments.


1 Answers

Your question has nothing to do with JPA or lambdas, but everything to do with jinq: it simply doesn't support translating String.contains() to a database query. See http://www.jinq.org/docs/queries.html#N65890 for what is supported.

like image 118
JB Nizet Avatar answered Oct 18 '22 04:10

JB Nizet