-->
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.  [ 1 post ] 
Author Message
 Post subject: Bug? many-to-one inside composite-element is not deleted
PostPosted: Wed Nov 18, 2009 4:18 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.