Hibernate temporary datastore - spring

I doing unit testing on spring-hibernate DAOs... configured using
#ContextConfiguration(
locations = {
"classpath:test-applicationContext.xml"
})
but it looks like its transacting with the actual database.
How do I use a temporary datastore with out working on actual database

Define your data store on a separate file and include that xml file with your mail application xml. When testing, include a separate xml file to hold your data store pointing to another database such as hsql. Then this will be the data source referred to by your main applicationContext.xml.

Thanks Guys I used H2 and got it working:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url"
value="jdbc:h2:mem:processdb;INIT=RUNSCRIPT FROM 'classpath:create.sql'" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="stateDAO" class="com.tutorial.jquery.dao.impl.StateDAOImpl"></bean>
<bean id="stateService" class="com.tutorial.jquery.service.impl.StateServiceImpl"></bean>

Related

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 bean optional property

I am using a data source defined in tomcat in my spring configuration as shown in the below xml.
It can happen sometimes that this data source may not be defined in the context.xml of tomcat.
In such cases , the context initialization fails since myDS is not found.
Is it possible to configure the datasource as optional so that application initialisation is not impacted ?
There can be a run time error when this data source is accessed , which is acceptable
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/myDS"/>
</bean>
<bean id="myEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="packagesToScan" value="com..XX.XX" />
<property name="persistenceUnitName" value="myPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="#{systemProperties['showSql'] == null ? 'true' : systemProperties['showSql'] }" />
</bean>
</property>
<property name="persistenceUnitPostProcessors">
<list>
<ref bean="wrkflw-punitpostprocessor" />
</list>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">#{systemProperties['dbDialect']}</prop>
</props>
</property>
</bean>
Thanks
Muhad
You may check the DelegatingDataSource, you could encapsulate the logic to load the datasource from JNDI within its instantiation. For your application there will be always a DataSource there, but in some cases (whenever its not able to load the DataSource from JNDI) there is no delegation.

How could I use Spring's TransactionInterceptor with JPA?

I have an existing project using Spring 3 and Hibernate 3. I have the following code in order to "safe-guard" the database consistency. If I'm going to convert the project into JPA, how could I resolve the transactionManager property inside the transactionInterceptor bean since JPA using persistence.xml and doesn't make use of dataSource and sessionFactory?
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="save">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Try using a configuration similar to the below xml snippet. This has been tested with Hibernate 4, but I would expect it to work with version 3 as well.
<!-- EntityManagerFactory configuration that doesn't need a persistence.xml -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="${jpa.entity.packages}">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="${hibernate.show_sql}"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Scans for classes/methods with #Transactional annotation to apply the
transaction management aspect (TransactionInterceptor) on them. -->
<tx:annotation-driven/>

spring jndi values from JNDI tree

I am working with spring 3.0 and within our JBoss application server I perform a JNDI lookup to a remote WebLogic application server. The properties to connect to the WebLogic JNDI server are contained in a properties file called soa.properties. Here is what the spring-config.xml looks like to initialize the JndiTemplate bean
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:app.properties</value>
<value>classpath:soa.properties</value>
</list>
</property>
</bean>
<bean id="soaJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${soa.provider.initial}</prop>
<prop key="java.naming.provider.url">${soa.provider.url}</prop>
<prop key="java.naming.security.principal">${soa.security.principal}</prop>
<prop key="java.naming.security.credentials">${soa.security.credentials}</prop>
</props>
</property>
</bean>
This works fine for pulling my values out of the soa.properties file, but what I would rather do is pull the WebLogic JNDI connection values out of the JBoss JNDI tree.
My problem appears to be with the initialization of the InitialContext object that requires Properties for initialization.
I put all of my JNDI String values for WebLogic JNDI connection into the JBoss JNDI. Below is the code I am using to pull the values out and initialize JndiTemplate bean.
<bean id="soaUrl" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="soa/provider/url" />
</bean>
<bean id="soaFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="soa/provider/connection/factory" />
</bean>
<bean id="soaInitial" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="soa/provider/initial" />
</bean>
<bean id="soaPrincipal" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="soa/security/principal" />
</bean>
<bean id="soaCredential" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="soa/security/credential" />
</bean>
<bean id="testsoaJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<map>
<entry key="java.naming.factory.initial" value-ref="soaUrl" />
<entry key="java.naming.provider.url" value-ref="soaFactory" />
<entry key="java.naming.provider.initial" value-ref="soaInitial" />
<entry key="java.security.principal" value-ref="soaPrincipal" />
<entry key="java.security.credential" value-ref="soaCredential" />
</map>
</property>
</bean>
My application comes up with these beans created, but when I use testsoaJndiTemplate with say soaConnectionFactory like so it boms.
<bean id="soaConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="testsoaJndiTemplate" />
<property name="jndiName" value="${soa.provider.connection.factory.name}" />
</bean>
The error is so cryptic it does make any sense.
Thanks

spring transaction and aop

I'm writing a simple application with spring, I defined my daos and services with spring annotations and defined hibernate as the orm and transaction manager like this:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:env/hibernate.cfg.xml</value>
</property>
<property name="packagesToScan">
<list>
<value>com.skyfence.management.cm.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
like you can see I'm using annotations for the transactions management,
Until this point everything works fine.
Then I added a Logger Aspect to add log4j printouts before and after every method so I added the following to my applicationContext.xml
<aop:aspectj-autoproxy />
and created a new annotated aspect class:
#Aspect
#Component
public class LoggingAspect
{
}
The problem is that from that point hibernate is no longer working and I'm getting the following exception:
org.hibernate.HibernateException: No Session found for current thread
I suspect that somehow by adding the aspect the transactions stopped working but I have no idea how to solve it
Any help will be appreciated,

Resources