Solved

1:M relationship - How do I remove & add the new ones

Posted on 2004-03-26
5
170 Views
Last Modified: 2010-03-31
I have a table which has a  

Unit : Unit-Teaches : INSTRUCTOR
1      :   M                : 1

Unit-Teaches hold the unit_key (UNit table) & instructor_key(Instructor Table)


A Unit can have one to many instructor. So the user when they update the unit can change all the instructor , add one/many or delete one/many.. My question how do I do this in a CMP and CMR? Should I be deleting all the record in the UNit-Teaches and then add again from the list I get from the user. Or how would I loop from the ones already existing and the ones I want to add or delete?


      //get the collection of Instructor already associated to this unit.
      Collection col = ul.getCrse_unit_instructor_teaches_fk_unit_key();
      Iterator it = col.iterator();

      while(it.hasNext)
      {
        Crse_unit_instructor_teachesLocal uitl = (Crse_unit_instructor_teachesLocal) it.next();
        Long instKey = uitl.getInstr_instructor_fk_instructor_key().getInstructor_key();
        //Loop through the instructor list selected by the user.
        for(int i=0; i < unit.getUnitInstructorList().length; i++)
        {
          //get the instructor key
          Long instructorKey = unit.getUnitInstructorList()[i];        
          ul.addUnitInstructorRelationship();
        }
      }


Any help would be appreciated












0
Comment
Question by:Caro0101
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10690409
Assuming that the m:n table has a primary key contraint on (unit_key, instructor_key), you can just remove/add records by specifying those values. There is no need to delete all, and then add certain ones back.

Uniqueness is on the combination of the two keys, which means that all the key combinations should be unique, i.e. there will only ever be one record of a certain combination of  unit_key and instructor_key.
0
 

Author Comment

by:Caro0101
ID: 10690494
I understand that but how do I loop to know which ones I need to remove and which ones are the same and which one I need to add..

I guess I need to loop through the list and add them and them loop through the list again and remove the ones I no longer want.. Is this correct?
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10690617
>> I guess I need to loop through the list and add them and them loop through the list again and remove the ones I no longer want.. Is this correct?
How are you presenting the information to the user? How is the user input transferred into business logic?

I would present a list of all instructors, and a list of instructors associated with the unit to the user. I would keep an empty list of instructors to add and one for the ones to remove from a unit behinds the scenes, and when the user adds or removes instructors, you can keep track of it like that. After editing is done, you can find out which instructors were added/removed from a unit and update the database table accordingly.
0
 

Author Comment

by:Caro0101
ID: 10690866
It is displayed as a multiple select list .

So by unselecting the instructor it just doesn't get passed in the String[] so I receive the list they want to add/exist.. If I also get the collection of the UNIT-Teaches this gives me
which ones are already in the database.. so then I need to loop and find out if it already exist if it does do nothing if it doesn't add it.. Then I also need to see if what is in the already existing list if it is no longer in the list the user has passed to me I need to delete it..

Does that make sense?

0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 500 total points
ID: 10691094
Since you're displaying the unit instructors in a multiple select list, assuming the instructors currently associated with unit are selected, you can subscribe to item selection events on the list. The events will tell you which index was affected, you can then query the list for the selection state of that index. Based on whether the item at the index is selected or not, you would add that instructor to the delete or add list (which is behind the scenes described above). Then once the editing is done, just iterate through the two lists and perform the updates to the database table.

A more efficient approach would be to double-check the two lists against the list of unit instructors that was initially read from the database, and remove from those two lists instructors not affected. E.g. instructor 1 was not on the unit, but during editing he/she was selected, and then unselected. Instructor 1 would now appear in both lists. By checking against the original list of instructors, you could just remove instructor 1 from any updates of the database table. Does that make sense to you?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
Cipher Configuration on Apache HTTPD 4 53
Basic Java Case or If-Else statement... 3 43
servlet doXXX methods 3 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now