Problems (VRaptor + Hibernate request)

0

I'm using Vraptor to develop a web application. More on certain tests, I saw that Hibernate is adding to the database, but when I send Result to my page with a new list of that object I deleted from my client, apparently it is bringing something from the cache. In the base my client is with 3 items and the Hibernate brings only the old 2.

I put Dao as @RequestScoped , just so I do not have problems of this type. How do I solve this problem?

Daun code:

public class DaoMaster {
private EntityManager manager;

@Inject
public DaoMaster(EntityManager manager) {
    this.manager = manager;
}

public DaoMaster() {
}

public void cria(Object o){
    manager.getTransaction().begin();
    manager.persist(o);
    manager.getTransaction().commit();
}

public void atualiza(Object o){
    manager.getTransaction().begin();
    manager.merge(o);
    manager.getTransaction().commit();

}

public void remove(Long id, Class<?> classe){
    Object o = buscaPorId(id, classe);
    manager.getTransaction().begin();
    manager.remove(o);
    manager.getTransaction().commit();
}

public Object buscaPorId(Long id,Class<?> classe){
    try{
        return manager.find(classe, id);
    }catch(Exception e){
        return null;
    }
}

public List<?> listaTodos(Class<?> classe){
    try{
        return manager.createQuery("select t from "+ classe.getSimpleName() +" t",classe).getResultList();
    }catch(Exception e){
        System.out.println("ERRO: "+e);
        return null;
    }
}

public List<?> listaPorQueryModificada(String queryModificada, HashMap<String, Object> parametros, Class<?> classe){
    try{
        TypedQuery<?> query = manager.createQuery(queryModificada,classe);
        if(parametros != null && parametros.size() > 0){
            for(String key: parametros.keySet()){
                Object value = parametros.get(key);
                query.setParameter(key, value);
            }
        }
        return query.getResultList();
    }catch(Exception e){
        System.out.println("ERRO: "+e);
        return null;
    }
}

public List<?> listaPorNamedQuery(String namedQuery, HashMap<String, Object> parametros,Class<?> classe){
    try{
        TypedQuery<?> query = manager.createNamedQuery(namedQuery,classe);
        if(parametros != null && parametros.size() > 0){
            for(String key: parametros.keySet()){
                Object value = parametros.get(key);
                query.setParameter(key, value);
            }
        }
        return query.getResultList();
    }catch(Exception e){
        System.out.println("ERRO: "+e);
        return null;
    }
}

public Object buscaPorNamedQuery(String namedQuery,HashMap<String, Object> parametros,Class<?> classe){
    Object objeto;
    try{
        TypedQuery<?> query = manager.createNamedQuery(namedQuery,classe);
        if(parametros != null && parametros.size() > 0){
            for(String key: parametros.keySet()){
                Object value = parametros.get(key);
                query.setParameter(key, value);
            }
        }
        return query.getSingleResult();
    }catch(Exception e){
        System.out.println("ERRO: "+e);
        return null;
    }
}

}

- Detailing -

I have a screen that makes subcategories, when registering a new item, I call a function with Vraptor to add, and at the end I send it to my other function that makes the call of the screen. In the function of the screen call, all subcategories with the hibernate and control to the result, to be able to show in the screen of the user.

It only happens that when I register a new item, the application registers this item in the base, and when it goes to the function of calling the screen again to search again for all the existing subcategories, it seems that the hibernate is bringing the old search of the previous call, where the new item I just registered did not exist, so the new item does not exist on the screen.

Calling code for Vraptor:

@Admin
public void listaSubCategoria(Long categoriaID){

    List<SubCategoria> subCategorias = subCategoriaDao.listaPorCategoria(categoriaID);
    result.include("categoria",categoriaDao.buscaPorId(categoriaID));
    result.include("subCategorias", subCategorias);
    result.include("pagina", "categoria");
}

@Admin
public void adicionaSubCategoria(SubCategoria subCategoria, UploadedFile imagem){
    /*VERIFICAR SE JÁ EXISTE UMA SUB CATEGORIA COM O MESMO NOME PARA NÃO DAR ERRO*/
    //subCategoriaDao.adiciona(subCategoria,imagem);
    if(subCategoria.getId() != null){
        daoMaster.atualiza(subCategoria);
    }else{
        subCategoria.setCategoria((Categoria) daoMaster.buscaPorId(subCategoria.getCategoria().getId(), Categoria.class));
        daoMaster.cria(subCategoria);
    }

    adicionaImagem(NomenclaturaArquivo._NOMENCLATURA_IMAGEM_CATEGORIA, imagem, subCategoria.getId());

    result.redirectTo(this).listaSubCategoria(subCategoria.getCategoria().getId());
}
    
asked by anonymous 15.01.2016 / 17:25

1 answer

1

Looking at your code, I saw that you are not giving commit to your queries. commit basically terminates the transaction by saving all the changes that occurred during the transaction.

It is recommended to use commit in all operations involving a bank, even in queries. It would look like this:

public Object buscaPorId(Long id,Class<?> classe){
    try{
        manager.getTransaction().begin();
        return manager.find(classe, id);
        manager.getTransaction().commit();
    }catch(Exception e){
        return null;
    }
}
    
15.01.2016 / 18:38