2

I have standalone Java application using JPA. I'm looking to load persistance.xml file from user configured folder. I know JPA vendor checks for meta-inf folder and loads it automatically.

Using this way mentioned below, it is working from any folder path inside the source folder but its not working if I need to load it from other project folders other than source folders. For my application, config folder will be in root folder of project or I might need to get from URL.

  properties.put("eclipselink.persistencexml", "config/persistence.xml");

Also, i tried with this classloader overwriting mechanism and it didn't work.

        Thread.currentThread().setContextClassLoader(new ClassLoader() {
        @Override
        public Enumeration<URL> getResources(String name) throws IOException                                                                                                                                                                                                                                                        
        {
            if (name.equals("META-INF/persistence.xml")) {
                return Collections.enumeration(Arrays.asList(new File("/config/persistence.xml").toURI().toURL()));
            }
            return super.getResources(name);
        }
        });

I will need to load persistance.xml manually from folder other than folder from source where classloader is scanning for classes. Does anybody know how to do it with eclipselink/toplink JPA in non-applicaiton server related environments?

  Map properties = new HashMap();
  //properities
  properties.put("eclipselink.************", "*****"); 



  javax.persistence.EntityManagerFactory emf_myDB=javax.persistence.Persistence.createEntityManagerFactory(DB_PERSISTENCE_UNIT_NAME, properties);
Samdalton
  • 21
  • 5
  • 2
    Possible duplicate of http://stackoverflow.com/questions/13717324/is-it-possible-to-have-persistence-xml-in-a-location-other-than-meta-inf – vinay Apr 20 '15 at 19:48
  • comething like that: http://stackoverflow.com/questions/13717324/is-it-possible-to-have-persistence-xml-in-a-location-other-than-meta-inf see last post. – pL4Gu33 Apr 20 '15 at 19:48

1 Answers1

3

It's not possible, at least not without modifying classloader environment.

Reason:
Under the hood, EclipseLink solely uses ClassLoader.getResources() to get the contents of the persistence.xml.
With this method it's impossible to reach resources outside classloader scope (which is the classpath in many classloader implementations).

The only thing you could theoretically do is to use a custom classloader which behaves differently, something like described here (even if it didn't worked for you).
However, I'd consider that as a hack in most cases and hence I would not use it in an enterprise application.

The more interesting question is why you want to do this.
A persistence unit is always coupled to a specific set of entity classes, hence there is no reason why a user would need to specify another persistence.xml (location). If the user should be able to set custom persistence unit properties - use a configuration mechanism of your choice and pass the user customized properties as a Map during EntityManagerFactory creation.

Community
  • 1
  • 1
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
  • Thanks, I already evaluated that classloader mechanism before posting here but it's failing to override the property "META-INF/persistence.xml" with given folder path out side of classloader scope for some reason. May be I need to look into URLclassloader. I have a custom application framework which has in built mechanism to expose a parameter to load the property driven configuration file from its work space which is visible to admin users. – Samdalton May 05 '15 at 14:01
  • So that they can update DB connectivity parameters in different hosts to connect from java based applications (JPA driven application in this case) unlike updating with unjaring and jaring. – Samdalton May 05 '15 at 14:01