-->
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.  [ 3 posts ] 
Author Message
 Post subject: Complex Delete :(
PostPosted: Tue Mar 21, 2006 8:13 am 
Newbie

Joined: Tue Mar 21, 2006 4:25 am
Posts: 13
Hello,

I have used Hibernate 3.0 for some time now, however I still consider myself as a newby. Latly I found this problem, which I did solve, however I do not know if I did solve it in the correct way.

I have the following objects. Semesters has a one to many relationship with Assignments, and an Assignment has a one to many relation with Subject.

In english this comes to that in a Semester you have more then one assignment of differen subjects.

In the Semester object I use a List to hold the different Assignments. The Assignment class has the methods get/setSemester which returns/sets the semester, get/setSubject which returns/sets the Subject and get/setId which returns/sets the id of the assignment (auto generated). The Subject class has the get/setId, get/setName and get/setDescription methods.

Now each time I change the assignments, I change the whole list. That is I do something as follows:

mySemester.setAssignments(newListOfAssignments);

However those elements in the previous list of assignment still have a reference to mySemester. And thus when I do save mySemester, I find that in the database I just added the new records, and not deleted the old ones (in the Assignments Table).

What I did , and did work is the following:

[code]
// Create a reference to the list of old assignments
List<Assignment> old = semester.getAssignments();

// Setting the selected newsletter inside the resource object.
SemesterService semesterService =
ServiceFactory.getSemesterService();
semester.setAssignments(assignments);
semesterService.update(semester);

// Removing reference of the other newsletter subscriptions of this user.
AssignmentService assignmentService =
ServiceFactory.getAssignmentService();
assignmentService.remove(old);
[/code]

That is I create a reference to the old list of assignments, I then set the new list of assignments in semester and persist the object (this will just add the new rows in the Assignments table in the database). And then I remove the assignments by setting transent all the elements in the list (This will remove the assignments from the Assignments Table in the database). This works because when i come to delete the assignments, semester no longer has a reference to those assignments.

I am sure there must be a more simple way how to implement this, and any help would me more then welcome.

Thanks,
kcorp


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 4:31 am 
Newbie

Joined: Tue Mar 21, 2006 4:25 am
Posts: 13
No I have any ideas on how I can make my code more logical. For example, is it an acceptable aproach that the code, to remove any referene of Assignment from Semester is moved to the SemesterDAO? So that all I have to do is persist the Semester object.

Or this may be clasified as a one of situation (and thus accepted) since in my code I am replacing one list with another?

thanks,
kcorp


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 6:01 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

You need a bidirectional parent child relationship. Investigate, if more help is needed, ask.


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