-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem: Hibernate mapping one-to-many many-to-one
PostPosted: Wed Oct 11, 2006 8:51 pm 
Beginner
Beginner

Joined: Tue Aug 01, 2006 4:57 am
Posts: 25
Hibernate version:3.1

MySQL 5.0.28

Hi I need help with relation mapping

I dont know what to do and I could not find enough examples for my situation. The hibernate documentation seems hard to me right now. So somebody please help me with this.

I have two java classes Sim(parent) and Message(child). The relationship between Sim and Message is One-to-Many and, Many-to-One (Is this what is called Bidirectional?). Until now this is what I was able to do through online references:

In Sim.java
Code:
public List<Message> getMessages() {
      return messages;
   }
   public void setMessages(List<Message> messages) {
      this.messages = messages;
   }


In Message.java
Code:
   public Sim getSim() {
      return sim;
   }
   public void setSim(Sim sim) {
      this.sim = sim;
   }


I have the HBM files like this:
Sim.hbm.xml:
Code:
<bag inverse="true" cascade="delete"  lazy="false" name="messages">
      <key column="SIM_ID"/>
      <one-to-many class="MessageSource"/>
    </bag>


Message.hbm.xml

Code:
<property name="simId" column="SIM_ID" />
   <many-to-one insert="false" column="SIM_ID" cascade="none"  update="false" outer-join="true" name="sim"/>


And I have the tables defined respectively.
My question is how do I implement the bidirectionalily?
I read on different websites that the associations between the objects to be persisted by the developer. How do I do that?
And also, is the implementation until now correct or are there any errors in it?
How do I write the Unit Tests for these classes

Please help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 5:44 pm 
Beginner
Beginner

Joined: Tue Aug 01, 2006 4:57 am
Posts: 25
No help????


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 6:22 pm 
Beginner
Beginner

Joined: Tue Aug 08, 2006 11:53 am
Posts: 37
public class Sim {
...
private Set messages=new HashSet();

public void addMessage(Message m) {
m.setSim(this);
this.messages.add(m);
}
}

public class Message {
...
private Sim sim;

public void setSim(Sim s) {
this.sim=s;
}
}

This is called the scafoolding code.

Pls rate if this helped you!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 6:24 pm 
Beginner
Beginner

Joined: Tue Aug 08, 2006 11:53 am
Posts: 37
If you are using Lists then Set should be replaced by List. Also i excluded the generics related stuff for brevity


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 13, 2006 3:21 pm 
Beginner
Beginner

Joined: Tue Aug 01, 2006 4:57 am
Posts: 25
sorry for this simple question but where am I calling the 'addMessage' method? i searched for scaffolding but could not find any info....

any help regarding this please?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 13, 2006 5:30 pm 
Newbie

Joined: Thu Aug 10, 2006 12:25 am
Posts: 13
GameOne wrote:
sorry for this simple question but where am I calling the 'addMessage' method? i searched for scaffolding but could not find any info....

any help regarding this please?


it something you write to implement the adding of the child to the parent.

first set the child's link id to the parents - this is the link from the child to the parent.

then add the child to the set/list - this is the link from the parent to the child


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.