2 Replies Latest reply on May 15, 2007 4:23 AM by Damian Harvey

    EntityHome + nested persist = FK constraint violation

    Damian Harvey Master

      Hi,

      I have an Entity USER that can be in many GROUPs. This relationship is held in the USERGROUP entity. I have a page where a new User can be created and their groups selected.

      When I go to persist these using the following code I get a Foreign Key Constraint Violation from mySQL as the USER doesn't yet exist in the database.

      public String add() {
      
       //Persist the user
       String status = userHome.persist();
       log.info("Persisted User");
      
       userHome.getEntityManager().flush();
       log.info("Flushed User");
      
       //Process the groups
       Iterator groupIter = groups.iterator();
       while(groupIter.hasNext()) {
       Groups group = (Groups)groupIter.next();
      
       //Use the EntityHome object to persist
       userGroupListHome.create();
       userGroupListHome.getInstance().setUser(userHome.getInstance());
       userGroupListHome.getInstance().setGroups(group);
      
       userGroupListHome.persist();
       }
      }
      


      My understanding was that the flush() would actually force the transient object to be saved to the database. If I look in the DB it isn't there.

      Would appreciate any advice as this has driven me nuts once before.

      Thanks,

      Damian.