Object in DB Error after using addObjectToBothSidesOfRelationshipWithKey

Webobjects <[email protected]>
Newsgroups gmane.comp.web.webobjects.devel
Organization Downtown Development Authority
Message-ID <[email protected]>
Hello,

I am having problems with addObjectToBothSidesOfRelationshipWithKey.
It is my understanding that if I wish to add a new object (not in a
editing context) to an object's relationship (in an editing context)
where the relationships are in place. I can do it in the following
way...

1. insert newObject into context
2. objectInContext. addObjectToBothSidesOfRelationshipWithKey(
newObject, "relationshipName")
3. context.saveChanges()


But everytime I do the following steps

1. Create book object and save to the db
2. Create a Summary Object and insert it in the default editing context
3. Use addObjectToBothSidesOfRelationshipWithKey to add the summary
object to the book. ( Note the book is in the default editing context
because I fetch from the db)
4. using savechanges() on the default editing context.

THIS WORK WITH NO PROBLEMS

5. Create article object ans save to the db (WORKS)
6. Create a Summary Object and insert it in the default editing context
7. Use addObjectToBothSidesOfRelationshipWithKey to add the summary
object to the Article. ( Note the article is in the default editing
context because I fetch from the db)
8. using savechanges() on the default editing context.

I GET THIS ERROR

---------------------------------------------------------------------------------------------------
java.lang.IllegalStateException: recordInsertForObject:
com.webobjects.eoaccess.EODatabaseContext
com.webobjects.eoaccess.EODatabaseContext@a3cf3e found a snapshot for EO
with Global ID:_EOIntegralKeyGlobalID[Article (java.lang.Integer)4] that
has been inserted into
com.webobjects.eocontrol.EOEditingContextcom.webobjects.eocontrol.EOEditingContext@ad44f6.
Cannot insert an object that is already in the database

---------------------------------------------------------------------------------------------------

Note, the relationships are ok in all the objects... I test it in
reverse order adding the article first and then the book I get the same
error but with it state the book is already in the DB..

I do not know what I am doing wrong..... you someone please help
clearify my mistake.

thank you,
richard

CODE
-----------------------------------

public void appendToResponse (WOResponse response, WOContext context)
	{
		
		aSummary = new Summary();
		aKeyPoint = new KeyPoint();
		aTerm = new Term();
		
			// I will first find if the user has a current reading
			// if I will make it not the current reading and then set the 
			// newCurrentReading as the user's current reading 
			
			EOEditingContext ec = session().defaultEditingContext();
			Session s = ( Session )session();
			
			
			// find the current reading if any
			// this not the must efficient way but easy to code and read
			//
			Enumeration enumeration =
s.sessionUser().articles().objectEnumerator();
			

			while( enumeration.hasMoreElements() ) {
				Article anArticle = ( Article )enumeration.nextElement();
				if( anArticle.isCurrentReading().equals( "Yes" ) ){
					currentReadingArticle = anArticle;
					showCurrentArticle = true;
					showGeneralArticleInfo = true;
				}//end if
				
			}//end while
			
			
			enumeration = s.sessionUser().books().objectEnumerator();
			
			while( enumeration.hasMoreElements() ) {
				Book aBook = ( Book )enumeration.nextElement();
				if( aBook.isCurrentReading().equals( "Yes" ) ){
					currentReadingBook = aBook;
					showCurrentBook = true;
					showGeneralBookInfo = true;
				}//end if
				
			}//end while
			
		
		super.appendToResponse(response, context);
}


    public My_Rep_CurrentReading addSummary()
    {
My_Rep_CurrentReading nextPage =
        (My_Rep_CurrentReading)pageWithName("My_Rep_CurrentReading");

        // Initialize your component here
		EOEditingContext ec = session().defaultEditingContext();
		NSTimestamp currentTime = new NSTimestamp();
			
		
			aSummary.setEntryDate(currentTime);
		
			ec.insertObject( aSummary );
			
			if( showCurrentArticle )
			{
				currentReadingArticle.addObjectToBothSidesOfRelationshipWithKey(
aSummary, "summarys" );
			} 
			else if ( showCurrentBook )
			{	
				currentReadingBook.addObjectToBothSidesOfRelationshipWithKey(
aSummary, "summarys" );
			}	

			ec.saveChanges();

        return nextPage;
    }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.