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 happensCode:
...
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;