Note that there are some explanatory texts on larger screens.

plurals
  1. POHow use Hibernate and MyBatis in the same application
    text
    copied!<p>I want to build an application using Hibernate and MyBatis integrate with Spring. In the prototype i've got to run them, but not toghether. My application context of Spring is:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!-- Document : applicationContext-spring.xml Created on : 26 de diciembre de 2012, 15:49 Author : Pedro Fdez Description: Fichero de configuración de Spring --&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName"&gt; &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.administracion.model.dao.implementations" /&gt; &lt;tx:annotation-driven transaction-manager="txManagerHibernate"/&gt; &lt;aop:aspectj-autoproxy /&gt; &lt;!-- ............................ --&gt; &lt;!-- Configuración de datasource --&gt; &lt;!-- ............................ --&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt; &lt;property name="url" value="${jdbc.url}"/&gt; &lt;property name="username" value="${jdbc.username}"/&gt; &lt;property name="password" value="${jdbc.password}"/&gt; &lt;/bean&gt; &lt;!-- .......................... --&gt; &lt;!-- Configuración de Hibernate --&gt; &lt;!-- .......................... --&gt; &lt;!-- SessionFactory de Hibernate --&gt; &lt;bean id="sessionFactoryHibernate" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource"/&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;property name="packagesToScan"&gt; &lt;list&gt; &lt;value&gt;com.administracion.model.pojos&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- Gestor transaccional de Hibernate --&gt; &lt;bean id="txManagerHibernate" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactoryHibernate"/&gt; &lt;/bean&gt; &lt;!-- ........................ --&gt; &lt;!-- Configuración Mybatis --&gt; &lt;!-- ........................ --&gt; &lt;!-- Gestor transaccional de MyBatis --&gt; &lt;bean id="txManagerMyBatis" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;tx:advice id="txAdviceMyBatis" transaction-manager="txManagerMyBatis"&gt; &lt;tx:attributes&gt; &lt;tx:method name="*" /&gt; &lt;/tx:attributes&gt; &lt;/tx:advice&gt; &lt;aop:config&gt; &lt;aop:pointcut id="transactionPointCut" expression="execution(* com.administracion.model.dao.interfaces.*.*(..))" /&gt; &lt;aop:advisor advice-ref="txAdviceMyBatis" pointcut-ref="transactionPointCut" /&gt; &lt;/aop:config&gt; &lt;!-- SessionFactory de MyBatis --&gt; &lt;bean id="sqlSessionFactoryMyBatis" class="org.mybatis.spring.SqlSessionFactoryBean"&gt; &lt;property name="configLocation" value="classpath:conf/mybatis/mybatis-config.xml" /&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;!-- MapperFactory de Mybatis --&gt; &lt;bean id="profesionMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"&gt; &lt;property name="sqlSessionFactory" ref="sqlSessionFactoryMyBatis" /&gt; &lt;property name="mapperInterface" value="com.administracion.model.dao.mappers.IProfesionMapper" /&gt; &lt;/bean&gt; &lt;bean id="profesionService" class="com.administracion.model.dao.implementations.ProfesionDaoImpl"&gt; &lt;property name="profesionMapper" ref="profesionMapper" /&gt; &lt;/bean&gt; &lt;!-- Declaramos la exportación del servicio vía RMI --&gt; &lt;bean class="org.springframework.remoting.rmi.RmiServiceExporter"&gt; &lt;property name="registryPort" value="${rmi.port.default}"/&gt; &lt;!-- Interface del servicio que exportamos --&gt; &lt;property name="serviceInterface" value="com.administracion.model.dao.interfaces.IProfesionDao"/&gt; &lt;!-- Nombre con que el servicio se va a llamar desde afuera --&gt; &lt;property name="serviceName" value="ProfesionService"/&gt; &lt;!-- Nombre del bean de la implementación que le hemos dado en el contexto de spring --&gt; &lt;property name="service" ref="profesionService"/&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>In this way, each one have his own transaction manager and session factory. It's wrong because in a nested transaction can run several transactions, for example:</p> <ol> <li>hibernate transaction</li> <li>hibernate transaction</li> <li>mybatis transaction</li> <li><p>hibernate transaction</p> <p>If mybatis transaction make an exception, it does rollback, but not the hibernate one.</p> <p>He's readen int this forum a thread about how to share transaction between Hibernate and MyBatis, but I don't understand it. </p> <p>Can Somebody tell me about some link, or any information for fix this, please?</p> <p>Excuse me for my English. It's very bad. </p> <p>Thanks in advance. </p> <p>Pedro J.Fdez. Madrid. Spain.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload