2 Replies Latest reply on Apr 17, 2003 4:52 PM by mlange

    Need help with m-n mapping....

    mdelamere

      Hi,

      I?m starting to go nuts with my n-m mapping :-). I?ve read various docs and
      have gone through the archive but I haven?t been able to get any wiser on
      this. Below you can see the code for adding ProfileValues to my relation
      profiles-collection in the CustomerValue. I then pass the CustomerValue to my
      sessionfacade which is supposed to handle the storing of my relationship
      customer<->profile. For some reason though the customer values are set
      correctly in the relationship table but the profile values have been newly
      created producing new values as apposed to writing the values that I passed
      to the session facade! If I then comment out the sequence key generation in
      my profile create method and just set the id form my profileValue object I
      get a "Entity with primary key [.139.] already exists" exception!?

      Could someone PLEASE help me on getting my relationship table filled with
      the correct values......

      I would be very grateful for any help given.

      Thanks,

      Michael


      wrong:
      --------------------------
      customer_id | profile_id
      --------------------------
      22 | 190 | new value instead of value
      22 | 191 | passed to sessionfacade
      22 | 192 | which is 139
      ---------------------------

      --------------------------------------------------------
      Client code: (processing form data)
      --------------------------------------------------------
      /**
      * Client code for creating new ProfileValues from form input
      * and adding to customer (relation) collection
      */
      public static CustomerValue addProfiesToCustomer(String[] profiles,
      CustomerValue cv) {
      for (int i = 0; i < profiles.length; i++) {
      ProfileValue pv = new ProfileValue(new Integer(profiles), null,
      null);
      cv.addprofile(pv);
      }
      return cv;
      }

      --------------------------------------------------------
      Profiles Sessionfacade code (storing to relation table)
      --------------------------------------------------------
      /**
      * Store the Customer-Profile relation in the relation table
      * using the values passed from the client code above
      */
      public void storeCustomerProfiles(CustomerValue cv) {
      CustomerLocalHome home = null;

      try {
      home = (CustomerLocalHome) CustomerUtil.getLocalHome();
      CustomerLocal cl = home.findByPrimaryKey(new
      CustomerPK(cv.getId()));
      cl.setCustomerValue(cv);
      } catch(Exception e) {
      }

        • 1. Re: Need help with m-n mapping....
          mdelamere

          Hi,

          is my explanation not clear below or am I in the wrong forum?

          I could really do with some help on this.

          Regards,

          Michael

          • 2. Re: Need help with m-n mapping....
            mlange

            Micheal, the relation descriptors look ok, so there must be sth. in your code.

            When you set the m-n relation you always have to take two entities into account (e.g. account and user):

            public void assignUserToAccount(AccountLocal account, UserLocal user)
            {
            Collection accounts = user.getAccounts();
            accounts.add(account);
            }

            I can't see the error in your code but I do not understand why you say there is a new pk generated for the profile...you have to know the object if you want to set the relation.

            Can you post some more code (profile entity)?

            -marek