Hibernate Version: 3.2
When a composite-element is removed from a set, the relationship many-to-one inside the element is not deleted. This is a bug?
Test Case:
Mapping:
Code:
<hibernate-mapping>
<class name="Student">
<id name="id">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
<set name="addresses" cascade="all-delete-orphan">
<key column="id_student" not-null="true"/>
<composite-element class="Address">
<property name="street" not-null="true"/>
<property name="city" not-null="true"/>
<property name="state" not-null="true"/>
<property name="zip" not-null="true"/>
<many-to-one name="attach" column="id_attach" unique="true" cascade="all,delete"/>
</composite-element>
</set>
</class>
<class name="Attach">
<id name="id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
Entities:
Code:
public class Student implements Serializable {
private Integer id;
private String name;
private Set<Address> addresses;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
}
public class Address implements Serializable {
private Integer id;
private String street;
private String city;
private String state;
private String zip;
private Attach attach;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Attach getAttach() {
return attach;
}
public void setAttach(Attach attach) {
this.attach = attach;
}
}
public class Attach implements Serializable {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
Test Code:
Code:
/* Insert the entity Student. */
session.beginTransaction();
Student student = new Student();
student.setName("Rafael Santini");
student.setAddresses(new HashSet<Address>());
Address address = new Address();
address.setStreet("Avenida das Nações Unidas");
address.setCity("Capivari de Baixo");
address.setState("Santa Catarina");
address.setZip("88745-000");
address.setAttach(new Attach());
student.getAddresses().add(address);
session.save(student);
session.getTransaction().commit();
/* Remove all addresses. */
session.beginTransaction();
for (Object obj : session.createQuery("from Student").list()) {
Student student = (Student) obj;
student.getAddresses().clear();
session.saveOrUpdate(obj);
}
session.getTransaction().commit();
Result:
The database records of attach table are not deleted.