QueryByExamplePolicy not being used on a ReadAllQuery

View: New views
5 Messages — Rating Filter:   Alert me  

QueryByExamplePolicy not being used on a ReadAllQuery

by nickgoodman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I've got some code that appears to correctly use a QueryByExamplePolicy; however, when I look at the SQL generated via TopLink Essentials (I'm using WebLogic 10.3), I see it using equals instead of like for the comparison of attributes in the example object (the result set is clearly the equals query and not the like query).  The following code is what's being executed:


/* searchPImpl has one field set to a value like "test%", and I want it to pickup values such as "test1", "test", "test  23", et cetera */

QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "like");
policy.addSpecialOperation(Integer.class, "equal");
               
ReadAllQuery raq = new ReadAllQuery();
raq.setExampleObject(searchPImpl);
raq.setReferenceClass(searchPImpl.getClass());
raq.setQueryByExamplePolicy(policy);


ClientSession cs = getClientSession();
ArrayList arrayList = new ArrayList((Vector) cs.executeQuery(raq));

Re: QueryByExamplePolicy not being used on a ReadAllQuery

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The code looks correct.  What is the SQL generated?  Is you String value null?

You might want to try the same on EclipseLink, to see if it still occurs (note, this is the EclipseLink forum, not TopLink Essentials).


nickgoodman wrote:
I've got some code that appears to correctly use a QueryByExamplePolicy; however, when I look at the SQL generated via TopLink Essentials (I'm using WebLogic 10.3), I see it using equals instead of like for the comparison of attributes in the example object (the result set is clearly the equals query and not the like query).  The following code is what's being executed:


/* searchPImpl has one field set to a value like "test%", and I want it to pickup values such as "test1", "test", "test  23", et cetera */

QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "like");
policy.addSpecialOperation(Integer.class, "equal");
               
ReadAllQuery raq = new ReadAllQuery();
raq.setExampleObject(searchPImpl);
raq.setReferenceClass(searchPImpl.getClass());
raq.setQueryByExamplePolicy(policy);


ClientSession cs = getClientSession();
ArrayList arrayList = new ArrayList((Vector) cs.executeQuery(raq));

Re: QueryByExamplePolicy not being used on a ReadAllQuery

by nickgoodman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Actually, this was Eclipselink, we've just ported and I mistyped.  And I discovered the problem... Somebody had extended the ReadAllQuery and not renamed it (so only the package was different), and they weren't handling the policies correctly.

Incidentially, this brings up the original question that I was looking into: I'd like to use a custom operation instead of "like".  Is that possible without modifying EclipseLink?

thanks,
  Nick



The code looks correct.  What is the SQL generated?  Is you String value null?

You might want to try the same on EclipseLink, to see if it still occurs (note, this is the EclipseLink forum, not TopLink Essentials).


nickgoodman wrote:
I've got some code that appears to correctly use a QueryByExamplePolicy; however, when I look at the SQL generated via TopLink Essentials (I'm using WebLogic 10.3), I see it using equals instead of like for the comparison of attributes in the example object (the result set is clearly the equals query and not the like query).  The following code is what's being executed:


/* searchPImpl has one field set to a value like "test%", and I want it to pickup values such as "test1", "test", "test  23", et cetera */

QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "like");
policy.addSpecialOperation(Integer.class, "equal");
               
ReadAllQuery raq = new ReadAllQuery();
raq.setExampleObject(searchPImpl);
raq.setReferenceClass(searchPImpl.getClass());
raq.setQueryByExamplePolicy(policy);


ClientSession cs = getClientSession();
ArrayList arrayList = new ArrayList((Vector) cs.executeQuery(raq));


Re: QueryByExamplePolicy not being used on a ReadAllQuery

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The addSpecialOperation can use anything that is a method on the Expression class.
If you need something beyond that, you can use an Expression instead of QBE (or even in addition).  Expressions allow for custom operators and functions.


Actually, this was Eclipselink, we've just ported and I mistyped.  And I discovered the problem... Somebody had extended the ReadAllQuery and not renamed it (so only the package was different), and they weren't handling the policies correctly.

Incidentially, this brings up the original question that I was looking into: I'd like to use a custom operation instead of "like".  Is that possible without modifying EclipseLink?

thanks,
  Nick


James Sutherland wrote:
The code looks correct.  What is the SQL generated?  Is you String value null?

You might want to try the same on EclipseLink, to see if it still occurs (note, this is the EclipseLink forum, not TopLink Essentials).


nickgoodman wrote:
I've got some code that appears to correctly use a QueryByExamplePolicy; however, when I look at the SQL generated via TopLink Essentials (I'm using WebLogic 10.3), I see it using equals instead of like for the comparison of attributes in the example object (the result set is clearly the equals query and not the like query).  The following code is what's being executed:


/* searchPImpl has one field set to a value like "test%", and I want it to pickup values such as "test1", "test", "test  23", et cetera */

QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "like");
policy.addSpecialOperation(Integer.class, "equal");
               
ReadAllQuery raq = new ReadAllQuery();
raq.setExampleObject(searchPImpl);
raq.setReferenceClass(searchPImpl.getClass());
raq.setQueryByExamplePolicy(policy);


ClientSession cs = getClientSession();
ArrayList arrayList = new ArrayList((Vector) cs.executeQuery(raq));

Re: QueryByExamplePolicy not being used on a ReadAllQuery

by nickgoodman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



I want to parse the filter criteria and create a custom expression based upon the "meaning" of that criteria for a wide range of objects.  To that end, I currently use the toplink maps and reflection to handle it.  I would like, however, to simply write code like:

public ArrayList getResults(Object filter) {

ExpressionBuilder bldr = new ExpressionBuilder(filter.getClass());
ReadAllQuery raq = new ReadAllQuery(filter.getClass());
raq.addExpressionBuilder(bldr);
QueryByExamplePolicy qep = new QueryByExamplePolicy();
qep.addSpecialOperation(java.lang.String, new FilteringExpression(bldr), "filterLike");

raq.setQueryByExamplePolicy(qep);

return findObjects(raq);

}

where, FilteringExpression is a class like:

class FilteringExpression {

ExpressionBuilder bldr;

public FilteringExpression (ExpressionBuilder bldr) {
this.bldr = bldr;
}

public Expression filterLike(String x) {

  if("today".equalsIgnoreCase(x))
          return bldr.get("postedDate").equal(Calendar.getInstance());
  if("this month".equalsIgnoreCase(x))
          return bldr.get("postedDate").greaterThan(......

...


This is just a pseudo-code example of the type of thing I'm doing.

  Nick







The addSpecialOperation can use anything that is a method on the Expression class.
If you need something beyond that, you can use an Expression instead of QBE (or even in addition).  Expressions allow for custom operators and functions.


Actually, this was Eclipselink, we've just ported and I mistyped.  And I discovered the problem... Somebody had extended the ReadAllQuery and not renamed it (so only the package was different), and they weren't handling the policies correctly.

Incidentially, this brings up the original question that I was looking into: I'd like to use a custom operation instead of "like".  Is that possible without modifying EclipseLink?

thanks,
  Nick


James Sutherland wrote:
The code looks correct.  What is the SQL generated?  Is you String value null?

You might want to try the same on EclipseLink, to see if it still occurs (note, this is the EclipseLink forum, not TopLink Essentials).


nickgoodman wrote:
I've got some code that appears to correctly use a QueryByExamplePolicy; however, when I look at the SQL generated via TopLink Essentials (I'm using WebLogic 10.3), I see it using equals instead of like for the comparison of attributes in the example object (the result set is clearly the equals query and not the like query).  The following code is what's being executed:


/* searchPImpl has one field set to a value like "test%", and I want it to pickup values such as "test1", "test", "test  23", et cetera */

QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "like");
policy.addSpecialOperation(Integer.class, "equal");
               
ReadAllQuery raq = new ReadAllQuery();
raq.setExampleObject(searchPImpl);
raq.setReferenceClass(searchPImpl.getClass());
raq.setQueryByExamplePolicy(policy);


ClientSession cs = getClientSession();
ArrayList arrayList = new ArrayList((Vector) cs.executeQuery(raq));