FacadeFactory in Appfoundation

Hi,
I use the appfoubdation add-on, it is very nice module :slight_smile:
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 :frowning:
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);


have some one any idea to do this?
thanks

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.

Hi,
Yes, I done this in the contextlistener :


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 :frowning:

Right, didn’t expect that. You’ll probably have to override the init() method as well and define yourself how the EMF is created.

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 :frowning: 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:


 MyJPAFacade myfacade = new MyJPAFacade();
		myfacade.init("myProjectPersistapp");
		
		 
    	try {
	//FacadeFactory.registerFacade("myProjectPersistapp",true);
	FacadeFactory.registerFacade(myfacade.getClass(), "myProjectPersistapp", true);
        .............

and it works!
thanks