|
Re: How to convert a jpql filter condition to Expression [message #670045 is a reply to message #669663] |
Thu, 12 May 2011 13:58 |
|
Can you store an Expression instead of JPQL fragment, or append the fragment to a JPQL query instead of Expression?
You can convert JPQL to an Expression, but it is not simple.
First you need an alias in your fragment, such as using "this"
String fragment = " this.name like :pName ";
String jpql = "select this from Foo this where " + fragment;
JPQLParseTree parseTree = JPQLParser.buildParseTree(jpql);
parseTree.setClassLoader(session.getLoader());
DatabaseQuery databaseQuery = parseTree.createDatabaseQuery();
databaseQuery.setJPQLString(jpql);
parseTree.populateQuery(databaseQuery, (AbstractSession) session);
Expression expression = databaseQuery.getSelectionCriteria();
James : Wiki : Book : Blog : Twitter
|
|
|
Re: How to convert a jpql filter condition to Expression [message #670298 is a reply to message #670045] |
Fri, 13 May 2011 12:51 |
attila Mising name Messages: 45 Registered: April 2010 |
Member |
|
|
Thank you James,
I was kind of afraid of this answer ...
I have a view-model declaration as shown below where the annotations create some descriptors at start-up
used by a service runtime engine (create fetch groups, filter, sort, query, save, etc).
For decent situations a base service does all the job, for complex ones specific services are mapped.
As some situations are too complex for jpql i prefered to use directly the native api in the engine.
What i'd like is to declare here in a way or other (in annotation or some statics or whatever )
the default where per view-model and some filter rules per field.
In order to increase flexibility i thought some jpql fragments would be the most interesting option
(i wouldn't create a custom service just because of an average filter rule )
I wouldn't mind to use expressions but i don't see how in this context.
@DsModel(entity = Employee.class,
sort = { @SortField(field=EmployeeDs.fLASTNAME) }
)
public class EmployeeDs extends AbstractDsModel<Employee> implements
IModelWithId, IModelWithClientId {
public static final String fID = "id";
public static final String fFIRSTNAME = "firstName";
public static final String fLASTNAME = "lastName";
public static final String fEMPLOYERID = "employerId";
public static final String fEMPLOYERCODE = "employerCode";
...
@DsField
private Long id;
@DsField(path = "employer.id", join = "left")
private Long employerId;
@DsField(path = "employer.code", join = "left")
private String employerCode;
@DsField
private String firstName;
@DsField
private String lastName;
...
}
Thank you
[Updated on: Fri, 13 May 2011 13:22] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03313 seconds