Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Problems when trying to delete an object

  1. #1
    Join Date
    Dec 2004
    Posts
    10

    Default Problems when trying to delete an object

    Hello,

    (Versions : Spring 1.0.2; Hibernbate 2.1; Oracle 8.1.7)

    I got a problem while trying to delete an instance of type 'Periode' : the first time i try, i got a DataAccessException because there are instances of 'InfosVentes' which are linked to instances of 'Articles' which belongs to the instance of 'Periode' i want to delete > normal and expected behaviour : i catch it and display a usr-friendly warning message.

    But when i try again to delete the periode (or any other) again, i got a tomcat error message with the following stacktrace :

    Code:
    java.lang.NullPointerException
    	oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1581)
    	oracle.jdbc.driver.OraclePreparedStatement.addBatch(OraclePreparedStatement.java:8540)
    	org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:257)
    	org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:257)
    	net.sf.hibernate.impl.BatchingBatcher.addToBatch(BatchingBatcher.java:30)
    	net.sf.hibernate.persister.EntityPersister.delete(EntityPersister.java:583)
    	net.sf.hibernate.impl.ScheduledDeletion.execute(ScheduledDeletion.java:29)
    	net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
    	net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2396)
    	net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
    	com.match.ventestrad.dao.hibernate.ObjectDAOImpl$2.doInHibernate(ObjectDAOImpl.java:78)
    	org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:150)
    	com.match.ventestrad.dao.hibernate.ObjectDAOImpl.delete(ObjectDAOImpl.java:75)
    	com.match.ventestrad.dao.hibernate.PeriodeDAOImpl.delete(PeriodeDAOImpl.java:91)
    	com.match.ventestrad.service.PeriodeService$7.doInTransaction(PeriodeService.java:136)
    	org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:114)
    	com.match.ventestrad.service.PeriodeService.deletePeriode(PeriodeService.java:133)
    	com.match.ventestrad.web.actions.GererPeriodesAction.perform(GererPeriodesAction.java:71)
    	org.apache.struts.action.ActionServlet.processActionPerform(ActionServlet.java:1787)
    	org.apache.struts.action.ActionServlet.process(ActionServlet.java:1586)
    	org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:492)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    Does anyone can help me and tell me what's wrong in my code and why 2nd try doesn't behave as first? (code and mapping below)
    Thanks in advance

    Mapping :
    Code:
    <class name="Periode" table="periode">
      <id name="id" column="id_periode" type="integer" unsaved-value="null">
        <generator class="sequence">
          <param name="sequence">seq_periode</param>
        </generator>
      </id>
      <property name="libelle" column="lc_libelle" type="string" />
      <property name="dateDebut" column="da_date_debut" type="date" />
      <property name="dateFin" column="da_date_fin" type="date" />
      <property name="visibleMags" column="fl_visible_mag" type="boolean" />
      <bag name="articles" table="article" lazy="true" inverse="true" cascade="all-delete-orphan" order-by="co_sous_secteur, co_code">
        <key column="id_periode" />
        <one-to-many class="Article" />
      </bag>
    </class>
    	
    <class name="Article" table="article">
      <id name="id" column="id_article" type="integer" unsaved-value="null">
        <generator class="sequence">
          <param name="sequence">seq_article</param>
        </generator>
      </id>
      <property name="code" column="co_code" type="string" />
      <property name="libelle" column="ll_libelle" type="string" />
      <many-to-one name="sousSecteur" column="co_sous_secteur" class="SousSecteur" cascade="none" outer-join="true" />
      <many-to-one name="periode" column="id_periode" class="Periode" cascade="none" outer-join="true" />
    </class>
    
    <class name="InfoVentes" table="info_ventes">
      <id name="id" column="id_info_ventes" type="integer" unsaved-value="null">
        <generator class="sequence">
          <param name="sequence">seq_info_ventes</param>
        </generator>
      </id>
      <property name="qteLivree" column="nb_qte_livree" type="integer" />
      <property name="qteVendue" column="nb_qte_vendue" type="integer" />
      <property name="qtePerdue" column="nb_qte_perdue" type="integer" />
      <property name="qteRecomm" column="nb_qte_preconisee" type="integer" />
      <property name="site" column="id_site" type="integer" />
      <many-to-one name="article" column="id_article" class="Article" cascade="none" outer-join="true" />
    </class>
    Code:
    public class PeriodeService &#123;
      public void deletePeriode&#40;final Periode periode&#41; throws ServiceException &#123;
        TransactionTemplate template = new TransactionTemplate&#40;ptm&#41;;
        template.execute&#40;new TransactionCallback&#40;&#41; &#123;
          public Object doInTransaction&#40;TransactionStatus status&#41; &#123;
            try &#123;
              periodeDAO.delete&#40;periode&#41;;
              return null;
            &#125;
            catch &#40;DataAccessException e&#41; &#123;
              throw new ServiceException&#40;"DELETE FAILED", e&#41;;
            &#125;
          &#125;
        &#125;&#41;;
      &#125;
    &#125;
    
    public class PeriodeDAOImpl implements PeriodeDAO &#123;
      public void delete&#40;final Object obj&#41; throws DataAccessException &#123;
        hibernateTemplate.execute&#40;new HibernateCallback&#40;&#41; &#123;
          public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123;
            session.delete&#40;obj&#41;;
            session.flush&#40;&#41;;
            return null;
          &#125;
        &#125;&#41;;
      &#125;
    &#125;

  2. #2

    Default

    This is almost certainly a Hibernate/JDBC problem, not a Spring one. Try the Hibernate forum:

    http://forum.hibernate.org/

    Having said that, make sure you're not trying to reuse the Hibernate session that caused the rollback.

  3. #3
    Join Date
    Dec 2004
    Posts
    10

    Default

    Maybe it's a stupid question but : how can i be sure i'm not reusing the same session? :?:

  4. #4

    Default

    Whatever method is retrying the operation should be outside of the transaction and session scope, and it shouldn't reuse existing persistent object instances. This is covered in the Hibernate manual:

    http://www.hibernate.org/hib_docs/re...ata-exceptions

    How you determine the scope of a transaction/session depends on how you've set up Hibernate and Spring, so I can't answer that.

  5. #5
    Join Date
    Dec 2004
    Posts
    10

    Default

    Thanks for your help rhasselbaum!
    It works fine with this code :

    Code:
    public class PeriodeDAOImpl implements PeriodeDAO &#123; 
      public void delete&#40;final Object obj&#41; throws DataAccessException &#123; 
        hibernateTemplate.execute&#40;new HibernateCallback&#40;&#41; &#123; 
          public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123; 
            session.reconnect&#40;&#41;;  //added line
            session.delete&#40;obj&#41;; 
            session.flush&#40;&#41;; 
            return null; 
          &#125; 
        &#125;&#41;; 
      &#125; 
    &#125;

  6. #6
    Join Date
    Dec 2004
    Posts
    10

    Default

    oups! :oops:
    I didn't look the error message carefully : it doesn't work > message says that "session is already connected" so it can't reconnect...

    So that doesn't solve my problem but I think solution is near...

  7. #7

    Default

    Reconnecting the session is not discarding it. You should not try to continue using the session. Please read the manual.

  8. #8
    Join Date
    Dec 2004
    Posts
    10

    Default

    The problem is while using 'Hibernate Template', I have no real visibility on session lifecycle (I have no 'session.open()' and 'session.close()' or equivalent methods in my code... everything is supposed to be handle by HibernateTemplate)

  9. #9

    Default

    Quote Originally Posted by Bradlex
    The problem is while using 'Hibernate Template', I have no real visibility on session lifecycle (I have no 'session.open()' and 'session.close()' or equivalent methods in my code... everything is supposed to be handle by HibernateTemplate)
    Right, so at a minimum, the session lifecycle is the duration of the call to HibernateTemplate. But, if you're using TransactionProxyFactoryBean or some other construct that opens a thread-local session, the lifecycle starts earlier and ends later. Although you don't open and close the session yourself, it's important to understand its scope. Typically, if you're using TransactionProxyFactoryBean the session scope is the same as the transaction's.

  10. #10
    Join Date
    Dec 2004
    Posts
    10

    Default

    OK. Thanks for your help; I have not enough time to go further this way so I will add some code that will check for linked objects before deleting the 'Periode' object. Sure it is not the best and 'cleanest' solution to my problem but, at least, it will fix it till i got more time to fix it properly...

    thanks again rhasselbaum.

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  5. Replies: 2
    Last Post: Mar 14th, 2005, 03:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •