-->
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: CGlib in parent/child delete operation
PostPosted: Sun Jan 21, 2007 1:28 pm 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2.1GA

Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>

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

<hibernate-mapping
>
    <class
        name="gr.eulife.eucatalogue.base.CategoryImpl"
        table="categories"
    >
        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="increment">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-BaseCategory.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <bag
            name="categories"
            lazy="true"
            inverse="true"
            cascade="save-update"           
        >

            <key
                column="parent"
            >
            </key>

            <one-to-many
                  class="gr.eulife.eucatalogue.base.CategoryImpl"
            />

      </bag>

        <bag
            name="fields"
            table="categories_fields"
            lazy="true"
            cascade="none"
        >

            <key
                column="category"
            >
            </key>

            <many-to-many
                class="gr.eulife.eucatalogue.base.FieldImpl"
                column="field"
                outer-join="auto"
             />

      </bag>

        <many-to-one
            name="parent"
            class="gr.eulife.eucatalogue.base.CategoryImpl"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="parent"
            not-null="false"
        />

        <bag
            name="localizedViews"
            lazy="true"
            inverse="true"
            cascade="all"
        >

            <key
                column="category"
            >
            </key>

            <one-to-many
                  class="gr.eulife.eucatalogue.i18n.CategoryLocalization"
            />

      </bag>

        <property
            name="code"
            type="java.lang.String"
            update="true"
            insert="true"
            column="code"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <bag
            name="defaultFields"
            table="default_fields"
            lazy="true"
            cascade="none"
        >

            <key
                column="category"
            >
            </key>

            <many-to-many
                class="gr.eulife.eucatalogue.base.FieldImpl"
                column="field"
                outer-join="auto"
             />

      </bag>

        <bag
            name="items"
            table="items_categories"
            lazy="true"
         cascade="none"
        >

            <key
                column="category"               
            >
            </key>

            <many-to-many
                class="gr.eulife.eucatalogue.base.ItemImpl"
                column="item"
                outer-join="auto"
             />

      </bag>
     
      <joined-subclass name="gr.eulife.eucatalogue.base.CatalogueCategoryImpl" table="catalogue_categories" >
     
         <key column="category" />
         
        <property
            name="catalogueName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="catalogue_name"
        />
       
      </joined-subclass>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-BaseCategory.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
tx = session.beginTransaction();           
Category category = session.load(CatalogueCategoryImpl.class, id);
category.setParent(null);
category.clearCategories();
session.delete(category);
session.flush(category); //exception occurs here
tx.commit();


CategoryImpl#setParent(Category parent)
Code:
       if(this.parent != null)
          this.getParent().removeCategory(this);
       
        this.parent = parent;


CategoryImpl#clearCategories()
Code:
        for(Iterator iter = getCategories().iterator(); iter.hasNext();)
        {
           Category child = (Category)iter.next();
           child.setParent(null);
        }


Full stack trace of any exception that occurs:
Code:

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [gr.eulife.eucatalogue.base.CatalogueCategoryImpl#4]
   at org.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:1014)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:168)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
   at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
   at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
   at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at gr.eulife.eupersistence.hibernate.ThreadLocalSession.flushSession(ThreadLocalSession.java:85)
   at gr.eulife.eupersistence.hibernate.HiberDatabase.flushSession(HiberDatabase.java:100)
   at gr.eulife.eupersistence.hibernate.HiberClient.flush(HiberClient.java:118)
   at gr.eulife.eucatalogue.base.Catalogue.delete(Catalogue.java:285)
   at gr.eulife.eucatalogue.base.Catalogue.deleteCategory(Catalogue.java:295)
   at gr.eulife.eucatalogue.web.admin.actions.DeleteCategory.execute(DeleteCategory.java:51)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at gr.eulife.eupersistence.filters.SimplePersistenceFilter.doFilter(SimplePersistenceFilter.java:64)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at gr.eulife.euweb.filters.EncodingFilter.doFilter(EncodingFilter.java:82)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:613)


Name and version of the database you are using:
PostgreSQL, version: 8.1.5


Hello there. This thing has been bothering me for a couple of hours now.

Running it as a JUnit test, works like a charm.
Moving it over to a Struts application it's broken.

Trying to trace it down I finally found out the following. When running it through Struts for some reason (I'm sill not able to trace the why), the object returned by Session#load(Object) shows up as CatalogueCategoryImpl$$EnhancerByCGLIB etc. while in the JUnit as plain CatalogueCategoryImpl.

I debugged it further and show that when calling
Code:
category.setParent(null)

Category#getParent() returns null (altough gr.eulife.eucatalogue.base.CatalogueCategoryImpl#4 has an associated parent) thus the category isn't removed as a child, hence the exception.

Any help much appreciated.

PS. As a side question, considering the above example where parent holds the state of the link ("inverse"), if I remove a child for the collection, do I also have to set explicitly the parent to null?

_________________
eu:life
http://www.eulife.gr


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 4:01 pm 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
Ok here is some feedback on this in case someone else has problems.

I found out two things while looking at the docs/forums/wiki (plenty of places to look for answers I know, its tough)

It seems that the implementation of #equals(Object) in CategoryImpl had the following problems.

First,
instanceof won't work with proxies unless you have explicitly used the "proxy" attribute in the "class" mapping to the interface or inherited class. You can also use #getClass() which will work.

Second,
make sure you use the getter of the property since it might have not be initialized.

It seems to be working fine now.

_________________
eu:life
http://www.eulife.gr


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.