Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » ERROR - Predicate "OR" is NULL --solved(Dynamic "OR"-query using Predicate -howto?)
ERROR - Predicate "OR" is NULL --solved [message #1069105] Mon, 15 July 2013 07:15 Go to next message
Hannes Kühtreiber is currently offline Hannes KühtreiberFriend
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 #1069263 is a reply to message #1069105] Mon, 15 July 2013 14:46 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
A conjunction is true, so what you would be are creating is "where true is null" which doesn't make a lot of sense. You can file a bug to have a better error thrown, but I don't see what you are attempting to do with the println statements - they should be removed.

Best Regards,
Chris

Re: ERROR - Predicate is (or is not?) NULL [message #1069271 is a reply to message #1069263] Mon, 15 July 2013 15:10 Go to previous messageGo to next message
Hannes Kühtreiber is currently offline Hannes KühtreiberFriend
Messages: 21
Registered: June 2013
Junior Member
Chris,
I added the println statements to find out where the NullPointer Exception is coming from.

If I omit them and try to build the Predicate like that
where=builder.or(where, builder.like(c.get("name"), searchString));

I still get the NullPointer Exception.
Do you have any Idea what is going wrong?

Hannes
Re: ERROR - Predicate is (or is not?) NULL [message #1069278 is a reply to message #1069271] Mon, 15 July 2013 15:26 Go to previous messageGo to next message
Hannes Kühtreiber is currently offline Hannes KühtreiberFriend
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

Re: ERROR - Predicate is (or is not?) NULL [message #1069764 is a reply to message #1069278] Tue, 16 July 2013 14:07 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Seems like a bug, please log the bug including your code and the exception stack trace.

As a workaround use,

where=builder.like(c.get("name"), searchString);
where=builder.or(where, builder.like(c.get("firstName"), searchString));
query.where(where);


James : Wiki : Book : Blog : Twitter
Re: ERROR - Predicate is (or is not?) NULL [message #1069771 is a reply to message #1069764] Tue, 16 July 2013 14:20 Go to previous messageGo to next message
Hannes Kühtreiber is currently offline Hannes KühtreiberFriend
Messages: 21
Registered: June 2013
Junior Member
Great, this works!
stupid I did not think of it myself ...

I'll find out how to log a bug.

thanks James!
Hannes
Re: ERROR - Predicate is (or is not?) NULL [message #1069795 is a reply to message #1069771] Tue, 16 July 2013 15:17 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
I've logged a bug with a patch at https://bugs.eclipse.org/bugs/show_bug.cgi?id=413084

Re: ERROR - Predicate is (or is not?) NULL [message #1070110 is a reply to message #1069105] Wed, 17 July 2013 09:02 Go to previous message
Hannes Kühtreiber is currently offline Hannes KühtreiberFriend
Messages: 21
Registered: June 2013
Junior Member
I'm quite surprised this bug did not show up earlier, it is not an uncommon query.
Thanks Chris!
Hannes
Previous Topic:EclipseLink-6069 when using BatchFetch and Inheritance
Next Topic:Using Fetch-Group with Many-to-One
Goto Forum:
  


Current Time: Thu Mar 28 17:48:36 GMT 2024

Powered by FUDForum. Page generated in 0.03945 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top