Spring+JPA/Hibernate questions - java

I have working code that saves entity to DB with using EJB+JPA+Hibernate. Now I need to change EJB to Spring.
Below is my simplified manager class.
//#Stateless - changed to #Service
#Service
public class Manager {
//#EJB - changed to Autowired
#Autowired
private ClientDao clientDao;
public void addClient(Client client) {
clientDao.addClient(client);
}
}
Below is my DAO class.
//#Stateless - changed to #Repository
#Repository
public class JpaClientDao implements ClientDao {
#PersistenceContext(unitName="ClientsService")
private EntityManager em;
public void addClient(Client client) {
em.persist(client);
}
}
Below is 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_2_0.xsd"
version="2.0">
<persistence-unit name="ClientsService" transaction-type = "JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>myJtaDatabase</jta-data-source>
<class>com.entity.Client</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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" />
<context:annotation-config/>
</beans>
resoures.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<Resource id="myJtaDatabase" type="DataSource">
JdbcDriver org.apache.derby.jdbc.ClientDriver
JdbcUrl jdbc:derby://localhost:1527/C:/ClientDB
UserName test
Password 123456
validationQuery = SELECT 1
JtaManaged true
</Resource>
</resources>
Questions.
1) When I used EJB, I had Container-Managed Transactions. Who should manage transactions with using Spring?
2) Do I need to necessarily use Spring Framework transaction management? Is there any alternatives?
I found some examples like this http://www.codingpedia.org/ama/java-persistence-example-with-spring-jpa2-and-hibernate/, and I cant undestand is code below spring specific or it suits for me.
<!-- ************ JPA configuration *********** -->
<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:config/persistence-demo.xml" />
<property name="dataSource" ref="restDemoDS" />
<property name="packagesToScan" value="org.codingpedia.demo.*" />
<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>
3) Do I need to edit Java code or should my steps be in xml configuration?
Any useful links are encouraged.

1) When I used EJB, I had Container-Managed Transactions. Who should
manage transactions with using Spring?
Answer : Spring provides container managed transaction support too (See JTA transaction manager , sample oracle article) and also application managed transaction (which means your application can declaratively/programatically manage the transactions with minimal effort by using Spring transaction API ) . See Spring transaction doc
2) Do I need to necessarily use Spring Framework transaction
management? Is there any alternatives?
Answer: If spring framework is not managing your transactions, then your container will need to manage them..you have alternatives of any Java EE JTA implementations like opensource JBossTS or enterprise JTA implementations Oracle-WebLogicJtaTransactionManager or IBM-WebSphereUowTransactionManager, you can find how to use them in the same point 1 spring documentation. You can even have your own transaction manager implemented.
But if you are already using Spring framework why not benefit from spring transaction management with plenty of configurations possible, (Spring + Hibernate transaction manager, Spring + JPAContainerManager, Spring+ JTAContainerManager etc..)
3) Do I need to edit Java code or should my steps be in xml
configuration?
Answer: Your transcation manager xml config seems fine to use a JpaTransactionManager, now you can initiate transactions in your service layer java code by annotating #Transactional which typically handles your service methods to participate in a transaction according to the configured transaction manager.
#Service
#org.springframework.transaction.annotation.Transactional
public class Manager {

Related

What is HibernateTemplate class?

I am new in Hibernate currently want to implement the Hibernate Template classes , any one please tell me about the Hibernate Template classes.
xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="mnrpass" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>employee.hbm.xml</value>
</list>
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="springHibernateExample" class="com.javarticles.spring.hibernate.SpringHibernateExample">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Copied from Hibernate Interview Questions:
Hibernate Template
When Spring and Hibernate integration started, Spring ORM provided two
helper classes – HibernateDaoSupport and HibernateTemplate. The reason
to use them was to get the Session from Hibernate and get the benefit
of Spring transaction management. However from Hibernate 3.0.1, we can
use SessionFactory getCurrentSession() method to get the current
session and use it to get the spring transaction management benefits.
If you go through above examples, you will see how easy it is and
that’s why we should not use these classes anymore.
One other benefit of HibernateTemplate was exception translation but that can be achieved easily by using #Repository annotation with
service classes, shown in above spring mvc example. This is a trick
question to judge your knowledge and whether you are aware of recent
developments or not.
HibernateTemplate is a helper class that is used to simplify the data access code. This class supports automatically converts HibernateExceptions which is a checked exception into DataAccessExceptions which is an unchecked exception. HibernateTemplate is typically used to implement data access or business logic services. The central method is execute(), that supports the Hibernate code that implements HibernateCallback interface.
Define HibernateTemplate.
org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.
HibernateTemplate benefits :
HibernateTemplate simplifies interactions with Hibernate Session.
The functions that are common are simplified to single method calls.
The sessions get automatically closed.
The exceptions get automatically caught and are converted to runtime exceptions.
Visit this link for the proper example
http://www.javarticles.com/2015/04/spring-hibernatetempate-example.html
HibernateTemplate is the class of org.springframework.orm.hibernate3. HibernateTemplate provides the integration of hibernate and spring. Spring manages database connection DML, DDL etc commands by itself. HibernateTemplate has the methods like save,update, delete etc.
Try to understand how to configure HibernateTemplate in our spring application.
Add xml configuration in application.xml of spring application.
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
hibernateTemplate will be used in the dao classes. Initialize hibernateTemplate. Provide a setter method to set hibernateTemplate by spring.
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void persist(){
User u1= new User(1,"Ankita");
hibernateTemplate.save(u1);
User u2= new User(2,"Renu");
hibernateTemplate.save(u2);
}

Is there a Spring ORM/JPA successor or alternative to TopLinkJpaVendorAdapter?

This seems like it's something pretty fundamental that I should be able to find out really easily, but I have not been able to find an answer to it!
In the Spring 3.2.4 documentation, it indicates that the vendor support for Toplink Essentials has been removed:
http://docs.spring.io/autorepo/docs/spring-framework/3.2.4.RELEASE_to_4.0.0.M3/changes/pkg_org.springframework.orm.jpa.vendor.html
But nowhere can I find what the alternative to using this class actually is - all the examples of configuring Toplink with Spring seem to be from around the time of Spring 2.5 or so, e.g. https://community.oracle.com/thread/597157
Can anyone tell me what the accepted approach for defining a spring application context for a Toplink JPA implementation now?
I'm using:
Spring (4.1.4)
Toplink Essentials (2.1-60f)
Embedded Derby DB (10.11.1.1)
Tomcat (8.0.18)
My current code and config setup is shown below, and it works - in the sense that the DB gets created when I go to /help in my mock web application - but it doesn't create my entity tables automatically, which is what I'm mainly trying to find a solution for right now. I suspect it's because all that's defined below is a simple JDBC datasource, rather than a proper JPA entity manager factory that can go that extra mile and create all my entities for me automatically.
Before I migrated this to a WAR and deployed it to Tomcat, I had a standalone java application that used the same persistence.xml and it worked just fine (automatically creating the database and entity tables that is). Now that I've moved to a web application and put Spring in the mix, not so much :-(
Basically, I know I need to specify create-tables (or generateDdl=true)somewhere in my Spring application context, like is done in the persistence.xml file, but I'm not sure how to do that without the likes of a TopLinkJpaVendorAdapter class (see the commented out elements in applicationContext.xml) below.
The Spring entity manager factory seems to reference the persistence unit name defined in persistence.xml, but doesn't seem to take notice of any the rest of what's defined in there - is there a way I can get Spring to take these other settings (i.e. Toplink entity manager factory provider class, and the JDBC details, etc.) from what's defined in persistence.xml? Or is it even worth carrying on down the road of Toplink if Spring have dropped vendor implementations for it, and just go with Eclipselink or Hibernate instead? (As an aside, does anyone know why this implementation class was dropped in the first place?)
META-INF/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 persistence_1_0.xsd">
<persistence-unit name="xyz-jpa" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>org.xyz.MyDbEntity</class>
<properties>
<!--
<property name="toplink.jdbc.user" value="league"/>
<property name="toplink.jdbc.password" value="league"/>
-->
<property name="toplink.jdbc.url" value="jdbc:derby:xyz;create=true"/>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
WEB-INF/spring/applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="org.xyz.web" />
<!-- <context:spring-configured /> -->
<context:load-time-weaver aspectj-weaving="autodetect"/>
<!--+
| (1) Define a Spring JDBC datasource wrapper for an embedded Derby database
+-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:xyz;create=true"/>
</bean>
<!--+
| (2) Define a Spring ORM entity manager factory bean wrapper for a Toplink JPA implementation
+-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="xyz-jpa"/>
<property name="dataSource" ref="dataSource"/>
<!--
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database.DerbyPlatform"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="toplink.weaving">static</prop>
<prop key="toplink.logging.level">FINEST</prop>
<prop key="toplink.ddl-generation">create-tables</prop>
<prop key="toplink.ddl-generation.output-mode">both</prop>
<prop key="toplink.drop-ddl-jdbc-file-name">generated_jpa.sql</prop>
</props>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" />
</property>
-->
</bean>
<!-- (3) Define a Spring ORM transaction manager wrapper for (1) and (2) -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="helpController" class="org.xyz.web.HelpController">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>XYZ</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value> <!-- No servlet specific context config file (i.e. "dispatcher-servlet.xml") => use applicationContext.xml -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
META-INF/context.xml:
<Context path="/" reloadable="true">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
org.xyz.web.HelpController:
#RestController
public class HelpController {
private EntityManagerFactory entityManagerFactory;
public HelpController() {
}
public EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
#RequestMapping( value="/help", produces=MediaType.TEXT_HTML_VALUE )
public #ResponseBody String getHelp() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
return "Helpy Mc Help-Help";
}
}
From the Wikipedia page on TopLink:
In 2007, TopLink source code was donated to the Eclipse Foundation and the EclipseLink project was born.
So, the open-source version of TopLink now lives on as EclipseLink, which is the reference implementation for JPA 2.0.
Unless you have a very specific reason for using TopLink Essentials (which doesn't seem likely because you are using recent versions of the Spring framework and Tomcat), you should use one of the other JPA implementations.

Spring IllegalStateException: A JTA EntityManager cannot use getTransaction()

So after a big refactoring project, I am left with this exception and am unsure as how to correct it. It's dealing with some code that I did not write and I am unfamiliar with how it all works. There are other questions out there dealing with this exception, but none seem to fit my situation.
The class which uses EntityManager is SpecialClaimsCaseRepositoryImpl:
package com.redacted.sch.repository.jpa;
//Imports
#Repository
public class SpecialClaimsCaseRepositoryImpl extends SimpleJpaRepository<SpecialClaimsCaseDto, SpecialClaimsCaseDto.Id> implements SpecialClaimsCaseRepository{
#PersistenceContext(unitName = "schManager")
private EntityManager em;
//Some autogenerated methods
public void setEntityManager(EntityManager em) {
this.em = em;
}
public EntityManager getEntityManager() {
return em;
}
}
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_1_0.xsd"
version="1.0">
<persistence-unit name="schManager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/SCH_DS</jta-data-source>
<class>com.redacted.sch.domain.model.SpecialClaimsCaseDto</class>
<properties>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.dialect" value="com.bcbsks.hibernate.dialect.DB2Dialect" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.jdbc.use_scrollable_resultset" value="true" />
</properties>
</persistence-unit>
</persistence>
sch_model_spring.xml:
<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/context http://www.springframework.org/schema/context/spring-context.xsd
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">
<context:component-scan base-package="com.redacted.repository.jpa,
com.redacted.sch.domain.model,
com.redacted.sch.repository.jpa,
com.redacted.sch.service,
com.redacted.sch.service.impl"/>
<tx:annotation-driven />
<tx:jta-transaction-manager />
<!-- Data source used for testing -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
<property name="url" value="jdbc:db2:redacted.redacted.com" />
<property name="username" value="redacted" />
<property name="password" value="redacted" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="schManager" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
And here's my project structure:
>
Here's a portion of the stack trace, with the full trace at this fpaste
Caused by: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:985)
at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:67)
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)
... 80 more
I'm a total noob here, so if any other information is needed just ask and I'll update.
Thanks for all the help!
The problem is your configuration. You have hibernate configured for JTA.
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
Whereas you are using local transactions instead of distributed transactions.
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)
You have 2 possible solutions
remove the JpaTransactionManager and replace it with a JTA transaction manager
remove the remove the hibernate.transaction.manager_lookup_class from the hibernate settings.
If you don't really need distributed transactions option 2 is the easiest, if you need distributed transactions simply adding <tx:jta-transaction-manager /> will setup a proper JTA tx manager for your environment. Remove the definition for the JpaTransactionManager.
Update:
Your configuration is flawed in 2 ways.
Your EntityManager configuration already contains a jndi lookup for the datasource, which you override in your applicationContext by configuring a local datasource
You have both a <tx:jta-transaction-manager /> and JpaTransactionManager which one do you want to use? At the moment the latter is overriding the first one.
Create 2 seperate configurations one for local testing and one for production using JTA en JNDI lookups. (Preferable your testing code only overrides the beans necessary).
Use WebSphereTransactionManagerLookup for the transaction manager lookup in Hibernate
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereTransactionManagerLookup" />
and remove your current transaction manager and replace it with the WebSphereUowTransactionManager.
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
for your transaction manager lookup in Spring.
See IBM Websphere and Spring docs
for more in depth documentation.

OSGi Declarative Services and Spring

I have a data access module that provides implementations of repositories using Spring and JDBC.
Therefore, I define a Spring context as follows:
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
<bean id="annotationTransactionAspect" factory-method="aspectOf" class="org.springframework.transaction.aspectj.AnnotationTransactionAspect">
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>
I also expose the repositories implementations as services using Declarative Services as follows:
<?xml version="1.0" encoding="UTF-8"?>
<component name="cdr-repository" enabled="true" xmlns="http://www.osgi.org/xmlns/scr/v1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/scr/v1.1.0 http://www.osgi.org/xmlns/scr/v1.1.0">
<!-- Property and Properties -->
<properties entry="OSGI-INF/myrepository-component.properties" />
<!-- Service (optional) -->
<service>
<provide interface="com.example.osgi.dataaccess.api.MyRepository" />
</service>
<!-- Zero or more references to services -->
<!-- Exactly one implementation -->
<implementation class="com.example.osgi.dataaccess.jdbc.impl.MyRepositoryImpl" />
</component>
So, my services are created outside the Spring environment and therefore they are not fully configured (e.g. datasource is not injected).
I'm looking for the right way to integrate Spring with Declarative Services.
Thanks, Mickael
Spring and Declarative services are not meant to be used together. So the approach you use will not work.Declarative services is a very simple framework that can only wire services into components and publish components as services. It has no capabilities regarding jpa. So I think DS will not be a good match if you want to use something like jpa with container managed persistence.
Like Holly mentioned blueprint supports that with the help of some other aries modules. I have created a tutorial that shows how to use this in a complete example. See: http://liquid-reality.de/pages/viewinfo.action?pageId=6586413
The blueprint approach is very different from how spring does jpa and container managed transactions. In spring you normally create the datasource in your context and inject it. In blueprint you work more like in standard jpa. There the datasource is referenced in persistence.xml and looked up using jndi. Aries jndi then bridges from jndi to OSGi services so another bundle can offer the datasource as an OSGi service.
The other option you have is to use spring dm to create jpa services the "spring way". But spring dm is not maintained and has a lot of problems in OSGi. So blueprint is currently the best way to go.

Why doesn't my (spring)HibernateTransactionManager work in wicket?

I tried to shorten this to what I think is relevant, I hope it's sufficient and not overwhelming. Please help!
I'm converting a small wicket+databinder+hibernate web application to use wicket+spring+hibernate. I have a DAO service class, with a hibernate SessionFactory injected by Spring. I am able to do read-only operations using the session factory (autocommit is on by default). What I want to do is use the HibernateTransactionManager and the #Transactional annotation to do a transactional operation.
I define a DAO service implementation, which uses an injected SessionFactory in a method marked #Transactional:
public class DAO implements IDAO {
#SpringBean
private SessionFactory sessionFactory;
public DAO() {
super();
}
#Transactional
public Object execute(SessionUnit sessionUnit) {
Session sess = sessionFactory.getCurrentSession();
Object result;
result = sessionUnit.run(sess);
sess.flush();
return result;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Transactional
public boolean isObjectPersistent(Object object) {
return sessionFactory.getCurrentSession().contains(object);
}
}
When I try to call isObjectPersistent(), I get a hibernate exception because no one has called session.beginTransaction():
Caused by: org.hibernate.HibernateException: contains is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at $Proxy38.contains(Unknown Source)
at com.gorkwobbler.shadowrun.karma.db.hibernate.DAO.isObjectPersistent(DAO.java:35)
(reflection stuff omitted...)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
(reflection stuff omitted...)
at org.apache.wicket.proxy.LazyInitProxyFactory$JdkHandler.invoke(LazyInitProxyFactory.java:416)
at org.apache.wicket.proxy.$Proxy36.isObjectPersistent(Unknown Source)
I also notice from the full stack trace that the OpenSessionInViewFilter is being invoked, I'm not sure if that's relevant. Let me know if you need the rest of the stack trace.
If I create a custom WebRequestCycle subclass, which begins a transaction, I can get past this. This seems to me to undermine the purpose of #Transactional, and my implementation of it also turned out to be problematic.
Here is my applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reference: http://wicketinaction.com/2009/06/wicketspringhibernate-configuration/ -->
<beans default-autowire="autodetect"
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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">
<!-- bean definitions -->
<bean id="wicketApplication" class="com.gorkwobbler.shadowrun.karma.view.wicket.core.WicketApplication" />
<bean id="placeholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="false" />
<property name="locations">
<list>
<value>classpath*:/application.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!-- setup transaction manager -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation">
<value>classpath:/hibernate.cfg.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.gorkwobbler.shadowrun.karma.domain</value>
<value>com.gorkwobbler.shadowrun.karma.domain.*</value>
</list>
</property>
</bean>
<bean id="dao"
class="com.gorkwobbler.shadowrun.karma.db.hibernate.DAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- Don't know what this is for, but it was in the sample config I started from -->
<!-- <context:component-scan base-package="com.gorkwobbler.shadowrun.karma" /> -->
</beans>
How can I get my DAO to begin a transaction, commit at the end of that method, or rollback on error? I want to use the most minimal/standard configuration possible; I prefer annotations over XML if given the choice.
Edit:
I revised the applicationContext above to remove the AOP configuration stuff, which wasn't working, anyway.
Using the debugger, I determined that the SessionImpl stored in the TransactionInterceptor's session holder map is not the same session as the SessionImpl that is retrieved in the DAO method when I call sessionFactory.getCurrentSession(). Can anyone explain why this is? What am I doing wrong? The magic is not working. =(
Edit
I also notice the following message in my console during startup:
WARN - stractEhcacheRegionFactory - No TransactionManagerLookup found in Hibernate config, XA Caches will be participating in the two-phase commit!
It turns out that the problem was not actually in the configuration information that I posted. Sorry!
My above configuration links to an externalized hibernate.cfg.xml, which declared the following property:
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
I must've copied this from some sample hibernate config file somewhere. This property caused my SessionFactory to ignore the session context provided by spring, and use a thread-local context in its place. Removing this property fixed the problem (hibernate uses the JTA context by default if none is specified).
This can be made much simpler. Read this section 13.3.3 (Hibernate) Declarative transaction demarcation, especially the last part. This is usually enough configuration if you use #Transactional:
<?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"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- SessionFactory, DataSource, etc. omitted -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven/>
<bean id="myProductService" class="product.SimpleProductService">
<property name="productDao" ref="myProductDao"/>
</bean>
</beans>
To answer your comments:
No, if you inject the components via Spring, Spring does the wiring, hibernate doesn't need to know anything about spring, that's the whole point of using spring (to decouple the individual layers). Service and dao is a separation that not everybody uses. The important part is that the public methods backed by the interface are marked as transactional, because all methods marked as transactional will be intercepted by a proxy that does the transaction handling, while others won't.
You may want to read the general section about Declarative Transaction Managmenet in Spring to understand the process (this applies to all transactional technologies, not just to hibernate).

Categories

Resources