I am trying to deploy an application that connects to Oracle Database and MySQL. I am using JPA 2.1, Hibernate 4.3.7, Spring, Spring Data and WildFly 8.2 but I am getting some errors when deploying the application. Eclipse shows error. He says that I cannot use more than one persistence unit. I was confused because I found some articles on the web showing files with more than one persistence unit. Like this one.
First of all my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="authwsPU" transaction-type="JTA">
<jta-data-source>java:/datasource/authwsds</jta-data-source>
..My MySQL mapping classes...
<properties>
<!-- MySQL -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/authwsEMF" />
</properties>
</persistence-unit>
<persistence-unit name="antaresPU" transaction-type="JTA">
<jta-data-source>java:/datasource/antaresds</jta-data-source>
...My Oracle mapping classes...
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/antaresEMF" />
</properties>
</persistence-unit>
</persistence>
I made change on jboss-web.xml too:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<context-root>/authws</context-root>
<persistence-context-ref id="autenticacaoCtx">
<persistence-context-ref-name>auth_ws/authEMF</persistence-context-ref-name>
<persistence-unit-name>authwsPU</persistence-unit-name>
</persistence-context-ref>
<persistence-context-ref id="antaresCtx">
<persistence-context-ref-name>auth_ws/antaresEMF</persistence-context-ref-name>
<persistence-unit-name>antaresPU</persistence-unit-name>
</persistence-context-ref>
</jboss-web>
And the errors. Eclipse, warned me, but I ignored.:
Multiple persistence units defined - only the first persistence unit will be recognized
Then I deployed and WildFly shows this error:
Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "authws.war". Either change the application deployment to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
at org.jboss.as.jpa.container.PersistenceUnitSearch.ambiguousPUError(PersistenceUnitSearch.java:187)
at org.jboss.as.jpa.container.PersistenceUnitSearch.findWithinDeployment(PersistenceUnitSearch.java:153)
at org.jboss.as.jpa.container.PersistenceUnitSearch.findPersistenceUnitSupplier(PersistenceUnitSearch.java:75)
at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:64)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.getPersistenceUnit(JPAAnnotationProcessor.java:372)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.getBindingSource(JPAAnnotationProcessor.java:296)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.processMethod(JPAAnnotationProcessor.java:206)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.processPersistenceAnnotations(JPAAnnotationProcessor.java:143)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.deploy(JPAAnnotationProcessor.java:100)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:159) [wildfly-server-8.2.0.Final.jar:8.2.0.Final]
... 5 more
UPDATE
I am updating the question with more details. First, the file where I create the EntityManagersFactory for Spring Data:
<jee:jndi-lookup jndi-name="java:jboss/poEMF" id="poEntityManagerFactory" expected-type="javax.persistence.EntityManagerFactory" />
<jpa:repositories base-package="br.com.po.dao" entity-manager-factory-ref="poEntityManagerFactory" />
<jee:jndi-lookup jndi-name="java:jboss/antaresEMF" id="antaresEntityManagerFactory" expected-type="javax.persistence.EntityManagerFactory" />
<jpa:repositories base-package="br.com.antares.dao" entity-manager-factory-ref="antaresEntityManagerFactory" />
Reading more about this online, I saw that the problem can be specification of the annotation #PersistenceUnit where I have to specify the unitName like #PersistenceUnit(unitName="defaultPersistenceUnit"). But, how I do this if I am using Spring Data?
For example you can use #EnableJpaRepositories in your #Configuration files (if you are using Bean Configuration) like this:
One configuration file with:
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(
basePackages = {"com.yourpackage.xxxx"},
entityManagerFactoryRef = "entityManagerFactory1",
transactionManagerRef = "transactionManager")
public class FirstPersistenceContextConfig {}
....
And a second configuration file:
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(
basePackages = {"com.yourpackage.yyyy"},
entityManagerFactoryRef = "entityManagerFactory2",
transactionManagerRef = "transactionManager")
public class SecondPersistenceContextConfig {}
...
I don't know if you've solved your problem.
If you're using a XML configuration, try this:
<jpa:repositories base-package="[repository]"
entity-manager-factory-ref="mysqlManager" transaction-manager-ref="mysqlTransaction"/>
<jpa:repositories base-package="[repository]"
entity-manager-factory-ref="oracleManager" transaction-manager-ref="oracleTransaction"/>
<bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- MySQL Configuration Connection -->
</bean>
<bean id="mysqlTransaction" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="mysqlManager" />
</bean>
<bean id="mysqlManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mysqlDataSource" />
<property name="persistenceUnitName" value="mysqlPU" />
<property name="packagesToScan" value="lh.ast.model" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- Oracle Configuration Connection -->
</bean>
<bean id="oracleTransaction" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="oracleManager" />
</bean>
<bean id="oracleManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="oracleDataSource" />
<property name="persistenceUnitName" value="oraclePU" />
<property name="packagesToScan" value="[your entity package]" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Related
There have been a alot of threads for this issue but I there was nothing that helped me. I tried to deploy this in Tomcat 7 and it worked perfectly. Then I tried to deploy this in JBOSS 6.4 and got this error below.
ERROR
"failure-description" => {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"TaskManagementSystem.war#persistenceUnit\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"TaskManagementSystem.war#persistenceUnit\": javax.persistence.PersistenceException: [PersistenceUnit: persistenceUnit] Unable to build EntityManagerFactory
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: persistenceUnit] Unable to build EntityManagerFactory
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set"}},
"rolled-back" => true
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.project.taskmanagement.model.User</class>
<class>com.project.taskmanagement.model.UserProfile</class>
<class>com.project.taskmanagement.model.Role</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
applicationContext.xml
<!-- DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>
</bean>
<!-- EntityManager -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${hibernate.show_sql}" />
<property name="generateDdl" value="${hibernate.generate_ddl}" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- <prop key="hibernate.jdbc.batch_size">0</prop> -->
</props>
</property>
</bean>
<!-- TransactionManager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
Versions I used in pom.xml
<spring.version>4.3.9.RELEASE</spring.version>
<spring.security.version>4.2.3.RELEASE</spring.security.version>
<hibernate.version>4.3.11.Final</hibernate.version>
<mysql.version>5.1.38</mysql.version>
<junit.version>4.11</junit.version>
<servlet-api.version>3.1.0</servlet-api.version>
<persistence-api.version>1.0.2</persistence-api.version>
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<java.version>1.8</java.version>
<tiles.version>3.0.7</tiles.version>
UPDATE:
Tried this in JBOSS 7.0 and it's working fine. But the requirement is to use JBOSS 6.4 so I still need to work on this one. Could someone please explain why am I getting this error? Thank you!
Our application currently uses a datasource which is defined in the JBoss standalone.xml, and basically we need to have this be defined within the app rather than in the container for a while. Our current setup is;
application-context.xml;
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="rtsPersistenceUnit" />
<property name="packagesToScan">
...
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
</bean>
</property>
</bean>
persistance.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="rtsPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/RTSdb</jta-data-source>
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.transaction.flush_before_completion" value="true" />
</properties>
</persistence-unit>
</persistence>
datasource.xml:
<bean id="rtsDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="..."/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>
Basically all I want is for the line
<jta-data-source>java:/jdbc/RTSdb</jta-data-source>
to read from datasource.xml rather than go the container (JBoss).
It seems like it would be simple but after reading some Spring and Oracle docs I couldn't find an answer.
Yes, you could use a JTA compliant transaction manager like Atomikos or Bitronix. Their respective sites have documentation on how to configure them with Spring. In general, you will have to follow the steps given below (if using Atomikos):
Retain your existing XA data source (rtsDatasource in your case) or create one if not already using (for example, if someone has a non-XA data source, that data source must be converted to an XA data source first).
Wrap the XA data source in an AtomikosDataSourceBean.
Point your EntityManagerFactory at the new AtomikosDataSourceBean instance.
Declare an XA transaction manager and an XA user transaction.
Wrap the XA transaction manager in a Spring JtaTransactionManager.
Use the Spring JtaTransactionManager.
A short configuration snippet using H2 database, Hibernate 4, Spring 4 and Atomikos 4 is shown below.
<bean class="org.h2.jdbcx.JdbcDataSource" id="originalDataStore" lazy-init="true">...</bean>
<bean class="com.atomikos.jdbc.AtomikosDataSourceBean" id="dataSource" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="xaDS"/>
<property name="xaDataSource" ref="originalDataStore"/>
<property name="poolSize" value="3"/>
</bean>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop>
...
</props>
</property>
</bean>
<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
<property name="transactionManager">
<bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="false"/>
</bean>
</property>
<property name="userTransaction">
<bean class="com.atomikos.icatch.jta.J2eeUserTransaction">
<property name="transactionTimeout" value="300"/>
</bean>
</property>
<property name="allowCustomIsolationLevels" value="true"/>
</bean>
<transaction:annotation-driven transaction-manager="transactionManager"/>
For details, you can see this app.
I previously had this config for Hibernate using RESOURCE-LOCAL transaction type:
persistence.xml:
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
</persistence-unit>
applicationContext (dataaccess bit):
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"></bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="persistenceUnitName" value="myPU"/>
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<!-- Are there any other properties required? -->
</bean>
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/CNTXESDB" />
<property name="lookupOnStartup" value="true" />
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
But this kind of transaction seems not to work with Glassfish, so I had to switch to JTA transactions.
The problem is -- to get Spring to manage transaction creation (through #Transactional) I need to define a TransactionManager bean but JtaTransactionManager included in spring-tx does not accept an entityManagerFactory bean, so it does not know where the entityManager is in order to open/close/flush Hibernate session.
So how can I configure Spring with Hibernate to use JTA transactions?
EDIT:
turns out you can use RESOURCE_LOCAL transactions with Glassfish, but somehow you cannot have a persistence.xml file. I renamed this file to my_persistence.xml and configured LocalContainerEntityManagerFactoryBean like this:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="persistenceUnitName" value="myPU"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/my_persistence.xml" />
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
I had a similar problem and finally I solved as you can see in this little demo:
https://github.com/miguelangelprogramacion/spring4_jpa_hibernate
With [1] as a reference, I prefer to use Spring's Transaction Support before JTA.
Also, I've used an annotation based approach.
[1] http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/
persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<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="xyz" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com......</class>
</persistence-unit>
</persistence>
ApplicationContext.xml
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:XE" />
<property name="username" value="yyy" />
<property name="password" value="yyy" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="xyz" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="showSql" value="true" />
<!-- <property name="generateDdl" value="true" /> -->
</bean>
</property>
</bean>
<bean id="theDao" class="com.cin.the.dataaccess.dao.the.TheJPA">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
the error i get is
[PersistenceUnit: xyz] Unable to build EntityManagerFactory
can any one tell me the mistake
The main problem was that the entites was not generated properly. so at the end of the stacktrace it was giving this error
Caused by: org.hibernate.MappingException: property mapping has wrong number of columns: com......date type: object
once the entity was generated correctly the problem was solved
Someone answered here:
If you define your persistence unit with the JTA transaction type, you
need also to define your datasource inside the jta-data-source
attribute.
Try adding this to your ApplicationContext.xml
<jee:jndi-lookup id="dataSource" jndi-name="your-jndi-name" />
and the following to <persistence-unit> element in persistence.xml:
<jta-data-source>your-jndi-name</jta-data-source>
You are getting this error because you have mapped your domain object datatype to be java.lang.Object. map it to more specific like a String, int, long...
I can't get automatic table creation working in spring when using hibernate / jpa.
Here are my config files:
<?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="naveroTest">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>xxx</class>
...
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:file:/tmp/naveroTestDB"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
<!-- For auto creation of tables -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:/tmp/naveroTestDB" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaVendorAdapter">
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="PictureBean" class="de.navero.server.bl.PictureBean">
<property name="entityManagerFactory"><ref local="entityManagerFactory" /></property>
</bean>
</beans>
Any ideas what may be wrong?
Thanks for any help :).
try the following:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="naveroTest" transaction-type="RESOURCE_LOCAL" />
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
...
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
This works for me.
I had exactly the same issue...
Commenting out property
<!--property name="generateDdl" value="true"--> in the JpaAdapter,
but setting
<property name="hibernate.hbm2ddl.auto" value="create"/> in persistence.xml
worked for me. I am using hsqldb.
I noticed in the logs that the mode was beign set to update no matter what I set in
persistence.xml.
The value in persistence.xml was actually picked up but never applied.
I am using Spring 3.0.6 and Hibernate 4
Can you try changing the generateDdl property to false on HibernateJpaVendorAdapter in your spring config file.
Seems to conflict with the hibernate hibernate.hbm2ddl.auto property
See https://jira.springframework.org/browse/SPR-6836 for more info.
In My hibernate-default.cfg.xml i used
<property name="hibernate.hbm2ddl.auto">update</property>
and it worked perfectly, so the config file was as follows
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="current_session_context_class">thread</property>
<!-- When an HQL statement declares a true/false, replace with the standard Y/N -->
<property name="hibernate.query.substitutions">true 'Y', false 'N'</property>
<!-- A useful property do debugging queries. Besure sure it is false or commented out when going PROD -->
<!-- <property name="hibernate.show_sql">true</property> -->
<!-- Format the printed out SQL generated by Hibernate -->
<property name="hibernate.format_sql">false</property>
<!-- If enabled, Hibernate will collect statistics useful for performance tuning - JMX -->
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class=....
sadly both solutions didn't work for me :(. "hibernate.hbm2ddl.auto=update" would be also ok, as it should create the tables if they're not present.
It seems that all property settings from the persistence.xml are recognized as username and database provider are set correctly. Sadly the tables aren't created at startup which causes my testcase to fail as the SELECT statement throws an error ...
As you can see, I've set the connection url to use a local file database, which allows me to review the database log after the test run. But the log file is still empty after the test run, even no error is written to it :(.
This worked for me
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem://productDb" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
</bean>
</property>
If I change value to false like this<property name="generateDdl" value="false" /> then I get SqlExceptionHelper:144 - Table not found in statement
Maybe to late, but today I had the same problem when I was writing some tests for a legacy app.
I was using spring 2.5, hibernate3, and HSQL database.
To solve it I changed the database from HSQL to H2 and the datasource from org.springframework.jdbc.datasource.DriverManagerDataSource to org.apache.commons.dbcp.BasicDataSource.
The spring-context.xml looks like:
<?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: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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven/>
...
</beans>
The persistence.xml looks like:
<?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="TestPU" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
</properties>
</persistence-unit>
</persistence>
I hope it helps.
In my case, the tables were not been created because the objects annotated with #Table where not annotated as #Entity