hibernate.hbm2ddl.import_files in persistence.xml is not working - spring

I need that hibernate read a sql file before start junit tests, so I did the following configuration in persistence.xml:
<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="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost:9001/test" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.hbm2ddl.import_files" value="/META-INF/load.sql" />
</properties>
</persistence-unit>
</persistence>
The applicationContext.xml to load spring context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.test" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="test" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean>
</beans>
Junit tests extends the class:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
#TestExecutionListeners(inheritListeners = false, listeners = {
TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class })
#TransactionConfiguration(defaultRollback = true)
#Transactional
public abstract class UnitTestConfiguration {}
When I run the junit tests, the file load.sql is not been imported and no errors are shown.
I'm using Hibernate 4 and Spring 3.0.5.

I solved my problem by putting the load.sql in src/test/resource/ folder.
This way isn't necessary to specify in persistence.xml where the sql file is.

I think(I usually use it) you should include it in your applicationContext.xml:
<import resource="classpath:/path_to_persistence/persistence.xml" />
Also you can specify several configuration files in test #ContextConfiguration:
#ContextConfiguration(locations = { "classpath:/applicationContext1.xml", ... ,"classpath:/applicationContextn.xml" })

Just put a file with name import.sql on src/test/resources directory.
The default value of property javax.persistence.hibernate.hbm2ddl.import_files is
import.sql.
Check: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html

Related

How can I connect to two different databases, one for reading and one for writing in the same application using Spring?

Use Case: I am running a spring application which has orders. I can create plans for these orders. My dev environment/db does not have any orders and they come from a different application (we share the same db). So, Is there a way I can read orders from production database but when I create a plan, it gets saved to dev database?
My persistence.xml is as below
<?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="Demo_PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.entity.ErrorMessage</class>
<class>com.entity.ErrorMessageTxt</class>
<class>com.entity.ErrorMessageTxtId</class>
<class>com.entity.Language</class>
<class>com.entity.TextEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.InformixDialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.cache.region.factory_class"
value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding"
value="UTF-8" />
<!-- Adding timeouts for Lock Mode and Query -->
<property name="javax.persistence.query.timeout" value="60000" />
<property name="javax.persistence.lock.timeout" value="60000" />
</properties>
</persistence-unit>
</persistence>
My spring-config.xml is as below
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com.project"/>
<context:component-scan base-package="com.project2"/>
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor"/>
<task:scheduler id="myScheduler"/>
<bean id="qa-informix" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/dcSysCommon"/>
</bean>
<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="localDataSource" value-ref="qa-informix" />
</map>
</property>
<property name="defaultDataSource" ref="qa-informix"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="pum" />
<property name="jpaDialect">
<bean class="com.project.utils.IsolationSupportHibernateJpaDialect"/>
</property>
<property name="persistenceUnitName" value="PYD_PU" />
</bean>
<!-- bean post-processor for JPA annotations -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- this enables the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="interpolator" class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="messageInterpolator" ref="interpolator" />
</bean>
<bean class="com.project3.common.core.bo.LanguageBusinessObject" primary="true" />
<bean name="restUtils" class="com.project1.utils.RestUtils" />
<bean id="srvPropertyFile" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="srvPropertyFile" />
</bean>
<bean id="beanConfig" class="io.swagger.jaxrs.config.BeanConfig">
<property name="title" value="Swagger UI for QAS"/>
<property name="version" value="1.0.0" />
<property name="schemes" value="http" />
<property name="host" value="#{srvPropertyFile.hostName}:#{srvPropertyFile.pydPort}" />
<property name="basePath" value="planyourday/#{srvPropertyFile.countryCode}/#{srvPropertyFile.dcNumber}"/>
<property name="resourcePackage" value="com.project1.resources"/>
<property name="license" value="Apache 2.0 License"/>
<property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/>
<property name="scan" value="true"/>
</bean>
<bean id="apiListingResource" class="com.project1.utils.SwaggerApiListingResource"/>
<bean id="swaggerSerializers" class="io.swagger.jaxrs.listing.SwaggerSerializers" scope="singleton"/>
</beans>
In my Jetty server config, I am defining the srvPropertyFile which references to the environment.properties file which has the database connection to dev db.
How should I manage my persistence context so that the entity manager used while reading orders and the entity manager used while creating plans connects to different databases?
you have to configure two persistence unit, data source and transaction manager(one for read and other for write) each.
While using entity manager specify the persistence unit like below.
PersistenceUnit(unitName = "x")
EntityManagerFactory entityManagerFactory;

Not able to read orm.xml in multiple module project in jpa

I have configured multi module project using maven ,so I am building the common module which is going to use across the project.
Now the problem is I have my Category.orm.xml file inside META-INF/domain/orm directory when i tried to read the named query BC_READ_ALL_CATEGORIES from this its throwing following error.
While if I tried to read the same named query from annotation then there is no problem occurred.
Please see where i am mistaking THANKS...
Exception in thread "main" java.lang.IllegalArgumentException: No query defined for that name [BC_READ_ALL_CATEGORIES]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.buildQueryFromName(AbstractEntityManagerImpl.java:788)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:925)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
at com.sun.proxy.$Proxy36.createNamedQuery(Unknown Source)
at com.ornamentbazzar.common.catalog.dao.CategoryDaoImpl.readAllCategories(CategoryDaoImpl.java:53)
Directory Structure
This is the applicationContext-persistent.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="com.ornamentbazzar.*" />
<!-- this is also used we can used this also -->
<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/retailer" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:* META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="ornament" />
<property name="packagesToScan" value="com.ornamentbazzar.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
<bean id="category" class="com.ornamentbazzar.common.catalog.dao.CategoryDaoImpl"></bean>
</beans>
persistent.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_2_0.xsd"
version="2.0">
<persistence-unit name="ornament" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/domain/orm/Category.orm.xml</mapping-file>
<!-- <mapping-file>META-INF/product.orm.xml</mapping-file> -->
<class>com.ornamentbazzar.common.catalog.entity.Category</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryMapper</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryMapperPK</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryAttribute</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryMapper</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.transaction.flush_before_completion"
value="false" />
<property name="hibernate.connection.autocommit" value="true" />
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
category.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<named-query name="BC_READ_ALL_CATEGORIES">
<query>SELECT category FROM com.ornamentbazzar.common.catalog.entity.Category category ORDER BY category.id</query>
</named-query>
</entity-mappings>
TestApplication
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext-persistence.xml");
CategoryDao categoryDao = (CategoryDao) applicationContext.getBean("category");
System.out.println(categoryDao.readAllCategories().size());
}
}
Your persistence.xml only refers to a single *.orm.xml file. To read multiple, you need to list multiple.
Your persistence.xml is only referring to one. Do you have multiple somewhere?
<mapping-file>META-INF/domain/orm/Category.orm.xml</mapping-file>
<!-- <mapping-file>META-INF/product.orm.xml</mapping-file> -->

Conflicting persistence unit definitions exception

I am getting the following exception on server startup. I have just one persistence.xml in my project. Any ideas?
weblogic.application.ModuleException:
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1520)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:484)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
Truncated. see log file for complete stacktrace
Caused By: java.lang.IllegalStateException: Conflicting persistence unit definitions for name 'TRAVEL_SYSTEM_UNIT': file:/D:/TravelSystem/workspace-sts/Travel_System-EAR/EarContent/APP-INF/classes/, file:/D:/TravelSystem/workspace-sts/Travel_System-dataaccess/target/classes/
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:362)
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:326)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:235)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
Truncated. see log file for complete stacktrace
My context file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation=" http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.dataaccess" />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/mysql" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="persistenceUnitName" value="SYSTEM_UNIT"></property>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="packagesToScan" value="com.dataaccess.model"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<!-- <property name="jpaVendorAdapter">
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"></property>
<property name="showSql" value="true" />
</bean>
</property> -->
</bean>
<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
</beans>
My persistence.xml file is in META-INF where it should be:
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="TRAVEL_SYSTEM_UNIT" transaction-type="JTA">
<jta-data-source>jdbc/mysql</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="default_batch_fetch_size" value="25" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="true" />
<!-- <property name="hibernate.hbm2ddl.auto" value="update"/> -->
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup" />
</properties>
</persistence-unit>
</persistence>
using 3.1.2.RELEASE Maven 3.0.4
I just add below configuration to My context file:
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath:META-INF/persistence-test.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"></property>
</bean>
On window I put META-INF/persistence.xml under src/java/resources.
Then I import same project to Ubuntu, this problem appeared. And it is fixed after I moved META-INF/persistence.xml to src/main/webapp.

Spring + JPA in standalone application doesn't save data in database

I am using
Spring 3.1.1
JPA 2
H2
Hibernate
in standalone Java application. When I run the application, the transaction is executed and when I query the data, the data is retrieved. But the data is not saved when the application is closed.
Please help me.
Here is spring configuration,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
</bean>
<bean id="sharedEntityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven />
</beans>
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="sling">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.sling.data.Trend</class>
<class>com.sling.data.Gc</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:sling;DB_CLOSE_DELAY=-1" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
</properties>
</persistence-unit>
</persistence>
DAO class,
#Repository
public class GcDao {
#PersistenceContext
private EntityManager em;
public GcDao() {
}
#Transactional
public void add(Gc gc){
em.persist(gc);
}
#SuppressWarnings("unchecked")
#Transactional(readOnly = true)
public List<Gc> getGc(){
String queryText = " from Gc";
Query query = em.createQuery(queryText);
return query.getResultList();
}
}
I believe you still need to add context:component-scan on your dao package before your tx:anotation-driven element.
<context:component-scan base-package="com.sling.dao" />
<tx:annotation-driven />

unit testing spring/jpa/hibernate and google-appengine with cloud SQL : API package rdbms missing

I try to make a simple test with spring/jpa/hibernate and GAE/Google Cloud SQL.
But I can't find the right configuration and I always keep getting :
Caused by: com.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'rdbms' or call 'OpenConnection()' was not found.
at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:98) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
at com.google.appengine.api.rdbms.RdbmsApiProxyClient$ApiProxyBlockingInterface.makeSyncCall(RdbmsApiProxyClient.java:95) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
at com.google.appengine.api.rdbms.RdbmsApiProxyClient$ApiProxyBlockingInterface.openConnection(RdbmsApiProxyClient.java:73) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
at com.google.cloud.sql.jdbc.internal.SqlProtoClient.openConnection(SqlProtoClient.java:58) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
at com.google.cloud.sql.jdbc.Driver.connect(Driver.java:66) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
at com.google.cloud.sql.jdbc.Driver.connect(Driver.java:26) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
at java.sql.DriverManager.getConnection(DriverManager.java:579) ~[na:1.7.0]
at java.sql.DriverManager.getConnection(DriverManager.java:190) ~[na:1.7.0]
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
versions :
Spring framework : 3.1.1
Hibernate : 4.1.2
Google App Engine : 1.6.4.1
persistence.xml :
<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="gae-test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.gro.gae.domainmodel.user.UserEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.hbm2ddl.auto" value="update"></property>
<property name="hibernate.connection.url" value="jdbc:google:rdbms://sqlpetstocks:petstocks/dngdatabase"></property>
<property name="hibernate.connection.driver_class" value="com.google.appengine.api.rdbms.AppEngineDriver"></property>
<property name="hibernate.connection.driver_class" value="com.google.cloud.sql.Driver"></property>
</properties>
</persistence-unit>
</persistence>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<jpa:repositories base-package="com.gro.gae.persistence"
factory-class="com.gro.gae.persistence.CustomJpaRepositoryFactoryBean" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="gae-test" />
</bean>
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<context:annotation-config />
<tx:annotation-driven />
</beans>
unit test :
#RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:applicationContext-persistence.xml" })
#TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
#TransactionConfiguration(defaultRollback = true)
#Transactional
public class ConfigurationTest {
#Test
public void testConfig(){
}
}
Any ideas ?
Thanks
Guillaume
Too late but maybe it'll help someone.
I found the same problem and the VM args didn't work under JUnit execution.
The solution was for me to define another entityManager/persistenceUnit for tests.
The key was to change in the new testPersistence.xml the properties:
<property name="javax.persistence.jdbc.driver" value="com.google.appengine.api.rdbms.AppEngineDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:google:rdbms:localhost:3306/DB_NAME" />
with
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/DB_NAME" />
Hope this helps.

Resources