-->
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.  [ 5 posts ] 
Author Message
 Post subject: General persistence association problem.
PostPosted: Thu Jul 28, 2005 1:00 pm 
Newbie

Joined: Thu Jul 28, 2005 11:39 am
Posts: 4
I've recently started using hibernate for a University project and am trying to convert an application written using EJB CMP to hibernate.

I realise that hibenate doesn't manage class associations like CMP, so "scaffolding code" has to be written to change both sides of the relationship. Is there an elegant way of making the relationship udpatable from both sides with the same references being made?
for example;

I have two classes, supervisor and student, that share a one-to-many relationship that is bi-directional.

Now with EJB;
supervisor.getStudents().add(student);
and
student.setSupervisor(supervisor);
will result in the same updates to the database(provided this is specifed in the deployment descriptor) .

Now when adding the code to the hibernate app to manage the relationship,

-- supervisor class --
public void addStudent(Student student)
{
this.getStudents().add(student);
student.setSupervisor(this);
}

-- student class --
public void setSupervisor(Faculty faculty)
{
this.supervisor = faculty;
faculty.getStudents().add(this);
}

So if I call supervisor.addStudent(student) then
faculty.getStudents().add(Student) will be called twice.

Of course I could leave this out of the setSupervisor method but then the relationship can only be udated from one side by supervisor.addStudent() method and still correctly update both sides of the relation.

cheers for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 4:52 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I think both the methods don't need the second line...

-- supervisor class --
public void addStudent(Student student)
{
this.getStudents().add(student);
//student.setSupervisor(this);
}

-- student class --
public void setSupervisor(Faculty faculty)
{
this.supervisor = faculty;
//faculty.getStudents().add(this);
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 5:41 pm 
Newbie

Joined: Thu Jul 28, 2005 11:39 am
Posts: 4
anar wrote:
I think both the methods don't need the second line...

-- supervisor class --
public void addStudent(Student student)
{
this.getStudents().add(student);
//student.setSupervisor(this);
}

-- student class --
public void setSupervisor(Faculty faculty)
{
this.supervisor = faculty;
//faculty.getStudents().add(this);
}


Doesn't this mean that when a student is added to a supervisors set of students with the supervisor.add(student) method, the student object won't then have it's suprvisor set.

I realise that this is the natural functionality of the Java language, I just thought there might be a way for hibernate to update both sides of the relation(i.e some form of managed persistence).

From all the examples and the caveat emptor example, it seems like the done thing is to have the relationship always updated in one direction and only put the scafolding code in that class.

cheers


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 11:02 am 
Newbie

Joined: Thu Jul 28, 2005 11:39 am
Posts: 4
I've realised what I was trying was wrong. Rather than having these "convenience methods" on both sides of the relation, I now just use the "scafolding code" in the client to udate either side.

Sorry, was just working in EJB mode where the world is nice and regulated, call me sick but I prefer it. Maybe I just need some more time to grow to love hibernate.

cheers


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 11:29 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I thought this was why entity beans are so bad, because you need a container to execute the bidirectional pointer mess (typical for a network programming language such as Java)? Now its good and POJO persistence is bad? :) You can't have it both ways. (Actually, we could all use relational databases and real data languages, but that won't happen.)


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