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.  [ 9 posts ] 
Author Message
 Post subject: Regarding Recursive Parent Child Relationship
PostPosted: Mon Mar 21, 2005 9:21 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
Basic idea is to have Categories within Categories within Categories. Sub Categories can have only one parent Category. A parent Category can have multiple sub categories. The relationship needs to be bi directional. Below is the Category.hbm.xml and Category.java files. I am getting an exception when I am trying to create and insert a category like this:

Code:
            Session session = getSession();
            Transaction tx = session.beginTransaction();
           
            session.save(cat);
            session.flush();
           
            tx.commit();


Hibernate version: 3.0

Mapping documents:
Code:
<?xml version="1.0"?>
   <!DOCTYPE hibernate-mapping PUBLIC
           "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
           "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
<hibernate-mapping>
    <class name="com.am.t.beans.Category" table="category">
        <id name="id" unsaved-value="0">
                <generator class="native"/>
        </id>
       
        <property name="name" type="string"/>
        <property name="description" type="string"/>       

      <many-to-one name="parentId" class="com.am.t.beans.Category"
           column="parentId"
           not-null="true"/>

       <set name="subCategories" cascade="all" lazy="true" inverse="true">
           <key column="parentId"/>
           <one-to-many class="com.am.t.beans.Category"/>
      </set>     
   </class>
</hibernate-mapping>


Name and version of the database you are using:
MySQL 4.1.3

Debug level Hibernate log excerpt:
Code:
DEBUG - closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.am.t.beans.Category.id
   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:119)
   at org.hibernate.tuple.AbstractTuplizer.getIdentifier(AbstractTuplizer.java:103)
   at org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:2904)
   at org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2681)
   at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:175)
   at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
   at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
   at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:232)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)
   at com.am.t.dao.DSCategoryDAO.insertCategory(DSCategoryDAO.java:82)
   at com.am.t.beans.TestCategrory.testInsertGroup(TestCategrory.java:36)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:105)
   ... 34 more


Category.java:
Code:
public class Category
{
    private Long id;
    private Long parentId;
    private String name;
    private String description;
    private Set subCategories;
   
    public Category()
    {       
    }
   
    public Category(Long parentId, String name, String description)
    {
        this.parentId = parentId;
        this.name = name;
        this.description = description;
    }

    public Long getId()
    {
        return id;
    }
   
    private void setId(Long id)
    {
        this.id = id;
    }

    public String getDescription()
    {
        return description;
    }
   
    public void setDescription(String description)
    {
        this.description = description;
    }
   
    public String getName()
    {
        return name;
    }
   
    public void setName(String name)
    {
        this.name = name;
    }
   
    public Long getParentId()
    {
        return parentId;
    }
   
    public void setParentId(Long parentId)
    {
        this.parentId = parentId;
    }
   
    public Set getSubCategories()
    {
        return subCategories;
    }
   
    public void setSubCategories(Set subCategories)
    {
        this.subCategories = subCategories;
    }
}


Does anyone have an idea whats going wrong? [Note: I have tried to find a solution to the problem by searching this forum and reading the documentation, but maybe I am missing something basic.]

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 22, 2005 11:31 am 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
I think that the parentId must be a Category and not a Long "type".

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 22, 2005 12:23 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
The examples talk with the parent being a Category and not a Long. Yes, I was wondering if it can be done using a Long and also whats the best way to do it? I believe it would depend on the business logic of the application. Can someone shed light on this?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 10:20 am 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Ok, now I understood :-)

Well, if you want only always the attribute parentId, so you can
use the parentId like a property and not like many-to-one.
Look:

Code:
<hibernate-mapping>
    <class name="com.am.t.beans.Category" table="category">
        <id name="id" unsaved-value="0">
                <generator class="native"/>
        </id>
       
        <property name="name" type="string"/>
        <property name="description" type="string"/>       

        <property  name="parentId" column="parentId" not-null="true"/>

       <set name="subCategories" cascade="all" lazy="true" inverse="true">
           <key column="parentId"/>
           <one-to-many class="com.am.t.beans.Category"/>
      </set>     
    </class>
</hibernate-mapping>



But I don´t know in this case about the collection subCategories.

Well, I would prefer use the object Category (called parent).

[]´s

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 11:24 am 
Newbie

Joined: Tue Mar 22, 2005 5:43 pm
Posts: 16
Location: Teksouth
I am using the similar case and it works. The definitions are little different. In my case I have defined subcategories as bag and not the set. lazy="false". In my table definition parent_id is NULL.
bag looks like this.
<bag name="subcategories" table="category" lazy="false" inverse="true" cascade="none">
<key column="parent_id"/>
<one-to-many class="com.am.t.beans.Category"/>
</bag>

In Category class bag is defined as a simple arraylist

Private List subcategories = new ArrayList();


Top
 Profile  
 
 Post subject: deleting a parent category throws exception.
PostPosted: Wed Mar 23, 2005 2:01 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
I finally settled on having the parent as a Category as opposed to a long and got the association working.

The problem now is that I am trying to delete a parent category and I want the result of that delete to delete the parent and the children categories of that parent. The problem is that my delete throws an exception since its failing a foreign key constraint.

This is the code that I am using to delete a category:
Code:
            Session session = getSession();
            Transaction tx = session.beginTransaction();
           
            session.delete(parentCategory);
            session.flush();
           
            tx.commit();


My Category.hbm.xml file now looks like:

Code:
<?xml version="1.0"?>
   <!DOCTYPE hibernate-mapping PUBLIC
           "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
           "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
<hibernate-mapping>
    <class name="com.am.t.beans.Category" table="category">
        <id name="id" unsaved-value="0">
                <generator class="native"/>
        </id>

        <property name="name" type="string"/>
        <property name="description" type="string"/>       

       <set name="subCategories" cascade="all" lazy="true" inverse="true">
           <key column="parentId"/>
           <one-to-many class="com.am.t.beans.Category"/>
      </set>     

      <many-to-one name="parent" class="com.am.t.beans.Category"
           column="parentId" cascade="all"/>
   </class>
</hibernate-mapping>


The problem is that when I try to delete it throws the following exception:

Code:
java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails
   at
com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:828)
   at com.mchange.v2.sql.filter.FilterPreparedStatement.executeBatch(FilterPreparedStatement.java:260)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
   at com.am.t.dao.DSCategoryDAO.deleteCategory(DSCategoryDAO.java:127)
   at com.am.t.beans.TestCategrory.testInsertGroup(TestCategrory.java:60)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
WARN  - SQL Error: 1217, SQLState: 23000
ERROR - Cannot delete or update a parent row: a foreign key constraint fails
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
   at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:161)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
   at com.am.t.dao.DSCategoryDAO.deleteCategory(DSCategoryDAO.java:127)
   at com.am.t.beans.TestCategrory.testInsertGroup(TestCategrory.java:60)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:828)
   at com.mchange.v2.sql.filter.FilterPreparedStatement.executeBatch(FilterPreparedStatement.java:260)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
   ... 22 more
ERROR - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
   at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:161)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
   at com.am.t.dao.DSCategoryDAO.deleteCategory(DSCategoryDAO.java:127)
   at com.am.t.beans.TestCategrory.testInsertGroup(TestCategrory.java:60)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:828)
   at com.mchange.v2.sql.filter.FilterPreparedStatement.executeBatch(FilterPreparedStatement.java:260)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
   ... 22 more


Top
 Profile  
 
 Post subject: exception while deleting.
PostPosted: Wed Mar 23, 2005 2:33 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
I changed my Category.hbm.xml file. I set cascade="all" in the <many-to-one ... element. I get a new exception. I am still trying to delete the parent category and delete all the child categories when I delete the parent. Can someone shed light on what I am doing wrong?

Thanks.

Code:
<?xml version="1.0"?>
   <!DOCTYPE hibernate-mapping PUBLIC
           "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
           "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
<hibernate-mapping>
    <class name="com.am.t.beans.Category" table="category">
        <id name="id" unsaved-value="0">
                <generator class="native"/>
        </id>

        <property name="name" type="string"/>
        <property name="description" type="string"/>       

       <set name="subCategories" lazy="true" inverse="true">
           <key column="parentId"/>
           <one-to-many class="com.am.t.beans.Category"/>
      </set>     

      <many-to-one name="parent" class="com.am.t.beans.Category"
           column="parentId" cascade="all"/>
   </class>
</hibernate-mapping>


Code:
DEBUG - begin
DEBUG - current autocommit status: false
DEBUG - deleting a persistent instance
DEBUG - deleting [com.am.t.beans.Category#1]
DEBUG - setting cache mode to: GET
DEBUG - processing cascade ACTION_DELETE for: com.am.t.beans.Category
DEBUG - done processing cascade ACTION_DELETE for: com.am.t.beans.Category
DEBUG - setting cache mode to: NORMAL
DEBUG - setting cache mode to: GET
DEBUG - processing cascade ACTION_DELETE for: com.am.t.beans.Category
DEBUG - cascading to delete: com.am.t.beans.Category
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.am.t.beans.Category#1]
   at org.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:681)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:166)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:416)
   at org.hibernate.engine.Cascades$5.cascade(Cascades.java:153)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:721)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:817)
   at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:121)
   at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:112)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:59)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
   at com.am.t.dao.DSCategoryDAO.deleteCategory(DSCategoryDAO.java:127)
   at com.am.t.beans.TestCategrory.testInsertGroup(TestCategrory.java:60)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
DEBUG - deleting a persistent instance
DEBUG - object was already deleted
DEBUG - done processing cascade ACTION_DELETE for: com.am.t.beans.Category
DEBUG - setting cache mode to: NORMAL
DEBUG - flushing session
DEBUG - processing flush-time cascades
DEBUG - processing cascade ACTION_SAVE_UPDATE for: com.am.t.beans.Category
DEBUG - cascading to saveOrUpdate: com.am.t.beans.Category
DEBUG - deleted instance of: com.am.t.beans.Category
DEBUG - saving transient instance
DEBUG - flushing to force deletion of re-saved object: [com.am.t.beans.Category#1]
DEBUG - closing session
DEBUG - closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 2:47 pm 
Newbie

Joined: Tue Mar 22, 2005 5:43 pm
Posts: 16
Location: Teksouth
Change

<many-to-one name="parent" class="com.am.t.beans.Category"
column="parentId" cascade="none"/>


Top
 Profile  
 
 Post subject: solved the delete problem by doing the following
PostPosted: Wed Mar 23, 2005 3:33 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
I changed my mapping to be this:

Code:
<?xml version="1.0"?>
   <!DOCTYPE hibernate-mapping PUBLIC
           "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
           "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
<hibernate-mapping>
    <class name="com.am.t.beans.Category" table="category">
        <id name="id" unsaved-value="0">
                <generator class="native"/>
        </id>

        <property name="name" type="string"/>
        <property name="description" type="string"/>       

       <set name="subCategories" lazy="true" inverse="true" cascade="all">
           <key column="parentId"/>
           <one-to-many class="com.am.t.beans.Category"/>
      </set>     

      <many-to-one name="parent" class="com.am.t.beans.Category"
           column="parentId"/>
   </class>
</hibernate-mapping>


and in my code i explicitly remove the category to be deleted from any collection it is in as a child and then use my delete code on that category. Doing this allows me to delete.


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