Hi Guys,I'm about two days trying to know what is happening with my transaction on JUnit Test using doInHibernate.
This is my code:
applicationContext.xml (just the data config)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans ... >
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${hibernateConnectionUrl}" />
<property name="username" value="${hibernateConnectionUsername}" />
<property name="password" value="${hibernateConnectionPassword}" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.model.hibernate.entidade.Loja</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.connection.providerClass">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.connection.charset">UTF-8</prop>
</props>
</property>
</bean>
</bean>
LojaHibernateDao and
GenericoDaoHibernateImplCode:
@Repository("lojaDao")
public class LojaHibernateDao extends GenericoDaoHibernateImpl<Loja, Long> implements LojaDao {
@Autowired
public LojaHibernateDao(SessionFactory sessionFactory) {
super(sessionFactory);
}
//...
}
abstract class GenericoDaoHibernateImpl<T, ID extends Serializable> extends HibernateDaoSupport implements GenericoDao<T, ID> {
// ...
@SuppressWarnings("unchecked")
protected Class<T> classePersistente = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
@Override
public void salva(T objeto) {
getHibernateTemplate().saveOrUpdate(objeto);
}
@Override
public T get(ID id) {
return getHibernateTemplate().get(classePersistente, id);
}
@Override
public Integer executaAtualizacao(final String sQuery, final Map<String,Object> params) {
return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
@Override
public Integer doInHibernate(Session sessao) throws HibernateException,SQLException {
Query query = sessao.createQuery(sQuery);
if (params!=null) {
Iterator<String> iterator = params.keySet().iterator();
while(iterator.hasNext()) {
String param = iterator.next();
query.setParameter(param, params.get(param));
}
}
return query.executeUpdate();
}
});
}
// ...
}
LojaDaoTestCode:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:**beans/applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class LojaDaoTest {
@Autowired
LojaDao lojaDao;
@Test
public void pegaLojasASeremIntegradas() throws Exception {
//IT WORKS!!
Loja loja = new Loja("valid value1",...);
loja.setUrl("http://hibernate.com");
lojaDao.salva(loja);
Loja loja2 = lojaDao.get(loja.getId());
assertEquals("http://hibernate.com",loja2.getUrl());
//IT DOESNT WORK!!
String query = "update Loja set url = :url where id = :id";
Map<String, Object> params = new HashMap<String, Object>();
params.put("url", "http://cnn.com");
params.put("id", loja2.getId());
lojaDao.executaAtualizacao(query,params); //Returns the value 1
Loja loja3 = lojaDao.get(loja2.getId());
assertEquals("http://cnn.com",loja3.getUrl()); //THE OLD VALUE IS RETURNED
}
}
Debugging the code I could notice that the session ID is the same in doInHibernate scope and in the get/salva scope.
Do you know what could have being?
Thank you.
Rondon