Hy, guys! I got the following problem:when I try to do a delete on a table Telefones, which is linked with a endereco table using a one-to-many association, where the telefones table is the many side, and the endereco table is linked with a contact table in a one-to-one association , and instead of deleting the row on telefones, he try's to update the row making the foreign key column null!here's the mapping of the two tables and the code I tried to execute:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Telefone" table="TELEFONES">
<id name="id" type="long" column="ID" >
<generator class="increment" />
</id>
<property name="telefone">
<column name="TELEFONE"/>
</property>
<property name="contact">
<column name="CONTACT"/>
</property>
<one-to-one name="Endereco" class="roseindia.tutorial.hibernate.Endereco" cascade="all" />
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Endereco" table="ENDERECO">
<id name="id" type="long" column="CONTACT" >
<generator class="foreign" >
<param name="property">Contact</param>
</generator>
</id>
<property name="rua">
<column name="RUA"/>
</property>
<property name="bairro">
<column name="BAIRRO"/>
</property>
<property name="complemento">
<column name="COMPLEMENTO"/>
</property>
<property name="numero">
<column name="NUMERO"/>
</property>
<one-to-one name="Contact" class="roseindia.tutorial.hibernate.Contact" cascade="all" />
<set name="telefones" cascade="all" >
<key column="CONTACT" />
<one-to-many class="roseindia.tutorial.hibernate.Telefone" />
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="increment"/>
</id>
<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
<one-to-one name="Endereco"
class="roseindia.tutorial.hibernate.Endereco"
cascade="all" />
</class>
</hibernate-mapping>
and here it is the code:
Code:
Contact contact3 = (Contact) session.get(Contact.class,new Long(1));
Query query = session.createQuery("select tel from roseindia.tutorial.hibernate.Telefone as tel where tel.contact = ?");
query.setLong(0,contact3.getId());
String q = query.getQueryString();
Transaction t4 = session.beginTransaction();
Iterator it = query.list().iterator();
while (it.hasNext()) {
Telefone t5 = (Telefone) it.next();
session.delete(t5);
}
t4.commit();