| How to convert a jpql filter condition to Expression [message #669663] |
Wed, 11 May 2011 01:25  |
attila Missing name Messages: 45 Registered: April 2010 |
Member |
|
|
Hi,
I have the following scenario, i'm describing it in pseudo code as the concept is what i'm interested in.
There is an entity:
@Entity
MyEntity {
name : String
....
}
Somewhere in the application i create a string:
String my_jpql_condition = " name like :pName ";
then in the core of the application i'd like to do :
ReadAllQuery query = new ReadAllQuery(MyEntity.class);
Expression where = .....
some_way_to_add_my_jpql_condition(where , my_jpql_condition )
query.setSelectionCriteria(where);
I believe there is somewhere in the JPA implementation a way to achieve this either:
translate my_jpql_condition into an expression or translate it into an sql where then append that to my query.
Any hint if this is possible and where to look for ?
Thank you
|
|
|
| Re: How to convert a jpql filter condition to Expression [message #670045 is a reply to message #669663] |
Thu, 12 May 2011 09:58   |
James Sutherland Messages: 1834 Registered: July 2009 |
Senior Member |
|
|
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
|
|
|
| Re: How to convert a jpql filter condition to Expression [message #670298 is a reply to message #670045] |
Fri, 13 May 2011 08:51  |
attila Missing 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 09:22] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.01446 seconds