0
I’m using the Vraptor
to develop a web application. More in certain tests, I saw that the Hibernate
is adding in the database, but when I send the Result
to my page with a new list of that object I deleted from my client, apparently he’s bringing something from the cache. At the base my client is with 3 items and the Hibernate
brings only the old 2.
I put Dao as @RequestScoped
, just to not have problems of this kind. How do I solve this problem?
Dao 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 I register subcategories, when registering a new item, I call a function with Vraptor to add, and at the end I have it redirected to my other function that makes the screen call. In the screen call function, I list all subcategories with Hibernate and send to the result, to be able to show on the user screen.
Only there is a time when I register a new item, the application registers this item at the base, and when it goes to the function of calling the screen again to do the new search of all existing subcategories, it seems that Hibernate is bringing the old research of the previous call, where there was not yet that new item that I just registered, making the new item does not exist on the screen.
Call 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());
}
You are committing all bank operations?
– DiegoAugusto
You said you’re deleting, and the base has 3 clients, because then it’s 2 old records? It wasn’t supposed to be 4?
– DiegoAugusto
To avoid problems with subcontractors, all dao processes, go through
manager.getTransaction().begin();
andmanager.getTransaction().commit();
, the information even not coming to the updated screen, are registered in the bank, without any problem.– Kelvin Stang Oenning
Put your code in the post and try to illustrate a little better what is happening.
– DiegoAugusto
Try committing queries as well. Put a sysout to display the list and see if it brings the right data.
– DiegoAugusto
I did the test, and again Hibernate registered correct at the base, more when it shows on screen, did not bring new information. I made the print of all before adding the list on the screen, and it is not in the function list either. If I restart the application’s Tomcat it will appear the same.
– Kelvin Stang Oenning
I forgot and mention, I do not know if it can be some configuration that is missing, but I am using the JPA plugin in vraptor. No Entity Manager with my classes.
– Kelvin Stang Oenning
I don’t know if this can be because I never used this plugin, I’ve had a similar problem and I decided by comiting my queries.
– DiegoAugusto
You say, calling the transaction Begin at the beginning and the commit at the end, also in the query functions?
– Kelvin Stang Oenning
That’s right @Kelvin
– DiegoAugusto
@Techies ran the tests, and apparently the problem really solves itself. I don’t really know if it will completely solve with this little test I did, more for this error simulation I was doing, solved. Thank you so much for your help! Thank you for paying attention to my problem. Thank you.
– Kelvin Stang Oenning
For nothing, if the problem was solved I will put an answer to help other people with the same problem
– DiegoAugusto
@Techies I did the general tests on the other screens of the system, and none presented the problem anymore. I can say that yes, it was solved. My queries were not opening the new transactions and closing at the end.
– Kelvin Stang Oenning
Calm down, I put an answer, if it meets you just mark as solution.
– DiegoAugusto