Does JPA require the Datasources come from JNDI? - java

I'm using Spring in Tomcat for my webapp. My datasources are built in Spring then published to JNDI using org.springframework.jndi.JndiTemplate. This is clunky for a few reasons, but my main problem is that it's difficult to control the database being used (which I want to do for testing). Is it possible to use JPA without using JNDI as a lookup service? Ideally, I'd be able to provide the data sources directly to JPA, or through some other method that doesn't rely on a container for the implementation (I have investigated JNDI implementations that aren't provided by a container, but they're not right for my needs).

You can specify a datasource in your spring configuration file. Here is an excerpt from mine which uses a MySql Database. To view the full configuration file and project view the source on GitHub.
<!-- Database -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/to_thought_tutorial" />
<property name="username" value="tutorial" />
<property name="password" value="tutorial" />
</bean>
<!-- Entity Manager -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="persistenceUnitName" value="tothought-tutorial" />
</bean>
I would also encourage you to visit my blog which contains a video describing how to setup a datasource: http://tothought.cloudfoundry.com/post/4
The Spring documentation also includes examples of how to setup a datasource that does not rely upon JNDI: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/jdbc.html#jdbc-datasource

You can define a data source connection directly in your persistence.xml for any given persistence unit:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="BlahBlah" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/db_name"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="xxx"/>
</properties>
</persistence-unit>
</persistence>
If you're not using Hibernate, you can lookup your providers properties, or use the JPA2 standard properties javax.persistence.jdbc.user, javax.persistence.jdbc.password, javax.persistence.jdbc.url etc.

Related

JUnit and Spring: Cannot find persistence.xml - but it does exist

I'm trying to get JUnit to run, but it keeps throwing the following error:
class path resource [META-INF/persistence.xml] cannot be opened because it does not exist
The entire stacktrace can be found here: http://codepad.org/OhlyjQKn. It is a Spring project, with Hibernate (also using JPA). The project runs fine - it doesn't complain about any missing files when running it normally (not running the JUnit test)
I try to run the following test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/classes/applicationContext.xml",
"file:src/main/webapp/WEB-INF/config/spring-security.xml"})
public class KeyServiceTests {
#Test
public void testKeyCreation() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
KeyService keyService = (KeyService) context.getBean("keyService");
// Some testing goes on here
}
}
The entityManagerFactory in applicationContext.xml is defined as follows:
(entire file here: http://codepad.org/UrCtj0pW)
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="hsqldb-ds" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
</bean>
my classpath is src/main/webapp/WEB-INF/classes - which holds applicationContext.xml and also META-INF/persistence.xml
The persistence.xml is defined as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="hsqldb-ds" transaction-type="RESOURCE_LOCAL">
<description>HSQLDB Persistence Unit</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:demodb" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.transaction.flush_before_completion" value="true" />
</properties>
</persistence-unit>
So yea. It can't find persistence.xml, but it exists where it should exist (relative to the classpath), and the project only complains about the file when running JUnit. Does anyone know what's going on, or what I should be doing to get JUnit to run properly? I'm new to using JUnit with Spring/Hibernate, so I'm probably doing a few things wrong.
Kind regards
I needed to move the META-INF/persistence.xml and applicationContext.xml to src/main/resources to get JUnit to figure out where the necessary files where. The following line in the JUnit test case then changed to this:
#ContextConfiguration(locations = {"classpath:applicationContext.xml"})
(I also removed the reference to spring-security.xml because it was kind of unnecessary)
Big thanks to user Wintermute!

OpenJPA: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property

I am getting above exception when trying to do a simple example using openjpa. Here I am using a custom persistance provider class. Following is my persistence.xml file. I went through the previous similar threads but couldn't find a proper solution with my configurations.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="juddiDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.juddi.test.persistance.JUDDIPersistenceProviderImpl</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
<!-- entity classes -->
<class>org.apache.juddi.model.Address</class>
<class>org.apache.juddi.model.AddressLine</class>
<class>org.apache.juddi.model.AuthToken</class>
<class>org.apache.juddi.model.BindingCategoryBag</class>
<class>org.apache.juddi.model.BindingDescr</class>
<class>org.apache.juddi.model.BindingTemplate</class>
<class>org.apache.juddi.model.BusinessCategoryBag</class>
<class>org.apache.juddi.model.BusinessDescr</class>
<class>org.apache.juddi.model.BusinessEntity</class>
<class>org.apache.juddi.model.BusinessIdentifier</class>
<class>org.apache.juddi.model.BusinessName</class>
<class>org.apache.juddi.model.BusinessService</class>
<class>org.apache.juddi.model.CategoryBag</class>
<class>org.apache.juddi.model.Clerk</class>
<class>org.apache.juddi.model.ClientSubscriptionInfo</class>
<class>org.apache.juddi.model.Contact</class>
<class>org.apache.juddi.model.ContactDescr</class>
<class>org.apache.juddi.model.DiscoveryUrl</class>
<class>org.apache.juddi.model.Email</class>
<class>org.apache.juddi.model.InstanceDetailsDescr</class>
<class>org.apache.juddi.model.InstanceDetailsDocDescr</class>
<class>org.apache.juddi.model.KeyedReference</class>
<class>org.apache.juddi.model.KeyedReferenceGroup</class>
<class>org.apache.juddi.model.Node</class>
<class>org.apache.juddi.model.OverviewDoc</class>
<class>org.apache.juddi.model.OverviewDocDescr</class>
<class>org.apache.juddi.model.PersonName</class>
<class>org.apache.juddi.model.Phone</class>
<class>org.apache.juddi.model.Publisher</class>
<class>org.apache.juddi.model.PublisherAssertion</class>
<class>org.apache.juddi.model.PublisherAssertionId</class>
<class>org.apache.juddi.model.ServiceCategoryBag</class>
<class>org.apache.juddi.model.ServiceDescr</class>
<class>org.apache.juddi.model.ServiceName</class>
<class>org.apache.juddi.model.ServiceProjection</class>
<class>org.apache.juddi.model.ServiceProjectionId</class>
<class>org.apache.juddi.model.Subscription</class>
<class>org.apache.juddi.model.SubscriptionChunkToken</class>
<class>org.apache.juddi.model.SubscriptionMatch</class>
<class>org.apache.juddi.model.Tmodel</class>
<class>org.apache.juddi.model.TmodelCategoryBag</class>
<class>org.apache.juddi.model.TmodelDescr</class>
<class>org.apache.juddi.model.TmodelIdentifier</class>
<class>org.apache.juddi.model.TmodelInstanceInfo</class>
<class>org.apache.juddi.model.TmodelInstanceInfoDescr</class>
<class>org.apache.juddi.model.TransferToken</class>
<class>org.apache.juddi.model.TransferTokenKey</class>
<class>org.apache.juddi.model.UddiEntity</class>
<class>org.apache.juddi.model.UddiEntityPublisher</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.Log" value="DefaultLevel=ERROR, Tool=ERROR"/>
<property name="openjpa.jdbc.UpdateManager" value="operation-order"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="warn"/>
<!-- h2 connection properties -->
<property name="openjpa.ConnectionDriverName" value="org.h2.Driver"/>
<property name="openjpa.ConnectionURL" value="jdbc:h2:repository/database/juddiv3"/>
<property name="openjpa.ConnectionUserName" value="test"/>
<property name="openjpa.ConnectionPassword" value="test"/>
<!-- mysql connection properties
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/juddi"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName" value="juddiv3"/>
<property name="openjpa.ConnectionPassword" value=""/>
-->
<property name="openjpa.Compatibility" value="CheckDatabaseForCascadePersistToDetachedEntity=true"/>
</properties>
</persistence-unit>
</persistence>
And I am getting the following error
org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property. Available properties in configuration are "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl#442ce698".
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:844)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:602)
at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1510)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:518)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:443)
at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:104)
at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:968)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:959)
at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:638)
at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:203)
at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:154)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:60)
at org.apache.juddi.config.PersistenceManager.getEntityManager(PersistenceManager.java:45)
at org.apache.juddi.config.AppConfig.getPersistentConfiguration(AppConfig.java:109)
at org.apache.juddi.config.AppConfig.loadConfiguration(AppConfig.java:95)
at org.apache.juddi.config.AppConfig.<init>(AppConfig.java:62)
at org.apache.juddi.config.AppConfig.getInstance(AppConfig.java:183)
at org.apache.juddi.config.AppConfig.getConfiguration(AppConfig.java:205)
at org.apache.juddi.Registry.start(Registry.java:55)
in your persistence.xml config file you have several datasource descriptions (MySQL and H2). Delete one of them and try again.

Environment specific Persistence xml in java standalone application

I have a standalone java application, which uses JPA for its persistence.
Right now I have a persistence.xml in META-INF.My application is currently in development.
My question is that if I move from development to the next envirnoment, say QA. I have to modify the persistence.xml and rebuild the jar. Is this the right way to go about it ?
If not,if I move the connection properties to a different file, where should this file be placed?
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ClassA</class>
<class>ClassB</class>
<class>ClassC</class>
<class>ClassD</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url"
value="url" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit>
</persistence>
Thanks in advance !
That's a good question. Normally, you put all these environment settings in an external file, say application.properties, and pass the location to it to the JVM when you start your application (e.g. -Dconfig.location=/conf/)
Then you should find a way to get the externalized properties into your EntityManagerFactory. You can't do that in persistence.xml, you can only hard-code things there. But you can do it when creating the entity manager factory by passing vendor properties.
If using a framework like spring, for example, this is easier to do, as spring provides a factory bean for the entity manager. Otherwise you should handle it yourself. Here's the relevant bit from spring:
provider.createEntityManagerFactory(persistenceUnitInfo, getJpaPropertyMap())

EclipseLink into OSGI Bundle persistence.xml not found

I'm having some problem with EclipseLink. I'm using GlassFish v3.1 and I'm trying to use EclipseLink for my persistence layer. I followed all tutorials available on the Eclipse wiki without luck. My persistence.xml file cannot be parsed and I receive this error while trying to create the EntityManagerFactory:
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: bundle://307.1:1/
Here is my persistence.xml located in /WEB-INF/classes/META-INF/:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="generic">
<class>com.generic.domain.Service</class>
<properties>
<!-- Embedded MySQL Login -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306"/>
<!-- TODO: replace with connection pool -->
<property name="javax.persistence.jdbc.userid" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.jdbc.read-connections.min" value="1"/>
<property name="eclipselink.jdbc.write-connections.min" value="1"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<!-- Logging Settings -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.exceptions" value="true" />
<property name="eclipselink.logging.timestamp" value="false"/>
</properties>
</persistence-unit>
</persistence>
I added this line to my MANIFEST.MF:
JPA-PersistenceUnits: generic
I can now confirm that it's a bug in EclipseLink. The work around to your problem is to either get hold of EntityManagerFactory using JNDI lookup or #PersistenceUnit instead of doing Persistence.createEntityManagerFactory().
1) It looks like you are trying to use OSGi/JPA from your WAB. Correct?
2) Can you tell if you have installed any new eclipselink bundles in your system? If so, what are they?
3) Can you provide stack trace?

Persistence deployment issue

I have a hibernate project, which uses JPA.
my persistence.xml contents is as follows:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Demo-PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/DemoDS</non-jta-data-source>
<class>com.demo.framework.entity.ReportDefinitionEntity</class>
<properties>
<!-- Database connection -->
<property name="hibernate.connection.url" value="jdbc:mysql://192.168.9.110:3306/demoDB" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<!-- Hibernate dialect -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Output goodies
-->
<property name="hibernate.query.jpaql_strict_compliance" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="false" />
<!-- Cache
-->
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
</persistence>
Now when I run it using eclipse I don't have a problem, but when I deploy it in Jboss, I get the below error:
ERROR [AbstractKernelController] Error
installing to Start:
name=persistence.unit:unitName=#Demo-PU
state=Create
java.lang.ClassCastException:
org.hibernate.ejb.HibernatePersistence
cannot be cast to
javax.persistence.spi.PersistenceProvider
And here is the list of Jar that I have
activation.jar
antlr-2.7.6.jar
asm-attrs.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-tools.jar
hibernate3.jar
javassist.jar
javax.persistence.jar
jdbc2_0-stdext.jar
jta.jar
mysql-connector-java-5.0.5-bin.jar
xml-writer.jar
How can I resolve this issue?
The ClassCastException is caused by having two copies of the javax.persistence APIs in your system (one in the common classloader provided by JBoss and the one in your app). When running on JBoss, you are just not supposed to provide this API in your application, don't package it.
By the way, it seems you're using a JPA 2.0 persistence.xml but I'm not convinced you're using the JPA 2.0 implemenation of Hibernate (actually, you seem to be using a pretty old version since I can see commons-logging.jar). You should probably fix that i.e. use the 1.0 version of persistence.xml.
Actually, you should very likely use a different persistence.xml when running on JBoss (using a JTA entity manager and a jta-data-source). And it seems weird to mix data source usage and Hibernate built-in connection pool.

Categories

Resources