Hi,
I use the appfoubdation add-on, it is very nice module
in reason to secure my application, I want to
modify the
persistence-unit parameters in the persistence.xml file,
the
FacadeFactory.registerFacade(âmypersistunitâ,true); set the persistence unit to be used. But I cant modify hers parameters
NB: in the ordinary JPA approach, I use the EntityManager and Properties to modify the the persistence-unit parameters:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mypersistunit");
EntityManager em = null;
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.user", myuser);
properties.put("javax.persistence.jdbc.password", mypassword);
em = emf.createEntityManager(properties);
To be able to do that, you need to extend JPAFacade and do the necessary modifications. The EntityManagerFactory is a protected property called âemfâ so you can access it in your extended implementations. Youâll also need to override the getEntityManager() method to be able to create the EntityManager with your properties.
public class ApplicationsContextListener extends JPAFacade implements ServletContextListener {
......
public void contextInitialized(ServletContextEvent arg0) {
try {
JPAFacade ff = new JPAFacade(){
private static final long serialVersionUID = 1L;
@Override
protected EntityManager getEntityManager() {
// Check if em is null or if the em has been closed.
if ((em.get() == null || !em.get().isOpen()) && emf != null) {
// create a new em if we didn't have a usable one available.
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.user", myuser);
properties.put("javax.persistence.jdbc.password", mypassword);
properties.put("eclipselink.jdbc.exclusive-connection.mode", "Always");
properties.put("eclipselink.jdbc.exclusive-connection.is-lazy", "false");
em.set(emf.createEntityManager(properties));
}
return em.get();
}
};
ff.init("myProjectPersistapp");
FacadeFactory.registerFacade("myProjectPersistapp",true);
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
..................
}
}
But the program consider the initial settings in persistance.xml file
I create other class that extends JPAFacade classe and overriding getEntityManager() method:
public class MyJPAFacade extends JPAFacade {
EntityManagerFactory emf = null;
@Override
public void init(String name) {
emf = Persistence.createEntityManagerFactory(name);
}
@Override
public EntityManager getEntityManager() {
if ((em.get() == null || !em.get().isOpen()) && emf != null) {
// create a new em if we didn't have a usable one available.
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.user",myuser);
properties.put("javax.persistence.jdbc.password",mypassword);
properties.put("eclipselink.jdbc.exclusive-connection.mode", "Always");
properties.put("eclipselink.jdbc.exclusive-connection.is-lazy", "false");
em.set(emf[b]
.createEntityManager(properties)
[/b]);
System.out.println("em: "+em.get().toString());
}
return em.get();
}
}
and I use it in the contextlistener class:
....
public void contextInitialized(ServletContextEvent arg0) {
MyJPAFacade myfacade = new MyJPAFacade();
myfacade.init("myProjectPersistapp");
//register this facade to be used
FacadeFactory.registerFacade(myfacade.getClass(), "myProjectPersistapp", true);
...
}
The same problem the
persistence unit parameters doesnât changes.
have some one any suggestion??
I havenât tested this, so I do not guarantee that it works, but the problem seems to be that you override the init() mehtod but you did not change its implementation, in other words, it works just as it did previously. What I meant is that you should change the way the emf is created, so instead of passing the persistence name to the create method, you should give the properties as a parameter.
public void init(String name) {
Map properties = new HashMap();
properties.put("...", "...");
properties.put("...", "...");
...
emf = Persistence.createEntityManagerFactory(properties);
}
I created a classe MyJPAFacade that extend the JPAFacade classe, and I override the getEntityManager() method. then, I instantiate a MyJPAFacade object in my ApplicationsContextListener class: