Properties parameter does not override persistence.xml entries in createEntityManagerFactory as JPA 2.0 spec requires #121

Closed
machadolab opened this Issue Sep 26, 2012 · 3 comments

Projects

None yet

3 participants

@machadolab

In my application, we need to pass programmatically the host/port/keyspace/dialect information vs having it stored in the persistence.xml file. I have the following code:

                Map<String, Object> props = new HashMap<String, Object>();
        props.put("kundera.nodes","localhost");
        props.put("kundera.port","9160");
        props.put("kundera.keyspace","KunderaExamples");
        props.put("kundera.dialect","cassandra");

            EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu", props);
            EntityManager em = emf.createEntityManager();

and xml:

<persistence-unit name="cassandra_pu">
        <provider>com.impetus.kundera.KunderaPersistence</provider>     
        <properties>            
            <property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
            <property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
            <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/>
        </properties>       
    </persistence-unit>

Which according to the JPA 2.0 spec, should override those properties from the persistence.xml:

9.4.3 Persistence Unit Properties
Persistence unit properties and hints may be passed to persistence providers in the Map parameter of the
createEntityManagerFactory(String, Map) method. These properties correspond to the
elements in the persistence.xml file. When any of these properties are specified in the Map
parameter, their values override the values of the corresponding elements in the persistence.xml
file for the named persistence unit. They also override any defaults that the provider might have applied.

But instead I get a NPE because the port value is not defined when Pelops tries to load the port configuration item:

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at com.impetus.client.cassandra.pelops.PelopsClientFactory.createPoolOrConnection(PelopsClientFactory.java:71)
at com.impetus.kundera.loader.GenericClientFactory.load(GenericClientFactory.java:88)
at com.impetus.kundera.configure.ClientFactoryConfiguraton.configure(ClientFactoryConfiguraton.java:57)
at com.impetus.kundera.configure.Configurator.configure(Configurator.java:64)
at com.impetus.kundera.KunderaPersistence.initializeKundera(KunderaPersistence.java:103)
at com.impetus.kundera.KunderaPersistence.createEntityManagerFactory(KunderaPersistence.java:81)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at ......

So it seems kundera should take the supplied properties map and override/add any missing properties before trying to use them to make the connection.

Collaborator
mevivs commented Sep 26, 2012

Yes, i guess this is what it should be doing. valid issue.

@kkmishra kkmishra was assigned Sep 26, 2012
Collaborator
mevivs commented Dec 15, 2012

Fix is available in current trunk branch:
d12d288

Collaborator
mevivs commented Jan 23, 2013

Fix added and will be released with 2.3

-Vivek

@mevivs mevivs closed this Jan 23, 2013
@kkmishra kkmishra was unassigned by machadolab Feb 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment