ERROR - Predicate "OR" is NULL --solved [message #1069105] |
Mon, 15 July 2013 07:15 |
Hannes Kühtreiber Messages: 21 Registered: June 2013 |
Junior Member |
|
|
Hello everybody,
For a RCP application I try to build a dynamic query: depending on user input several search criteria should be concatenated.
I used a Predicate, but it returns a NullPointerException.
What I did is:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery query = builder.createQuery();
Root c = query.from(ContactImpl.class);
Predicate where = builder.conjunction();
where.alias("MyNameIsAlias");
System.out.println("## where="+where);
System.out.println("## alias="+where.getAlias());
System.out.println("## where=null? "+where.isNull());
where=builder.or(where, builder.like(c.get("name"), searchString));
where=builder.or(where, builder.like(c.get("firstName"), searchString));
where=builder.or(where, builder.like(c.get("lastName"), searchString));
query.where(where);
However, unfortunately the method fails before getting to the search criteria. The predicate has an id, but when I ask if its null I get the exception:
## where=org.eclipse.persistence.internal.jpa.querydef.PredicateImpl@180b22e
## alias=MyNameIsAlias
Exception in thread "main" java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.querydef.ExpressionImpl.isNull(ExpressionImpl.java:147)
I am very confused, first the predicate is there, then it is null !?
(just added the alias to test, I get the same result without it.)
When I did the query like this, all was fine:
// query.where(builder.or(
// (builder.like(c.get("name"), searchString)),
// (builder.like(c.get("firstName"), searchString)),
// (builder.like(c.get("lastName"), searchString))
// )
What is going wrong here?
How else could I construct a dynamic query like this?
Any hints are wellcome!
Hannes
--using Eclipse Juno Service Release 2 and
EclipseLink version: 2.5.0.v20130507-3faac2b
[Updated on: Thu, 18 July 2013 06:41] Report message to a moderator
|
|
|
|
|
Re: ERROR - Predicate is (or is not?) NULL [message #1069278 is a reply to message #1069271] |
Mon, 15 July 2013 15:26 |
Hannes Kühtreiber Messages: 21 Registered: June 2013 |
Junior Member |
|
|
Chris,
thanks for pointing me in this direction.
conjunction/"and" works,
Predicate where = builder.conjunction();
where=builder.and(where, builder.like(c.get("name"), searchString));
where=builder.and(where, builder.like(c.get("firstName"), searchString));
query.where(where);
but disjunction/"or" does not:
Predicate where = builder.disjunction();
where=builder.or(where, builder.like(c.get("name"), searchString));
where=builder.or(where, builder.like(c.get("firstName"), searchString));
query.where(where);
This code still produces the NullPointer Exception.
Exception in thread "main" java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.querydef.CriteriaBuilderImpl.or(CriteriaBuilderImpl.java:413)
How do you construct a select-or statement dynamically?
Hannes
[Updated on: Mon, 15 July 2013 16:10] Report message to a moderator
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02701 seconds