1 Reply Latest reply on Jul 12, 2011 10:32 PM by Ozzy Ozz

    Populating the relationship table

    Ozzy Ozz Newbie
      I had a question regarding inserting values into the relationship table.

      Assume we have 3 tables

      Account (id, name)
      Employee (id, name)
      Account_Employee(account_id, employee_id)

      On the employee edit screen I have a set of checkboxes where each checkbox represents an account.

      1. How do I get the mapping inserted into the table Account_Employee?
      2. Also how do I get the mapping deleted from the table Account_Employee once the checkbox is unchecked?
        • 1. Re: Populating the relationship table
          Ozzy Ozz Newbie
          So basically within the Employee class file (under the model directory) I have the below code:

          @Transient
          public void addAccount(int account_id)
          {
              if (!isAccountExists(account_id)) {
                  AccountEmployee ae = new AccountEmployee();
                  ae.setAccountId(account_id);
                  ae.setEmployeeId(this.id);
              }
          }

               @Transient
               private Boolean isAccountExists(int account_id)
               {
                    Iterator<AccountEmployee> iter = this.getAccountEmployees().iterator();
                    while (iter.hasNext()) {
                         if (iter.next().getAccountId()==account_id) {
                              return true;
                         }
                    }
                    return false;
               }

          Although the new AccountEmployee is created, it is not save to the database. How do I make the Employee Entity actually update the AccountEmployee entities too?