-->
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.  [ 2 posts ] 
Author Message
 Post subject: Why Hibernate intend to update table without call to update?
PostPosted: Tue Mar 27, 2012 2:01 pm 
Newbie

Joined: Tue Mar 27, 2012 1:21 pm
Posts: 1
Hi all..

Background:
I have two DB, in the first one I have a tbl_seg_usuarios table (users of my application), wich have a one-to-one relationship with tbl_seg_usuarios_detalles (detailed information of the user, only for some users), in the second DB I have a employes table with detailed information of my users in the first DB.


I have a methos to search users from DB. The query looks like this...
Code:
public List<T> search(Map<String, Object> searchOpts, int maxResults, String sortProperty, String sortDirection) {
      List<T> list = null;

      List<Object> values = new ArrayList<Object>();
      StringBuilder hql = new StringBuilder("from " + this.persistentClass.getSimpleName());
      if(searchOpts != null) {
         // Constructs query...
      }
      if(StringUtils.isNotBlank(sortProperty)) {
         // Constructs sort...
      }

      if(maxResults < 0) {
         maxResults = Constants.HIBERNATE_MAX_RESULTS;
      }
      hibernateTemplate.setMaxResults(maxResults);

      [b]list = hibernateTemplate.find(hql.toString(), values.toArray());[/b]

      return list;
   }


That method is called from a Controller, I'm using Spring 3, like this...
Pass across the business layer...
Code:
public List<T> search(Map<String, Object> searchOpts, int maxResults, String sortProperty, String sortDirection) {
      return dao.search(searchOpts, maxResults, sortProperty, sortDirection);
   }


Then in the Controller...
Code:
List<NotificacionArea> notificacionAreaList = notificacionAreaManager.getAll();
List<Usuario> usuarioList = userManager.search(null, 0, null, null);
usuarioList = userManager.cargarInformacionUsarios(usuarioList);


The userManager.search(...) returns a list with 3 users. Now, I'm working with two databases, in the 2nd database stores the detailed information of the users, so, I must update the objects in the list with that information.
So I call usuarioList = userManager.cargarInformacionUsarios(usuarioList);.

The code of cargarInformacionUsarios is this..
Code:
public List<Usuario> cargarInformacionUsarios(List<Usuario> usuarios) throws UsuarioRHNotFoundException {
       for(Usuario usuario : usuarios) {
          boolean esRH = usuario.getEsRH();
          if(esRH) {
             UsuarioDetalle detalle = cargarInformacionUsario(usuario.getUsername(), usuario.getPassword());
             usuario.setDetalle(detalle); [b]// HERE sets the detailed information to the users in the usuarioList.[/b]
          }
       }
        [b]System.out.println(usuarios);[/b]
       return usuarios;
    }

Code:
public UsuarioDetalle cargarInformacionUsario(String nombreUsuario, String password) throws UsuarioRHNotFoundException {
       UsuarioRH usuarioRH = rhUserManager.getUserByCredentials(nombreUsuario, password);
      UsuarioDetalle detalle = new UsuarioDetalle(usuarioRH);
      return detalle;
    }


All works fine and whe I print the list of users (System.out.println(usuarios);), the detailed information is OK...
the problem occurs when the execution finishes the cargarInformacionUsarios(...) method, and Hibernate throws the next error in the Java console:
Code:
WARN [33139188@qtp-5568322-11] JDBCExceptionReporter.logExceptions(233) | SQL Error: 2291, SQLState: 23000
ERROR [33139188@qtp-5568322-11] JDBCExceptionReporter.logExceptions(234) | ORA-02291: integrity constraint (SAE.FK_USU_DET_USUARIOS) violated - parent key not found

ERROR [33139188@qtp-5568322-11] AbstractFlushingEventListener.performExecutions(324) | Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update


And in the screen of my Web application prints the error:
Code:
Could not execute JDBC batch update; SQL [[b]insert into tbl_seg_usuarios_detalles[/b] (apellido_materno, apellido_paterno, cr, email, estado, fecha_nacimiento, nombre, id_usuario) values (?, ?, ?, ?, ?, ?, ?, ?)]; constraint [SAE.FK_USU_DET_USUARIOS]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update


So this is my question, when or how Hibernate try to update directly to the DB the detailed information of the users in the usuarioList, if I haven't called explicitly any update() method to Hibernate?
Why hibernate tries to execute insert into tbl_seg_usuarios_detalles...?
And how can I to prevent this?

I hope I have explained clearly my problem... Thanks...


Top
 Profile  
 
 Post subject: Re: Why Hibernate intend to update table without call to update?
PostPosted: Fri Mar 30, 2012 7:31 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
all objects loaded by Hibernate are managed, until you detach them, close the Session, or clear the Session. That means it monitors for changes, there is no need to call any "update" method.

If you change an entity, you are effectively scheduling update operations to be performed at transaction commit.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.