Announcement Announcement Module
Collapse
No announcement yet.
Spring/Hibernate Delete/Update Problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring/Hibernate Delete/Update Problem

    Hello,

    I am using Spring 1.2.1 + Hibernate 3.05 and quite new to it. In my first version all worked quite well, except of lazy initialization. So i implemented a solution for that.

    Now i have the problem, that i can load the objects, but cant delete or manipulate them. It seems, the session takes the objects but doesn't synchronize it with the database. I hope you can tell me why, the documentation doesn't help me anymore.

    Here is my code:



    Code:
    	
    <bean id="mySessionFactory"
    - some configuration -
    </bean>
    <bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="mySessionFactory"/></property>
    	</bean>
    	
    	<bean id="myTransactionInterceptor" 
            class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager">
                <ref bean="myTransactionManager"/>
            </property>
            <property name="transactionAttributes">
                <props>
                	 <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
    	<bean id="myPersistenceManager" class="PersistenceManager">
     		 <property name="sessionFactory">
      			<ref bean="mySessionFactory"/>
     		 </property>
    	</bean>
    	
    	<bean id="myPersistenceServiceTarget" class="PersistenceManagerServiceImpl">
            <property name="persistenceManager">
                <ref bean="myPersistenceManager"/>
            </property>
        </bean>
    
    	<bean id="myPersistenceService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces">
                <value>de.wiol.infomat.persistence.PersistenceManagerService</value>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>myTransactionInterceptor</value>
                    <value>myPersistenceServiceTarget</value>
                </list>
            </property>
        </bean>
    	
    </beans>
    Example method in PersistenceManager for deleting, other methods are similar to that.
    Code:
    public void deleteObject&#40;Persistable obj&#41; throws DataAccessException
        &#123;
            Session session = SessionFactoryUtils.getSession&#40;getSessionFactory&#40;&#41;,
                    true&#41;;
            try
            &#123;
                session.delete&#40;obj&#41;;
            &#125;
            catch &#40;HibernateException e&#41;
            &#123;
                SessionFactoryUtils.convertHibernateAccessException&#40;e&#41;;
            &#125;
        &#125;
    Will be called from the Service class
    public void deleteObject(Persistable obj)
    {
    this.persistenceManager.deleteObject(obj);
    }
    Thx for the time.

  • #2
    Please post your Hibernate mapping files. Also, if you turn on SQL logging in the Hibernate config or log4j, you should see SQL scroll by, indicating what Hibernate is doing. Be sure you are doing this within the scope of a transaction, otherwise your changes will update the database, but then immediately be rolled back! See the HibernateTemplate in the Spring docs.

    Log4J param to turn on SQL logging.

    Code:
    log4j.logger.org.hibernate.SQL=DEBUG

    Comment


    • #3
      First of all, thx.

      I think i dont get the hint on HibernateTemplate, if i use that instead of the SessionFactoryUtils, lazy initialisation does not work.

      The log, opens one session for loading and one for deleting, it seems that the second session doenst take part in any transaction but i thought that is what Spring takes care about, so i like to know why:

      Code:
      09&#58;08&#58;39,187 DEBUG SessionImpl&#58;250 - opened session at timestamp&#58; 4582683931197440
      09&#58;08&#58;39,187 DEBUG DefaultLoadEventListener&#58;143 - loading entity&#58; &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,187 DEBUG DefaultLoadEventListener&#58;290 - attempting to resolve&#58; &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,187 DEBUG DefaultLoadEventListener&#58;326 - object not resolved in any cache&#58; &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,187 DEBUG BasicEntityPersister&#58;2467 - Materializing entity&#58; &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,187 DEBUG Loader&#58;1340 - loading entity&#58; &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,202 DEBUG AbstractBatcher&#58;290 - about to open PreparedStatement &#40;open PreparedStatements&#58; 0, globally&#58; 0&#41;
      09&#58;08&#58;39,202 DEBUG ConnectionManager&#58;296 - opening JDBC connection
      09&#58;08&#58;39,874 DEBUG SQL&#58;324 - select fulldocume0_.id as id1_, fulldocume0_.name as name1_1_, fulldocume0_.path as path1_1_, fulldocume0_.datatype_id as datatype4_1_1_, fulldocume0_.extracted_text as extracted5_1_1_, fulldocume0_.analysed_text as analysed6_1_1_, fulldocume0_.extraction_date as extraction7_1_1_, fulldocume0_.extraction_size as extraction8_1_1_, datatype1_.id as id0_, datatype1_.name as name4_0_, datatype1_.programPath as programP3_4_0_ from document fulldocume0_ inner join datatype datatype1_ on fulldocume0_.datatype_id=datatype1_.id where fulldocume0_.id=?
      Hibernate&#58; select fulldocume0_.id as id1_, fulldocume0_.name as name1_1_, fulldocume0_.path as path1_1_, fulldocume0_.datatype_id as datatype4_1_1_, fulldocume0_.extracted_text as extracted5_1_1_, fulldocume0_.analysed_text as analysed6_1_1_, fulldocume0_.extraction_date as extraction7_1_1_, fulldocume0_.extraction_size as extraction8_1_1_, datatype1_.id as id0_, datatype1_.name as name4_0_, datatype1_.programPath as programP3_4_0_ from document fulldocume0_ inner join datatype datatype1_ on fulldocume0_.datatype_id=datatype1_.id where fulldocume0_.id=?
      09&#58;08&#58;39,890 DEBUG AbstractBatcher&#58;378 - preparing statement
      09&#58;08&#58;39,890 DEBUG LongType&#58;59 - binding '2' to parameter&#58; 1
      09&#58;08&#58;39,983 DEBUG AbstractBatcher&#58;306 - about to open ResultSet &#40;open ResultSets&#58; 0, globally&#58; 0&#41;
      09&#58;08&#58;39,983 DEBUG Loader&#58;405 - processing result set
      09&#58;08&#58;39,983 DEBUG Loader&#58;410 - result set row&#58; 0
      09&#58;08&#58;39,983 DEBUG LongType&#58;86 - returning '3' as column&#58; id0_
      09&#58;08&#58;39,983 DEBUG Loader&#58;828 - result row&#58; EntityKey&#91;de.wiol.infomat.data.DataType#3&#93;, EntityKey&#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,983 DEBUG Loader&#58;978 - Initializing object from ResultSet&#58; &#91;de.wiol.infomat.data.DataType#3&#93;
      09&#58;08&#58;39,999 DEBUG BasicEntityPersister&#58;1651 - Hydrating entity&#58; &#91;de.wiol.infomat.data.DataType#3&#93;
      09&#58;08&#58;39,999 DEBUG StringType&#58;86 - returning 'doc' as column&#58; name4_0_
      09&#58;08&#58;39,999 DEBUG StringType&#58;86 - returning '' as column&#58; programP3_4_0_
      09&#58;08&#58;39,999 DEBUG Loader&#58;978 - Initializing object from ResultSet&#58; &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,999 DEBUG BasicEntityPersister&#58;1651 - Hydrating entity&#58; &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;39,999 DEBUG StringType&#58;86 - returning 'bingo.doc' as column&#58; name1_1_
      09&#58;08&#58;39,999 DEBUG StringType&#58;86 - returning '/home/haeudel/dokumente/bingo.doc' as column&#58; path1_1_
      09&#58;08&#58;39,999 DEBUG LongType&#58;86 - returning '3' as column&#58; datatype4_1_1_
      09&#58;08&#58;39,999 DEBUG StringType&#58;86 - returning '
      
      Science Bingo
      
      Select of the terms provided. 
      Write one word in each of the 25 boxes in whatever sequence you desire. Do not repeat words, and do not leave any spaces blank.
      The instructor will read definitions in sequence.  Place the number of the definition in the box with the associated term.  When you have five in a row, shout ?Bingo!?
      
      
      
      Name&#58;________________________________________     Date&#58; __________________
      
      &#40;Norman Herr, 2000
      
      
      
      ' as column&#58; extracted5_1_1_
      09&#58;08&#58;39,999 DEBUG StringType&#58;86 - returning 'science bingo select term provid write one word boxe whatev sequence desire repeat word leave space blank instructor read definit sequence place number definit box associ term five row shout bingo name date norman herr ' as column&#58; analysed6_1_1_
      09&#58;08&#58;40,046 DEBUG TimestampType&#58;86 - returning '2005-06-13 15&#58;37&#58;57' as column&#58; extraction7_1_1_
      09&#58;08&#58;40,046 DEBUG LongType&#58;86 - returning '20480' as column&#58; extraction8_1_1_
      09&#58;08&#58;40,046 DEBUG Loader&#58;429 - done processing result set &#40;1 rows&#41;
      09&#58;08&#58;40,046 DEBUG AbstractBatcher&#58;313 - about to close ResultSet &#40;open ResultSets&#58; 1, globally&#58; 1&#41;
      09&#58;08&#58;40,046 DEBUG AbstractBatcher&#58;298 - about to close PreparedStatement &#40;open PreparedStatements&#58; 1, globally&#58; 1&#41;
      09&#58;08&#58;40,046 DEBUG AbstractBatcher&#58;416 - closing statement
      09&#58;08&#58;40,046 DEBUG Loader&#58;528 - total objects hydrated&#58; 2
      09&#58;08&#58;40,046 DEBUG TwoPhaseLoad&#58;96 - resolving associations for &#91;de.wiol.infomat.data.DataType#3&#93;
      09&#58;08&#58;40,062 DEBUG CollectionLoadContext&#58;141 - creating collection wrapper&#58;&#91;de.wiol.infomat.data.DataType.documents#3&#93;
      09&#58;08&#58;40,062 DEBUG TwoPhaseLoad&#58;167 - done materializing entity &#91;de.wiol.infomat.data.DataType#3&#93;
      09&#58;08&#58;40,062 DEBUG TwoPhaseLoad&#58;96 - resolving associations for &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;40,062 DEBUG DefaultLoadEventListener&#58;143 - loading entity&#58; &#91;de.wiol.infomat.data.DataType#3&#93;
      09&#58;08&#58;40,062 DEBUG DefaultLoadEventListener&#58;208 - entity found in session cache
      09&#58;08&#58;40,077 DEBUG CollectionLoadContext&#58;141 - creating collection wrapper&#58;&#91;de.wiol.infomat.data.FullDocument.metaData#2&#93;
      09&#58;08&#58;40,077 DEBUG CollectionLoadContext&#58;141 - creating collection wrapper&#58;&#91;de.wiol.infomat.data.FullDocument.classAssignments#2&#93;
      09&#58;08&#58;40,077 DEBUG CollectionLoadContext&#58;141 - creating collection wrapper&#58;&#91;de.wiol.infomat.data.FullDocument.terms#2&#93;
      09&#58;08&#58;40,077 DEBUG TwoPhaseLoad&#58;167 - done materializing entity &#91;de.wiol.infomat.data.FullDocument#2&#93;
      09&#58;08&#58;40,077 DEBUG PersistenceContext&#58;789 - initializing non-lazy collections
      09&#58;08&#58;40,077 DEBUG Loader&#58;1368 - done entity load
      09&#58;08&#58;40,077 DEBUG JDBCContext&#58;322 - after autocommit
      09&#58;08&#58;40,077 DEBUG SessionImpl&#58;403 - after transaction completion
      09&#58;08&#58;40,077 DEBUG SessionImpl&#58;250 - opened session at timestamp&#58; 4582683935035392
      09&#58;08&#58;40,077 DEBUG DefaultDeleteEventListener&#58;61 - deleting a transient instance
      The mapping file of FullDocument:

      <?xml version="1.0"?>
      <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

      <hibernate-mapping>

      <class name="de.wiol.infomat.data.FullDocument" table="document" lazy="false">
      <id name="id" column="id" type="long" unsaved-value="null">
      <generator class="native"/>
      </id>
      <property name="name" column="name" type="string" length="255"/>
      <property name="path" column="path" type="string" length="255"/>
      <many-to-one name="dataType" foreign-key="fk_datatype" class="de.wiol.infomat.data.DataType" column="datatype_id" fetch="join" not-null="true" />
      <property name="extractionText" column="extracted_text" type="string" length="16000000" lazy="true"/>
      <property name="analysedText" column="analysed_text" type="string" length="16000000" lazy="true"/>
      <property name="extractionDate" column="extraction_date" type="timestamp"/>
      <property name="extractionSize" column="extraction_size" type="long" length="255"/>
      <bag name="metaData" table="metadata" cascade="all-delete-orphan" inverse="true" lazy="true">
      <key column="document_id"/>
      <one-to-many class="de.wiol.infomat.data.MetaData"/>
      </bag>
      <bag name="classAssignments" table="class_assignment" order-by="probability desc" cascade="all-delete-orphan" inverse="true" lazy="true">
      <key column="document_id"/>
      <one-to-many class="de.wiol.infomat.data.ClassAssignment"/>
      </bag>
      <bag name="terms" table="document_keywords" order-by="quantity desc" cascade="all-delete-orphan" inverse="true" lazy="true">
      <key column="document_id"/>
      <one-to-many class="de.wiol.infomat.data.Term"/>
      </bag>
      </class>

      </hibernate-mapping>

      Comment


      • #4
        When i merge the object from the first session to the second session, the delete seems to work for the session object, but there seems to be no commit, because there are no sql statements shown in the debug messages.

        So why is the session not bound to a transaction ?

        Thx for the help.

        12:09:59,178 DEBUG DefaultInitializeCollectionEventListener:61 - collection initialized
        12:09:59,178 DEBUG Cascades:67 - cascading to delete: de.wiol.infomat.data.Term
        12:09:59,179 DEBUG DefaultDeleteEventListener:91 - deleting a persistent instance
        12:09:59,180 DEBUG DefaultDeleteEventListener:126 - deleting [de.wiol.infomat.data.Term#5329]
        12:09:59,181 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,182 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,183 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,184 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,184 DEBUG Cascades:67 - cascading to delete: de.wiol.infomat.data.Term
        12:09:59,185 DEBUG DefaultDeleteEventListener:91 - deleting a persistent instance
        12:09:59,185 DEBUG DefaultDeleteEventListener:126 - deleting [de.wiol.infomat.data.Term#5330]
        12:09:59,186 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,186 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,187 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,188 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,188 DEBUG Cascades:67 - cascading to delete: de.wiol.infomat.data.Term
        12:09:59,189 DEBUG DefaultDeleteEventListener:91 - deleting a persistent instance
        12:09:59,190 DEBUG DefaultDeleteEventListener:126 - deleting [de.wiol.infomat.data.Term#5331]
        12:09:59,191 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,192 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,192 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,193 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,223 DEBUG Cascades:67 - cascading to delete: de.wiol.infomat.data.Term
        12:09:59,224 DEBUG DefaultDeleteEventListener:91 - deleting a persistent instance
        12:09:59,225 DEBUG DefaultDeleteEventListener:126 - deleting [de.wiol.infomat.data.Term#5332]
        12:09:59,225 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,226 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,226 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,227 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,228 DEBUG Cascades:67 - cascading to delete: de.wiol.infomat.data.Term
        12:09:59,229 DEBUG DefaultDeleteEventListener:91 - deleting a persistent instance
        12:09:59,230 DEBUG DefaultDeleteEventListener:126 - deleting [de.wiol.infomat.data.Term#5344]
        12:09:59,230 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,231 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,232 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,232 DEBUG SessionImpl:1071 - setting cache mode to: GET
        .
        .
        .
        12:09:59,433 DEBUG Cascades:908 - done cascade ACTION_DELETE for collection: de.wiol.infomat.data.FullDocument.terms
        12:09:59,433 DEBUG Cascades:919 - deleting orphans for collection: de.wiol.infomat.data.FullDocument.terms
        12:09:59,434 DEBUG Cascades:929 - done deleting orphans for collection: de.wiol.infomat.data.FullDocument.terms
        12:09:59,434 DEBUG Cascades:861 - done processing cascade ACTION_DELETE for: de.wiol.infomat.data.FullDocument
        12:09:59,435 DEBUG SessionImpl:1071 - setting cache mode to: NORMAL
        12:09:59,437 DEBUG SessionImpl:1071 - setting cache mode to: GET
        12:09:59,437 DEBUG Cascades:836 - processing cascade ACTION_DELETE for: de.wiol.infomat.data.FullDocument
        12:09:59,439 DEBUG Cascades:861 - done processing cascade ACTION_DELETE for: de.wiol.infomat.data.FullDocument
        12:09:59,440 DEBUG SessionImpl:1071 - setting cache mode to: NORMAL
        12:09:59,440 DEBUG SessionImpl:269 - closing session
        12:09:59,441 DEBUG ConnectionManager:317 - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
        12:09:59,444 DEBUG JDBCContext:283 - after transaction completion
        12:09:59,444 DEBUG SessionImpl:403 - after transaction completion

        Comment


        • #5
          Please see my post here and let me know if it answers your question:

          http://forum.springframework.org/showthread.php?t=15705

          I recommend having your data access objects extend org.springframework.orm.hibernate3.support.Hiberna teDaoSupport
          Last edited by robyn; May 16th, 2006, 05:05 AM.

          Comment

          Working...
          X