Question

I am seeking to use Apache Cayenne to implement a database, and would like to know how to use Cayenne's API to perform an "add" operation.

I have been reading over the query document for two days. It gives good information on how to get a list of objects in a table, how to search by primary key, how to use expressions to modify searches, and all the many different ways you can search a database.

It does not tell how to add a new record to a table, or how to make changes to a record. The doco writers seem primarily interested in documenting the nice searches you can do.

Can someone please either provide or point me to an example of doing adds and updates??? An example of a delete would be nice, too...

Someone please advise.

Était-ce utile?

La solution

Adding and deleting "data" in Cayenne is done via ObjectContext operations on corresponding "objects". The examples are available in the tutorial here and here and also in the main docs, but here is a quick explanation:

// create new object in memory
Artist a = context.newObject(Artist.class);

// now save it to DB. This will generate INSERT SQL
context.commitChanges();

// delete object in memory
context.deleteObjects(a);

// save this to DB too. This will generate DELETE SQL
context.commitChanges();

And I certainly recommend to go through the tutorial. It shows all the main pieces of Cayenne without giving too many distracting details.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top