-->
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.  [ 4 posts ] 
Author Message
 Post subject: many to one association
PostPosted: Tue Jun 15, 2004 6:01 pm 
Newbie

Joined: Tue Jun 15, 2004 5:22 am
Posts: 2
I'm beginner with hibernate (Hibernate 2.1.4 ) and i have a mapping problem (perhaps more a conception problem).

This is a simple example:
I have 2 beans : Boat and Port

public class Boat{

private String ID;
private Port portofdeparture;

...setters and getters

}

public class Port{

private String ID;

...setter and getter

}

my hibernate mapping is :

<hibernate-mapping>
<class name="com.sea.Boat" table="boat">

<id name="ID" column="ID" type="long" unsaved-value="0">

<many-to-one name="portofdeparture" class="com.sea.Port"
column="portofdepartureID">
</class>

<class name="com.sea.Port" table="boat">

<id name="ID" column="ID" type="long" unsaved-value="0">

</class>

Now, I create 2 ports and 1 boat

Port p1 = new Port();
p1.setID("Le Havre");
Port p2 = new Port();
p2.setID("Amsterdam");

session.save(p1);
session.save(p2);

I save this 2 ports in the table port

Now I create a Boat :
Boat b1=new Boat();
b1.setID("Titanic");
b1.setPortofdeparture("Le Havre");
session.save(b1);

I save this boat in the table boat

Now i decide to delete the port Le Havre

If i try to get the Titanic i get one exception who tell me that the reference to Le havre doesn't exist. Thi is logical because i have deleted it.

So what is the best practice for deleting the port and the reference to the port in the boat. A sort of bidirectionnal association.

I hope this question is not too stupid.

Thanx for you help.

PS: I take this example to simplify the problem, it is not implemented so excuse me if there are code mistakes.


Top
 Profile  
 
 Post subject: play around with cascading
PostPosted: Wed Jun 16, 2004 9:28 am 
Newbie

Joined: Tue Jun 15, 2004 9:31 am
Posts: 4
Location: Frankfurt
If you want to delete child relations by just deleting the parent, you should play around with cascading (http://www.hibernate.org/hib_docs/refer ... -manytoone).

Unfortunately in my own case it doesn't work, but theoretically it should. Anyway, if you read a little more about the cascade attribute you are on the right track - I promise;-)


Good luck, Dirk


Top
 Profile  
 
 Post subject: delete the child
PostPosted: Wed Jun 16, 2004 10:49 am 
Newbie

Joined: Tue Jun 15, 2004 5:22 am
Posts: 2
Thanx Dirk for your response (you're the only one!)

The problem is i want to delete the child and not the parent, so the child has no reference to his parent in the mapping. In this case i can't specify a cascade in the port mapping.

I'm sure this is a concept problem...


Top
 Profile  
 
 Post subject: conceptual problem
PostPosted: Wed Jun 16, 2004 11:15 am 
Newbie

Joined: Tue Jun 15, 2004 9:31 am
Posts: 4
Location: Frankfurt
Salut Francois,

I am a little bit confused: 1st you wrote you want to delete a Port (the Parent), then you wrote you want to delete the Boat (the Child).

If you want to delete a Boat (the Child) you can simply delete it with the following Java-Code (I got my own session manager, so ignore the session code-lines and open/close a session your own way)...

++++++++++++++++++++++++
public void removeChild(Long theChildId){
try {
Session session = SessionManager.currentSession();
Transaction tx = session.beginTransaction();
Child theChild = (Child)session.load(Child.class, theChildId);
session.delete(theChild);
tx.commit();
SessionManager.closeSession();
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
}
}
+++++++++++++++++++++++++

If you want to delete a Port including all refering childs, then have a look at the following code including the 2 example mapping files...

----- my mapping-file for the parent -----

<hibernate-mapping>
<class name="example.Parent" table="PARENT">
<id name="parentId" column="parent_id" type="long">
<generator class="increment"/>
</id>
<property name="parentName" column="parent_name" type="java.lang.String"/>
<set name="childs" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="parentId"/>
<one-to-many class="example.Child"/>
</set>
</class>
</hibernate-mapping>


----- my mapping-file for the child -----

<hibernate-mapping>
<class name="example.Child" table="CHILD">
<id name="childId" column="child_id" type="long">
<generator class="increment"/>
</id>
<property name="childName" column="child_name" type="java.lang.String"/>
<many-to-one name="parent" class="example.Parent" column="parentId"/>
</class>
</hibernate-mapping>

----- the java-code to delete the parent including all refering childs -----

public void removeParent(Long theParentId){
try {
Session session = SessionManager.currentSession();
Transaction tx = session.beginTransaction();
Parent theParent = (Parent) session.load(Parent.class, theParentId);
session.delete(theParent);
tx.commit();
SessionManager.closeSession();
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
}
}


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

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.