-->
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: Cascade delete with not null column
PostPosted: Wed Jan 19, 2005 12:45 pm 
Newbie

Joined: Wed Jan 19, 2005 11:10 am
Posts: 4
Location: Brazil
Hi,

I'm trying to do a cascade delete in a many-to-one relation using the native feature from Hibernate.
The feature was working fine before I set the foreign-key in child to not-null. After this, I had see the Hibernate is doing a update in child, setting the parent's foreign-key to null before do the delete.
I'd read the docs about inverse="true", and all cascade delete tags (all-delete-orphan, delete-orphan, delete).
I'd tried a lot of possible combinations such inverse="true, false", cascade="none, delete, all-delete..." but I always had the same problem.
I also read a lot of posts about this issue but no one helps me.
See some examples about this issue:
http://opensource.atlassian.com/projects/hibernate/browse/HBI-31
http://forum.hibernate.org/viewtopic.php?t=936931&highlight=cascade+delete+notnull+inverse
http://forum.hibernate.org/viewtopic.php?t=936983&highlight=cascade+delete+notnull+inverse

Have someone the sollution for this?


Hibernate version: 2.1.6

Mapping documents:
Code:
<hibernate-mapping>
      <class
          name="com.wplex.common.business.po.CityBO"
          table="CITY"
      >
          <id
              name="id"
              column="PID"
              type="java.lang.Long"
          >
              <generator class="native">
              </generator>
          </id>

        <property
            name="name"
            type="java.lang.String"
        >
            <column
                name="name"
                length="50"
                unique="true"
                not-null="true"
            />
        </property>

        <set
            name="quarters"
            lazy="true"
            inverse="true"
            cascade="delete-orphan"
            sort="unsorted"
        >
              <key
                  column="pid_city"
              >
              </key>

              <one-to-many
                  class="com.wplex.common.business.po.QuarterBO"
              />
        </set>
    </class>
</hibernate-mapping>


<hibernate-mapping>
    <class
        name="com.wplex.common.business.po.QuarterBO"
        table="QUARTER"
    >
        <id
            name="id"
            column="PID"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>
        <property
            name="name"
            type="java.lang.String"
        >
            <column
                name="name"
                length="50"
                unique-key="U_QRTER_NME_CITY"
                not-null="true"
            />
        </property>

        <many-to-one
            name="city"
            class="com.wplex.common.business.po.CityBO"
            cascade="delete"
            outer-join="auto"
        >
            <column
                name="pid_city"
                unique-key="U_QRTER_NME_CITY"
                not-null="true"
            />
        </many-to-one>
    </class>
</hibernate-mapping>


Some Code:
Code:
        //1st transaction, creating the city
        CityVO city = new CityVO();
        city.setName("Realeza"); //$NON-NLS-1$

        city = facade.createCity(city);

        //2nd transaction, creating the quarter of city
        QuarterVO quarter = new QuarterVO();
        quarter.setName("Agronômica"); //$NON-NLS-1$
        quarter.setCity(city);

        QuarterVO quarter = facade.createQuarter(quarter);

        assertNotNull(createdA.getId());

        //3rd transaction, creating the quarter of city
        QuarterVO quarter2 = new QuarterVO();
        quarter2.setName("João Paulo"); //$NON-NLS-1$
        quarter2.setCity(city);

        QuarterVO quarter2 = facade.createQuarter(quarter2);

//Finally trying to delete the city here the problem happens
Code:
            ...
            Session session = getSession();
            Transaction transaction = session.beginTransaction();
            CityBO city = (CityBO) session.get(clazz, id);
            transaction.commit();
            session.close();
            ...
            Session session = getSession();
            Transaction transaction = session.beginTransaction();
            session.delete(city); //here the exception
            transaction.commit();
            session.close();
            ...


Full stack trace of any exception that occurs:
12:51:19,724 WARN [JDBCExceptionReporter] SQL Error: 515, SQLState: HY000
12:51:19,724 ERROR [JDBCExceptionReporter] [Microsoft][SQLServer 2000 Driver for
JDBC][SQLServer]Cannot insert the value NULL into column 'pid_city', table 'wpl
excommons.dbo.QUARTER'; column does not allow nulls. UPDATE fails.
12:51:19,724 WARN [JDBCExceptionReporter] SQL Error: 3621, SQLState: HY000
12:51:19,724 ERROR [JDBCExceptionReporter] [Microsoft][SQLServer 2000 Driver for
JDBC][SQLServer]The statement has been terminated.
12:51:19,724 ERROR [JDBCExceptionReporter] could not delete collection: [com.wpl
ex.common.business.po.CityBO.quarters#3]
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Can
not insert the value NULL into column 'pid_city', table 'wplexcommons.dbo.QUARTE
R'; column does not allow nulls. UPDATE fails.

Name and version of the database you are using:
SqlServer2000

Database DDLs:
Code:
create table CITY (
   PID numeric(19,0) identity not null,
   name varchar(50) not null,
   primary key (PID),
   unique (name, state)
);
create table QUARTER (
   PID numeric(19,0) identity not null,
   name varchar(50) not null,
   pid_city numeric(19,0) not null,
   primary key (PID),
   unique (name, pid_city)
);
alter table QUARTER add constraint FK519F2E8CD4FFDF9F foreign key (pid_city) references CITY;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 19, 2005 6:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Read the documentation some more. Properly, this time.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 8:46 am 
Newbie

Joined: Wed Jan 19, 2005 11:10 am
Posts: 4
Location: Brazil
gavin wrote:
Read the documentation some more. Properly, this time.


Finally I found the origin of problem, and was not mine!

The Xdoclet is not generating the tag inverse properly:
http://opensource.atlassian.com/projects/xdoclet/browse/XDT-1166


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 9:48 am 
Newbie

Joined: Fri Aug 13, 2004 5:08 am
Posts: 3
Hi,

I am experiencing the same problem, also using XDoclet 1.2.2. How did you solve this? Are you setting the hibernate version to 2.0, or does that break anything else?

To manually edit the hbm-files is not an option!

Regards
Niklas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 2:15 pm 
Newbie

Joined: Wed Jan 19, 2005 11:10 am
Posts: 4
Location: Brazil
grunbaum wrote:
Hi,

I am experiencing the same problem, also using XDoclet 1.2.2. How did you solve this? Are you setting the hibernate version to 2.0, or does that break anything else?

To manually edit the hbm-files is not an option!

Regards
Niklas


Yes, changing to 2.0 solve this and a lot of problems, for example, the constraints names.
You also can change the content of the XDT file to solve the problem and keep using 2.1 flag.

Regards,

Mauro.


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.