autocreate schema with jdo, spring and H2 with datanucleus - spring

I Recently migrated to using the spring framework for DI - working fine. I'm injecting a persistence manager which also works fine. On a new install, I get:
SEVERE: Required table missing .... Either your MetaData is incorrect, or you need to enable "datanucleus.autoCreateTables"
Fair enough, I'm not enabling autocreate tables.
I create my persistence manager like this in the spring context.xml per the doc:
<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:./thedbpath.db;MV_STORE=FALSE;MVCC=FALSE;FILE_LOCK=NO"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<bean id="pmf" class="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" destroy-method="close">
<property name="connectionFactory" ref="dataSource"/>
<property name="nontransactionalRead" value="true"/>
</bean>
everything works - but i can't figure out where to set the datanucleus.autoCreateTables
This normally would be set in the persistence.xml - I don't see where to put datanucleus properties in the spring context.xml. Thanks in advance
edit: thanks to the answer below, this was the correct config:
<bean id="pmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
<property name="jdoProperties">
<props>
<prop key="javax.jdo.PersistenceManagerFactoryClass">
org.datanucleus.api.jdo.JDOPersistenceManagerFactory
</prop>
<prop key="javax.jdo.option.ConnectionURL">jdbc:h2:./database/db;MV_STORE=FALSE;MVCC=FALSE;;FILE_LOCK=NO</prop>
<prop key="javax.jdo.option.ConnectionUserName">sa</prop>
<prop key="javax.jdo.option.ConnectionPassword"></prop>
<prop key="javax.jdo.option.ConnectionDriverName">org.h2.Driver</prop>
<prop key="org.jpox.autoCreateSchema">true</prop>
<prop key="org.jpox.identifier.case">PreserveCase</prop>
<prop key="datanucleus.autoCreateTables">true</prop>
</props>
</property>
</bean>

This page
http://www.datanucleus.org/products/accessplatform_3_0/guides/jdo/springframework/index.html
has a "jdoProperties" property that can be used to specify JDO implementation-specific properties. Maybe try that?

Related

Configuring auto_escape in freemarker spring-mvc Application

I am using spring-mvc with freemarker-2.3.27-incubating.jar.
And bean configuration for view-resolver like below...
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/"/>
<property name="freemarkerSettings">
<props>
<prop key="template_exception_handler">rethrow</prop>
<prop key="number_format">0.########</prop>
<prop key="date_format">dd/MM/yyyy</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="exposeSessionAttributes" value="true"/>
<property name="requestContextAttribute" value="rc"/>
</bean>
Where and how can I use <#ftl output_format="HTML"> or <#ftl output_format="HTML" auto_esc=true> to enable HTML auto-scape ?
The #ftl tags you just add at the beginning of the template files themselves. But I would recommend setting recognize_standard_file_extensions to true in the freemarkerSettings properties, and then use .ftlh file extension instead of .ftl. Or, even better, you set incompatible_improvements to 2.3.27 there instead, which enables recognize_standard_file_extensions and some fixes. (Last not least, in case you want this for .ftl for some reason, you can set output_format to HTMLOutputFormat in freemarkerSettings properties too.)

Spring JPA with Hibernate Persistence provider and setting javax.persistence.schema-generation properties

I am using Spring JPA with Hibernate as the persistence provider and Derby as the database. (Specifically, the version of jars are Spring version 4.3.4, Hibernate Entity Manager version 5.2.10 and Derby 10.13.1.1).
I want to check the correct approach for
The schema generation in the JPA properties.
How to use the schema generated from step 1 to create the database schema.
(Note this is not a Spring Boot application.)
My application config file is this:
<context:annotation-config />
<context:component-scan base-package="org.service"/>
<context:component-scan base-package="org.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan">
<array>
<value>org.model</value>
<value>org.repository</value>
<value>org.service</value>
</array>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:C:\\tmp\\testdb"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<jpa:repositories base-package="org.repository"></jpa:repositories>
For step (1), my investigation and testing suggests that it's possible to set the persistence schema generation inside the jpaProperties property as follows:
<prop key="javax.persistence.schema-generation.scripts.action">create</prop>
<prop key="javax.persistence.schema-generation.scripts.create-target">create.sql</prop>
This will generate a create.sql file. For step (2), I used
<prop key="javax.persistence.schema-generation.create-source">script</prop>
<prop key="javax.persistence.schema-generation.create-script-source">create.sql</prop>
<prop key="javax.persistence.schema-generation.database.action">create</prop>
However do I need to still set create=true in the URL jdbc:derby:C:\tmp\testdb? Do the above lines for javax.persistence.schema-generation assume the database has been created?

Spring and H2 or Derby - Multiple Transactions

I have a project that uses Spring. In local environment I use Derby (with Jetty) that gives me the perfomance that I need in this environemnt (in production is used Oracle and Weblogic).
The problem is that I need new transactions in some specific operations. This specific operations use the annotation #Transactional(propagation=REQUIRES_NEW).
The problem is that with Derby when comes the time that a new transaction was supposed to be createad, the operation hangs and due time out. I tried to use H2 but in the same moment of creation of the new transaction, the database is restarted, meaning, the tables are dropped and are created again.
Derby and H2 don't support multiple transactions? I can't find anything that tells me the opposite.
My database configuration is:
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
<property name="persistenceUnitName" value="ORACLE_PU"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.DerbyPlatform"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="shared-cache-mode">NONE</prop>
<prop key="eclipselink.cache.shared.default">false</prop>
<prop key="eclipselink.query-results-cache">false</prop>
<prop key="eclipselink.weaving">false</prop>
<prop key="eclipselink.ddl-generation">${oracle.eclipselink.ddl.generation}</prop>
<prop key="eclipselink.ddl-generation.output-mode">database</prop>
<prop key="eclipselink.create-ddl-jdbc-file-name">create-tables.sql</prop>
<prop key="eclipselink.drop-ddl-jdbc-file-name">drop-tables.sql</prop>
<prop key="eclipselink.application-location">target/generated-sources/database</prop>
<prop key="eclipselink.jdbc.cache-statements">true</prop>
<prop key="eclipselink.custom.sql.import.file">${oracle.eclipselink.custom.sql.import.file}</prop>
...
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:target/memory;create=true"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
My attempt with H2 is similar but with the proper configurations. In weblogic environemnt everything works as expected.
What I'm missing?
It seems you are using derby in embedded in-memory mode. AFAIK, both derby and H2 support concurrent transactions only in standalone mode, when you connect through a TCP connection, but not in embedded mode. Try running derby as a seperate procese and change your database to connect to it.

Intellij JPA Console with persistence.xml

I'm setting up a xml-free persistence JPA/Hibernate 4+/Spring 3+ using Intellij 13+. When I try to execute a query in the jpa console, I get the following error:
javax.persistence.PersistenceException: Unable to build entity manager factory
java.lang.RuntimeException: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.PostgreSQL9Dialectt.PostgreSQL9Dialect] as strategy [org.hibernate.dialect.Dialect]
With the default postgres dialect, I get the same error.
Any idea what's going on?
Configuration extract:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.wikiz.service.model.rep" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.pass}"/>
</bean>
And variables:
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
Ok here it is.
I am not 100% sure if this helps your case but I think this is what you need to do:
Add a jdbc connection to your database from the database tab (usually to your right)
Then add the hibernate facet to your module
Go to modules (alt+ctrl+shift+s) then add it:
Now you have enabled the persistance tab on your left (usually) and you can assign a datasource to your
Now add the hibernate configuration but you have to add the xml file of hibernate. I haven't tried it with just adding the spring application context instead of hibernate.cfg.xml. Maybe it will work...
Now you have enable the presistance configuration for intellij and you can assign a datasource to it.
Select that datasource that you want and you will be able to use the JPA console with your jpa POJOs and HSQLs

Spring - setting property value from JNDI

It is a bit of followup to my previous question
Spring and Hibernate - changing dialect
if for example I have this piece of .xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >
</prop>
</props>
</property>
</bean>
now, I wanted to set hibernate.dialect to value that is exposed by jndi by jdbc/dialect, but when I put <jee:jndi-lookup jndi-name="jdbc/MyDataSource"/> I am getting Invalid content was found starting with element 'jee:jndi-lookup'. No child element is expected at this so I suspect that I can't put any tags in prop.
So, is there any way I can insert jndi resource to this property?
Not completely sure, but you should be able to use Spring-EL here, like this:
<jee:jndi-lookup id="dialect" jndi-name="..." />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >#{dialect}</prop>
</props>
</property>
</bean>

Resources