Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

removing a child

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The only thing that kept the leeches off of me was this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic