Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Passing parameters to be used in Customizer
Passing parameters to be used in Customizer [message #673589] Tue, 24 May 2011 04:17 Go to next message
Eclipse UserFriend
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 #673745 is a reply to message #673589] Tue, 24 May 2011 11:38 Go to previous messageGo to next message
Eclipse UserFriend
How are you executing the query?

If you try calling beginEarlyTranaction() on the UnitOfWork associated with the EntityManager first, do you then get the ClientSession or UnitOfWork?

There has been some work done on this in the 2.3 release, you could try the 2.3 release candidate.


Re: Passing parameters to be used in Customizer [message #673767 is a reply to message #673745] Tue, 24 May 2011 13:17 Go to previous message
Eclipse UserFriend
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
Previous Topic: @NamedStoredProcedureQuery and StoredProcedureCall problem
Next Topic:EmbeddedObjects - fields not populated correctly
Goto Forum:
  


Current Time: Wed Jul 23 16:05:37 EDT 2025

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

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

Back to the top