-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Synchronize data with database
PostPosted: Sun Jun 10, 2007 11:42 am 
Newbie

Joined: Sun Jun 10, 2007 11:13 am
Posts: 3
Hi

I'm very new to hibernate, but has this is a very specific problem, i feel the need to make the question here.

I'm using Hibernate Core + Annotations + EntityManager in a Java EE environment, but without a EJB3 containner (i'm using tomcat).


I don't know if i'm using a correct arquitecture or not... it's just working for now!

My problem is that when i change "manually" the data in the database, the EntityManager does not load's this data again... the state of the Entity stays as if it was not updated.

So, to go around that problem, i'm calling the refresh() method of the entity manager for every entity read from the database, but although this is not an application with strong performance issues, i don't think this is the most elegant way of doing things...

Some code to show what i'm doing:

DbConn class: used to get the entity manager
Code:
public class DbConn {
   
   private static EntityManager entityManager;
   
   /**
    * This method will return an instance of entityManager; If it has not yet been instancieted, it will create one;
    * @return
    */
   public static synchronized EntityManager getSrocEntityManager(){
      
      if(entityManager==null){
         EntityManagerFactory emf = Persistence.createEntityManagerFactory("sroc");
         entityManager = emf.createEntityManager();
      }
      
      return entityManager;
   }

}


A simple method to list Cliestes entity from the database:
Code:
   public List<ClientesForm> findAll(){
      //return em.createQuery("SELECT c FROM Clientes c").getResultList();
      
      List<ClientesForm> result = new ArrayList();
      
      List<Clientes> clientes = em.createQuery("SELECT c FROM Clientes c").getResultList();
      Iterator it = clientes.iterator();
      Clientes cl = null;
      ClientesForm clForm = null;
      while(it.hasNext()){
         cl = (Clientes)it.next();
         em.refresh(cl); //synchronize with the database
         
         clForm = new ClientesForm();
         
         clForm.setClienteId(cl.getClienteId());
         clForm.setCodigoPostal(clForm.getCodigoPostal());
         clForm.setEmail(cl.getEmail());
         clForm.setFax(cl.getFax());
         clForm.setMorada(cl.getMorada());
         clForm.setNif(cl.getNif());
         clForm.setNome(cl.getNome());
         clForm.setSigla(cl.getSigla());
         clForm.setTelefone(cl.getTelefone());
         
         result.add(clForm);
      }
      
      return result;
   }



Is there any way of make this work without having to call refreash for every object returned? Is this the correct approach?


Top
 Profile  
 
 Post subject: Ideas?
PostPosted: Tue Jun 12, 2007 7:12 am 
Newbie

Joined: Sun Jun 10, 2007 11:13 am
Posts: 3
Anyone? Some coments would be apreciated...

Thanks
kimile


Top
 Profile  
 
 Post subject: Hi
PostPosted: Tue Jun 12, 2007 8:27 am 
Newbie

Joined: Tue Jun 12, 2007 6:44 am
Posts: 13
Location: Brazil
To apanhando para usar esse hibernate também.
Isso é programação extrema, voce nunca sabe o resultado final.

Dei uma olhada no seu codigo e não vi nada estranho.



Tente dar uma olhada na configuração do seu entitymanager (criacao). Sabe como é neh: temos resultset updatable, not updatable.

[]´s


Top
 Profile  
 
 Post subject: Re: Hi
PostPosted: Sat Jun 16, 2007 6:18 am 
Newbie

Joined: Sun Jun 10, 2007 11:13 am
Posts: 3
charllescuba wrote:
To apanhando para usar esse hibernate também.
Isso é programação extrema, voce nunca sabe o resultado final.

Dei uma olhada no seu codigo e não vi nada estranho.



Tente dar uma olhada na configuração do seu entitymanager (criacao). Sabe como é neh: temos resultset updatable, not updatable.

[]´s


Em português não :)

Hell, my code works... but i have to go call refresh in every entity i get from hibernate!!

I'll have a look at some configuration options for the entity manager.

Thanks
kimile


Top
 Profile  
 
 Post subject: Re: Synchronize data with database
PostPosted: Wed Jun 20, 2007 12:10 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 9:11 pm
Posts: 32
kimile wrote:
My problem is that when i change "manually" the data in the database, the EntityManager does not load's this data again... the state of the Entity stays as if it was not updated.


Any ORM including Hibernate will only know about data that you tell it about. It makes an assumption that it is the only thing modifying the database. There might be an option to "refresh" all objects before each access; but I would be surprised if there is.

It's not performant at all to refresh every object on every access. If this is THE common use case in your application, are you sure you want to use an ORM?

An option to catch conflicting updates would be to use versioning. You then need to make sure that the manual db update also increments the version column. You could then add special error handling code for when there is a version conflict. Versioning is laid out pretty well in the docs.

Chris...

_________________
___________
Chris....
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 2:51 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hibernate does not assume it's the only one working on the DB.
It assumes that you are using DB transactions with what you expect as an isolation level

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.