-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-many cascading delete problem
PostPosted: Thu Aug 19, 2004 4:37 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
Hi,

I have a many-to-many relationship set up. Two objects are involved. A "Category" contains many "Terms". "Terms" may belong to many "Categories".

When I delete a category I want it to cascade and delete the term if and only if it does not belong to another category.

Is there a way to do this? What I have now causes the term to be deleted even if it belongs to another Category.

Thanks for any advice you can give me.

Don


Hibernate version: 2.1.4

Mapping documents:

Code:
<?xml version="1.0"?>

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

<!-- Copyright (c) 2004 Attensity Corporation, All Rights Reserved. -->

<hibernate-mapping>

  <!-- Category Table (class Category) -->
  <class name="com.attensity.powerdrill.dao.beans.Category"
         table="Categories">

    <meta attribute="class-description">
      A category bean.  A category can contain multiple terms.
      @author Don Mitchell
    </meta>

    <meta attribute="implement-equals">true</meta>

    <id name="id" column="CategoryID" type="long" unsaved-value="null">
      <meta attribute="use-in-tostring">true</meta>
      <meta attribute="use-in-equals">true</meta>
      <generator class="native"/>
    </id>

    <property name="name" type="string">
      <meta attribute="use-in-tostring">true</meta>
      <meta attribute="use-in-equals">true</meta>
      <column name="Name" unique-key="NameType" length="128" not-null="true"/>
    </property>

    <property name="type" type="character">
      <meta attribute="use-in-tostring">true</meta>
      <meta attribute="use-in-equals">true</meta>
      <column name="CategoryType" unique-key="NameType" not-null="true"/>
    </property>

    <property name="lastModDate" column="LastModDate"
              type="timestamp" not-null="true"/>

    <!-- Reference back to self for parent category. -->
    <many-to-one name="parent" column="ParentID"
        class="com.attensity.powerdrill.dao.beans.Category"/>

    <!-- CategoryTerms Table -->
    <set name="terms" table="CategoryTerms"
         cascade="all" lazy="false" inverse="false">
      <meta attribute="field-description">Terms within this category</meta>
      <key column="CategoryID" foreign-key="TermID"/>
      <many-to-many
        class="com.attensity.powerdrill.dao.beans.CategoryTerm">
        <column name="TermID" unique-key="CategoryTerm"/>
      </many-to-many>
    </set>

  </class>

  <!-- Terms Table (class CategoryTerm) -->
  <class name="com.attensity.powerdrill.dao.beans.CategoryTerm"
         table="Terms">

    <meta attribute="class-description">
      A category term bean.  A term may belong to multiple categories.
      @author Don Mitchell
    </meta>

    <meta attribute="implement-equals">true</meta>

    <id name="id" column="TermID" type="long" unsaved-value="null">
      <meta attribute="use-in-tostring">true</meta>
      <meta attribute="use-in-equals">true</meta>
      <generator class="native"/>
    </id>

    <property name="term" type="string">
      <meta attribute="use-in-tostring">true</meta>
      <meta attribute="use-in-equals">true</meta>
      <column name="Term" unique-key="Term" length="255" not-null="true"/>
    </property>

    <property name="lastModDate" column="LastModDate" type="timestamp"
              not-null="true">
      <meta attribute="use-in-tostring">true</meta>
    </property>

    <set name="categories" lazy="false" table="CategoryTerms" inverse="true">
      <key column="TermID" foreign-key="CategoryID"/>
      <many-to-many
        class="com.attensity.powerdrill.dao.beans.Category">
        <column name="CategoryID" unique-key="CategoryTerm"/>
      </many-to-many>
    </set>

  </class>

</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Code:
Caused by: net.sf.hibernate.HibernateException: Batch update row count wrong: 0
        at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.
java:65)
        at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
        at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:5
9)
        at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:5
6)
        at net.sf.hibernate.impl.BatcherImpl.prepareBatchStatement(BatcherImpl.j
ava:109)
        at net.sf.hibernate.persister.EntityPersister.delete(EntityPersister.jav
a:582)
        at net.sf.hibernate.impl.ScheduledDeletion.execute(ScheduledDeletion.jav
a:29)
        at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
        at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
        at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
        at com.attensity.apps.hibernate.ejb.HibernateDAO.removeObj(HibernateDAO.
java:70)
        ... 14 more


Name and version of the database you are using:

MySQL 4.0.12-nt

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 20, 2004 12:28 pm 
Newbie

Joined: Wed Aug 18, 2004 8:44 am
Posts: 12
can you post your classes please?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 12:26 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
Here are my tables. Note that I'm missing the Innodb setting and I believe I want that. I'm using MySQL 4.0.20. If I was writing these create scripts by hand I would tak on a FOREIGN KEY with "ON DELETE CASCADE", but I don't know how to get Hibernate to do that.

Thanks for you help!
Don

Code:
create table Categories (
   CategoryID BIGINT NOT NULL AUTO_INCREMENT,
   Name VARCHAR(128) not null,
   CategoryType CHAR(1) not null,
   LastModDate DATETIME not null,
   ParentID BIGINT,
   primary key (CategoryID),
    unique (Name, CategoryType)
);
create table CategoryTerms (
   CategoryID BIGINT not null,
   TermID BIGINT not null,
   primary key (TermID, CategoryID),
    unique (TermID, CategoryID)
);
create table Terms (
   TermID BIGINT NOT NULL AUTO_INCREMENT,
   Term VARCHAR(255) not null,
   LastModDate DATETIME not null,
   primary key (TermID),
    unique (Term)
);
alter table Categories add index FKC419223C49E5F365 (ParentID), add constraint FKC419223C49E5F365 foreign key (ParentID) references Categories (CategoryID);
alter table CategoryTerms add index FK43303B09951BCEA7 (TermID), add constraint FK43303B09951BCEA7 foreign key (TermID) references Terms (TermID);
alter table CategoryTerms add index FK43303B09C4195AB9 (CategoryID), add constraint FK43303B09C4195AB9 foreign key (CategoryID) references Categories (CategoryID);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 1:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate2 does not use database-level ON DELETE CASCADE constraints. HIbernate3 does.

But AFAIK, nether ON CASCADE DELETE, nor Hibernate's cascade="delete" can be made to handle the kind of delete you are talking about. That's something different, which must be done at the application level.


Top
 Profile  
 
 Post subject: Hibernate addToXXX lazy initialization problem in 3 tier app
PostPosted: Mon Aug 23, 2004 1:14 am 
Beginner
Beginner

Joined: Sun Jun 20, 2004 11:39 pm
Posts: 24
Hi guys,
I am developing a 3 tier app (struts, ejb, hibernate). now the problem is that i have a parent - child relationship where i retrieve the parent object in the ejb and transfer that object to struts, now when i am in struts, i create many child objects and want to add to these child objects to the parent object so i call the parent.addTo(child) but i got a lazy initialization exception. what i had planned to do is to add the children to the parent and only transfer the parent only back to the ejb to update and save the parent and the children into the database. can anyone tell me is there anyway to tell hibernate that the parent is a detached object and not try to do anything to it? Thanks to anyone in advance who can help me out on this one!!


Best regards


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