Passing parameters to be used in Customizer [message #673589] |
Tue, 24 May 2011 04:17  |
Eclipse User |
|
|
|
Hi, I'm trying to define a Customizer to an Eclipselink entity, using the annotation @Customizer. The code of the customizer is working well, however I need to pass a parameter to be used in a subquery inside the customizer. In the ReportQuery.setSelectionCriteria I put an Expression like: subQuery.getExpressionBuilder().getTable("User").getField("id").equal(new PropertyValue("customID"));
The class PropertyValue is an instanceof ParameterExpression which will be called when the customizer is invoked (the method "public Object getValue(AbstractRecord translationRow, AbstractSession session)" is called). I'm trying to get a property passed by the EntityManager, however I only receive the properties defined in persistence.xml. I use the following code to create the EntityManager is each query:
...
private static EntityManagerFactory factory;
...
public synchronized static EntityManager getEntityManager() {
if (factory == null) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
EntityManager entityManager = factory.createEntityManager();
}
With the previous code, a new EntityManager is created for each query, however when the "getValue" of ParameterExpression is called, only the ServerSession (the one created by the "Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);") is received and only the properties defined in the persistence.xml are accessible.
How can I pass a property/parameter value to the Customizer that will be supplied dynamically per EntityManager in each query?
Many thanks,
Pedro Oliveira
|
|
|
|
Re: Passing parameters to be used in Customizer [message #673767 is a reply to message #673745] |
Tue, 24 May 2011 13:17  |
Eclipse User |
|
|
|
Hi! Thanks for the answer.
I'm running the query like this:
...
private static EntityManagerFactory factory;
...
public synchronized static EntityManager getEntityManager() {
if (factory == null) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
EntityManager entityManager = factory.createEntityManager();
}
...
EntityManager entityManager = getEntityManager();
entityManager.setProperty("customID", 10);
Query query = entityManager.createQuery("SELECT usr FROM User usr");
List resultList = query.getResultList();
entityManager.close();
The Customizer of the User entity is something like this:
...
public void customize(ClassDescriptor descriptor) throws Exception {
ExpressionBuilder eb = new ExpressionBuilder(descriptor.getJavaClass());
ExpressionBuilder builder = new ExpressionBuilder(Network.class);
ReportQuery subQuery = new ReportQuery(Network.class, new ExpressionBuilder());
subQuery.addAttribute("networks", subQuery.getExpressionBuilder().getTable("NE_US").getField("idnetwork"));
subQuery.setSelectionCriteria(subQuery.getExpressionBuilder().getTable("NE_US").getField("idusers").equal(new PropertyValue("customID")));
subQuery.addArgument("customID");
Expression e = eb.get("idnetwork").in(subQuery);
descriptor.getQueryManager().setAdditionalJoinExpression(e);
}
All the code work well, except passing the parameter from the entityManager to the PropertyValue class (instance of ParameterExpression).
Thanks,
Pedro Oliveira
|
|
|
Powered by
FUDForum. Page generated in 0.31684 seconds