Ver Mensaje Individual
  #2 (permalink)  
Antiguo 08/05/2011, 12:11
hualro
 
Fecha de Ingreso: enero-2008
Mensajes: 197
Antigüedad: 17 años, 1 mes
Puntos: 10
Respuesta: Insertar una Lista de Objetos con Hibernate

Puedes usar persist en lugar de save, ademas usa una transaction generandola de la session de hibernate, cuando termines y todo se inserte correctamente entonces le das transaction.commit y si hay error le das transaction.rollback.

ejemplo Usando Spring y Hibernate:

Código Java:
Ver original
  1. Session s = null;
  2. Transaction tx = null;
  3.  
  4.         try {
  5.  
  6.             s = this.getHibernateTemplate().getSessionFactory().openSession();
  7.             tx = s.beginTransaction();
  8.  
  9. /* Tu codigo de persist*/
  10. for(Object object: listaObjects) {
  11. s.persist(object);
  12. }
  13.  
  14.  
  15. tx.commit();
  16.  
  17.         } catch(HibernateException he) {
  18.             LOG.error(he.getMessage(), he);
  19.             if(Validator.isNotNull(tx)) {
  20.                 tx.rollback();
  21.                 LOG.error("Transaction Rolled Back.");
  22.             }
  23.         } finally {
  24.             if(Validator.isNotNull(s)) {
  25.                 s.clear();
  26.                 s.close();
  27.                 LOG.debug("Hibernate Session Cleared and Closed.");
  28.             }
  29.         }