2

I realize it is always possible to parse the persistence.xml myself, but I would like to know if there is any better way to extract the properties from persistence.xml, for example the values of the database driver/url/user/password, in a way working for both Hibernate and EclipseLink (and hopefully all other implementations too) ?

I suppose you should be able to use the method "EntityManagerFactory.getProperties()" for this purpose but maybe there is a bug in EclipseLink ?

When I am using a file "/META-INF/persistence.xml" like this:

<persistence ...>
    <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        ...
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/MyH2DatabaseFile"/>
            ...

Then the following code works as I expected for Hibernate (5.0.7 which is the currently latest version) to retrieve the database url value:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPersistenceUnit");
Map<String, Object> properties = emf.getProperties();
System.out.println(properties.get("javax.persistence.jdbc.url")); // "jdbc:h2:~/MyH2DatabaseFile"

However, when switching implementation to EclipseLink (2.6.2 which is the currently latest version) with this provider instead specified in persistence.xml:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

Then it does not work, and I can also see that the properties object is empty with properties.size() == 0.

Considering that it works as I expected with Hibernate, then I suppose it is a bug in EclipseLink ? (though another possibility could be that I have misunderstood something about how to use "getProperties" but it just happens to works with Hibernate because of some unknown reason the pragmatic programmer coined as "Programming by Coincidence")

Maybe some EclipseLink developer is reading this question and can confirm that it is a known bug (which will be fixed soon) ?

Since "EntityManagerFactory.getProperties()" does not seem to work for all JPA implementations (EclipseLink), then is there any other standard method available (i.e. without using third part libraries which e.g. parses the xml code) which can be used for extracting property values which works for both EclipseLink and Hibernate ?

Pelle
  • 137
  • 2
  • 6

0 Answers0