[jboss-user] [JBoss Seam] - EntityHome + nested persist = FK constraint violation

damianharvey do-not-reply at jboss.com
Thu May 10 12:03:16 EDT 2007


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.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4044727#4044727

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4044727



More information about the jboss-user mailing list