File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes removing a child Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "removing a child" Watch "removing a child" New topic
Author

removing a child

Sudheer Palaparambil
Greenhorn

Joined: Sep 10, 2008
Posts: 2
Hello Friends,

I am a Hibernate newbie. I am using Hibernate 3. I have a one-to-many ORM, code given below

Please note that there is no cascade effect. Now I add some data and persist it

Person john = new Person();
john.setFirstName("John");
john.setLastName("Smith");

Dog rover = new Dog();
rover.setName("Rover");
john.addPet(rover);
rover.setFriend(john);

Dog jimmy = new Dog();
jimmy.setName("Jimmy");
john.addPet(jimmy);
rover.setFriend(john);

session.save(john);

Now How do I remove jimmy from the collection and update its parent?
Do we need a Iterator here?

public class Dog
{
private long id;
private String name;
private Person friend;
........
........
public Person getFriend() { return friend; }
public void setFriend(Person newFriend) { friend = newFriend; }
}

Dog.hbm.xml
------------
<hibernate-mapping>
<class name="com.agiledeveloper.Dog" table="dog">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="native" />
</id>
<property name="name" column="name" type="string"/>
<many-to-one name="friend"class="com.agiledeveloper.Person" column="person_id" />
</class>
</hibernate-mapping>

public class Person
{
private long id;
private String firstName;
private String lastName;
private Set pets = new HashSet();
........
........
public Set getPets()
{
return pets;
}

public void setPets(Set thePets)
{
pets = thePets;
}
public void addPet(Dog aPet)
{
if (!pets.contains(aPet))
{
pets.add(aPet);
}
}
public void removePet(Dog aDog)
{
pets.remove(aDog);
}
}

Person.hbm.xml
---------------
<hibernate-mapping>
<class name="com.agiledeveloper.Person" table="person">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="native" />
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<set name="pets" inverse="true" lazy="true">
<key column="person_id"/>
<one-to-many class="com.agiledeveloper.Dog"/>
</set>
</class>
</hibernate-mapping>

Thank you.

Sudheer Palaparambil
Ramesh Srinivasan
Ranch Hand

Joined: Aug 17, 2008
Posts: 35
Person.hbm.xml
---------------
<hibernate-mapping>
<class name="com.agiledeveloper.Person" table="person">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="native" />
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<set name="pets" inverse="true" lazy="true">
<key column="person_id"/>
<one-to-many class="com.agiledeveloper.Dog"/>
</set>
</class>
</hibernate-mapping>

Thank you.

Sudheer Palaparambil[/QB]

Hi,

I believe you need to use cascade to delete the orphan has given below

cascade="all-delete-orphan" on your set.

One more thing you have to iterate your child elements to be deleted and explicitly you have say delete.

Have look at the below hibernate references..
http://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html


SCJP 5.0 93%<br /> <br />"We are what we repeatedly do , then excellence is not an action it is an Habit" Aristotle
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: removing a child