简介
我有一个JavaEE应用程序,它包含一个RESTFul Web服务。此WS负责接收一些请求并使用XML进行回答。有存储和服务的服务来捕获MySQL DB的信息 该系统使用JAX-WS和JAX-B等API以及用于数据持久性的框架Hibernate完成。
问题
当我直接或通过WS服务更新,删除或插入我的数据库中的任何内容,并尝试通过服务查看结果时,除非重新启动我的WS,否则不会出现差异。
可能性
我相信它是关于Hibernate缓存的,但是我没有找到任何可以帮助我修复它的东西。
有人有这个问题吗?有什么想法吗?
hibernate.cfg.xml中:
<?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/context_server</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password"></property>
<mapping class="br.marcelo.bean.CsMediaAudio"/>
<mapping class="br.marcelo.bean.CsMediaImage"/>
<mapping class="br.marcelo.bean.CsMediaVideo"/>
<mapping class="br.marcelo.bean.CsContext"/>
<mapping class="br.marcelo.bean.CsMedia"/>
<mapping class="br.marcelo.bean.CsResource"/>
<mapping class="br.marcelo.bean.CsUser"/>
</session-factory>
</hibernate-configuration>
获取信息的服务:
@GET
@Path("/getResourceByIdXML/{id}")
@Produces(MediaType.APPLICATION_XML)
public CsResource getResourceByIdXML(@PathParam("id")Long id) {
return new DAOResource().find(id);
}
类:使用上面使用的 find 方法的DAOResource:
public class DAOResource extends DAOGenerics<CsResource, Long> {
private final Session s;
public DAOResource()
{
s = HibernateSessionFactory.getSession();
}
@Override
public CsResource find(Long id) {
try {
s.flush();
s.clear();
String sql = "from CsResource where id = :id";
Query qr = s.createQuery(sql);
qr.setParameter("id", id);
return (CsResource) qr.uniqueResult();
}catch(HibernateException e){
System.out.println(e);
return null;
} finally {
s.close();
}
}
}
上面使用的 getSession 方法的HibernateSessionFactory类:
public class HibernateSessionFactory {
private static SessionFactory sf;
static
{
try
{
sf = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
}
catch (Exception erro)
{
System.err.println(erro);
sf = null;
}
}
public static Session getSession()
{
return sf.openSession();
}
public static Connection getConnection(){
try {
SessionFactoryImplementor sessionFactoryImplementation = (SessionFactoryImplementor) sf;
ConnectionProvider connectionProvider = (ConnectionProvider)sessionFactoryImplementation.getConnectionProvider();
java.sql.Connection connection = connectionProvider.getConnection();
return connection;
} catch (SQLException ex) {
Logger.getLogger(HibernateSessionFactory.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
修改
调用flush()和clear()方法的DAOGenerics类
public abstract class DAOGenerics<K, G> {
private final Session s;
public DAOGenerics()
{
s = HibernateSessionFactory.getSession();
}
public abstract K buscar(G obj);
public abstract List<K> buscarTodos();
public boolean inserir(K obj){
try {
s.flush();
s.clear();
s.getTransaction().begin();
s.saveOrUpdate(obj);
s.getTransaction().commit();
s.close();
return true;
}
catch (HibernateException erro){
System.out.println(erro.getMessage());
s.flush();
s.clear();
s.getTransaction().rollback();
s.close();
return false;
}
}
public void apagar(K obj) {
try {
apagar(obj, s);
} finally {
s.flush();
s.clear();
s.close();
}
}
public void apagar(K obj, Session s) {
try {
s.flush();
s.clear();
s.getTransaction().begin();
s.delete(obj);
s.getTransaction().commit();
}
catch (HibernateException erro){
System.out.println(erro);
s.flush();
s.clear();
s.getTransaction().rollback();
}finally{
s.close();
}
}
}
答案 0 :(得分:0)
是的,如果您更改了&#34;视图&#34;之外的内容对于hibernate,hibernate缓存没有意识到这一点。所以有两个选择: