estou com o seguinte erro:
1º Quando mudo o valor de alguma coisa pelo banco de dados por ex: “nome do cliente” na aplicação pressiono F5 e ele continua desatualizado.
2º Tenho a aplicação aberta em dois browsers diferentes altero alguma coisa em um deles ai no outro quando pressiono F5 ele atualiza se eu pressionar novamente mostra o valor anterior e fica em loop nisso.
o que vcs acham que pode ser?
com ninguém deu esse erro?
vc tá com o cache de 2o nivel do hibernate habilitado? vc tá usando um entityManager/Session por request?
meu hibernate.cfg
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/atendimento</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="entity.Usuario" />
<mapping class="entity.Cliente" />
<mapping class="entity.Servico" />
<mapping class="entity.Grupo" />
<mapping class="entity.Agendamento" />
</session-factory>
</hibernate-configuration>
utilizo hibernateUtil e gostaria de continuar utilizando:
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static ThreadLocal<Session> sessionThread = new ThreadLocal<Session>();
private static ThreadLocal<Transaction> transactionThread = new ThreadLocal<Transaction>();
static {
try {
sessionFactory = new AnnotationConfiguration().configure("hibernate/hibernate.cfg.xml").buildSessionFactory();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public static Session getSession() {
Session session = sessionThread.get();
if (session == null) {
session = sessionFactory.openSession();
sessionThread.set(session);
}
return session;
}
public static void closeSession() {
Session session = sessionThread.get();
if (session != null) {
session.flush();
session.close();
sessionThread.set(null);
}
}
public static Transaction beginTransaction() {
Transaction transaction = transactionThread.get();
if (transaction == null) {
transaction = getSession().beginTransaction();
transaction.begin();
transactionThread.set(transaction);
}
return transaction;
}
public static void closeTransaction() {
Transaction transaction = transactionThread.get();
if (transaction != null) {
transactionThread.set(null);
}
}
}
HibernateDao que as outras classes herdam:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package model.dao;
import br.com.caelum.vraptor.ioc.Component;
import entity.Servico;
import hibernate.HibernateUtil;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
/**
*
* @author Hugo Mesquita
*/
@Component
public class HibernateDAO<T> implements InterfaceDAO<T> {
protected Class<T> classe;
public HibernateDAO(Class<T> classe) {
this.classe = classe;
}
@Override
public Serializable salvar(T entidade) {
Serializable result = null;
try {
HibernateUtil.beginTransaction();
result = HibernateUtil.getSession().save(entidade);
HibernateUtil.beginTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.beginTransaction().rollback();
} finally {
HibernateUtil.closeTransaction();
}
return result;
}
@Override
public void alterar(T entidade) {
try {
HibernateUtil.beginTransaction();
HibernateUtil.getSession().update(entidade);
HibernateUtil.beginTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.beginTransaction().rollback();
} finally {
HibernateUtil.closeTransaction();
}
}
@Override
public void excluir(T entidade) {
try {
HibernateUtil.beginTransaction();
HibernateUtil.getSession().delete(entidade);
HibernateUtil.beginTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
HibernateUtil.beginTransaction().rollback();
} finally {
HibernateUtil.closeTransaction();
}
}
@Override
public List<T> listarPorNome(String nome) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public List<T> listar() {
return (List<T>) HibernateUtil.getSession().createCriteria(classe).list();
}
@Override
public List<T> listarPorOrdemDescId() {
return HibernateUtil.getSession().createCriteria(Servico.class).addOrder(Order.desc("id")).list();
}
@Override
public List<T> listarPorOrdemAscId() {
return HibernateUtil.getSession().createCriteria(Servico.class).addOrder(Order.asc("id")).list();
}
@Override
public T getPorId(Long id) {
return (T) HibernateUtil.getSession().get(classe, id);
}
@Override
public int count() {
Criteria criteria = HibernateUtil.getSession().createCriteria(classe);
criteria.setProjection(Projections.rowCount());
return ((Integer) criteria.list().get(0)).intValue();
}
}
o segundo já consegui corrigir obrigado! como eu desabilito essa cache de 2º nível? ela vem por default ativada?
Pronto!! consegui!!!
implementei:
[code]@Intercepts
public class HibernateTransactionInterceptor implements Interceptor {
private final Session session;
private final Validator validator;
public HibernateTransactionInterceptor(Session session, Validator validator) {
this.session = session;
this.validator = validator;
}
@Override
public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) {
Transaction transaction = null;
try {
transaction = session.beginTransaction();
stack.next(method, instance);
if (!validator.hasErrors()) {
transaction.commit();
}
} finally {
if (transaction != null && transaction.isActive()) {
transaction.rollback();
}
}
}
@Override
public boolean accepts(ResourceMethod method) {
return true; // Will intercept all requests
}
}[/code]
Obrigado pela ajuda!
ps: mudei as classes, implementei as daqui: https://github.com/caelum/vraptor-hibernate