• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Inserting records into to-many relationship
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inserting records into to-many relationship


  • Subject: Re: Inserting records into to-many relationship
  • From: Florijan Stamenkovic <email@hidden>
  • Date: Mon, 15 Aug 2005 13:13:29 +0200

Hi all,
Thanks for the help. But I am still stuck with the problem. What I am doing is I am declaring eoEditingC= this.session().defaultEditingContext() and I am inserting all the EO in it. I am not using any other EC for that matter. I have also added the EO to the EC before using the addObjectTO... function. Now it is giving me another exception saying "Cannot insert Object which is already there in the database". I am stumped again. What am I doing wrong now??

You can use many editing contexts, and many different kinds of them. The basics are covered in a quite understandable way in Joshua Marker's WO5 for Mac OSX (Visual QuickPro Guide). You however do not have to, so wait until you gain some experience with handling just one.


Inserting objects into the EC:
You only do that with new objects, that were not saved to the database yet.


If you already have records in the database (say some Author records), and you fetch them (in a certain EC, the default in your case), you can not insert them again. They are already contained in the editing context and you can relate other objects (that are inserted / fetched into the same EC) to them using addObjectToBothSides...

But to avoid trouble, use a static method createAndInsertInstance(EOEditingContext ec, String entityName) to create new objects (it is in the EOUtilities class). That way you know they are inserted into an editing context as soon as they are created. There will be some casting involved if you are generating java classes for your different entities.

Example relating an existing author record with a new book record

//fetch some authors
EOFetchSpecification fs = new EOFetchSpecification("Author", null, null) //qualifiers and sort orderings are null, see API
NSArray authors = session().defaultEditingContext().objectsWithFetchSpecification(fs);


//create a new book
Book aBook = (Book)EOUtilities.CreateAndInsertInstance(session().defaultEditingContex t(), "Book");


//take the first author available
if(authors.count() == 0)
	return _doSomethingPeculiar();
Author firstAuthor = (Author)authors.objectAtIndex(0);

//relate the two
aBook.addObjectToBothSidesOfRelationshipWithKey(firstAuthor, "Author");

//save the new book and the relationships
session().defaultEditingContext().saveChanges();


not sure about all method signatures being correct though. but that is the general drift, just to show the flow of things. if you are still having problems after using this typical flow, be sure to check you model definition. That one should be good however if you are doing an Apple or so tutorial.


Cheers
Flor

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Inserting records into to-many relationship
      • From: Tanmoy Roy <email@hidden>
References: 
 >Inserting records into to-many relationship (From: Tanmoy Roy <email@hidden>)
 >Re: Inserting records into to-many relationship (From: Zak Burke <email@hidden>)
 >Re: Inserting records into to-many relationship (From: Florijan Stamenkovic <email@hidden>)
 >Re: Inserting records into to-many relationship (From: Arturo PĂ©rez <email@hidden>)
 >Re: Inserting records into to-many relationship (From: Florijan Stamenkovic <email@hidden>)
 >Re: Inserting records into to-many relationship (From: Tanmoy Roy <email@hidden>)

  • Prev by Date: Re: Inserting records into to-many relationship
  • Next by Date: Re: NULLS and Formatters
  • Previous by thread: Re: Inserting records into to-many relationship
  • Next by thread: Re: Inserting records into to-many relationship
  • Index(es):
    • Date
    • Thread