Hi David, I did something very similar (although your api looks
cleaner than mine; I particularly liked the Query portion...) in a
recent project combining tapestry 5 with cayenne.
I was able to have a single "add/edit' page and a single "view" page
for all of about 15 tables in the application, which would
automatically pick up new properties, etc.
There are certainly places in the application where a more detailed
knowledge of the schema is necessary (there are only 15 tables, but
some of them will store literally hundreds of thousands of records,
and the app is required to generate fairly complicated reports; for
performance purposes, using simple object queries, etc. just wouldn't
always cut it...). At some point, I will very likely adapt my code
for more general use outside of this particular project.
Robert
On Oct 9, 2007, at 10/97:21 AM , David Marko wrote:
Hello,
I like a ROR ActiveRecord a lot and I decided to create a special
interface for
Cayenne to let me work easier in most cases. I borrowed some ideas
and created a
wrapper for Apache Cayenne. Instead a common DAO like scenario of
using services
for each model, I can now use one EntityManager that serves for all
models.
The solution requires Java 5 as generics and some other features
are beeing
used. Here are just a few code snippets of how it can be used.
Syntax examples:
EntityManager em;
// create and save object
Contact contact=em.create(Contact.class);
em.saveChanges();
// find object by id
Contact contact=em.findById(Contact.class, id);
// find object by property
Contact contact=em.findFirstByProperty(Contact.class, "username",
form.getFieldValue("username"));
// find many objects by property
List contacts=em.findAllByProperty(Contact.class, "subdomain.name",
"subdomain");
// find objects based on user defined query
List contacts=em.findAllByQuery(Contact.class,
Query.select().where("subdomain = $subdomain and age
> $age")
.param("subdomain", "agh")
.param("age", 18)
.order("lastname",true)
.offset(200)
.limit(10)
.include(Contact.ROLE_ARRAY_PROPERTY));
// count items per invoice
List counts=em.count(Item.class, "invoice_id")
Its just a special API interface that let you work with Apache
Cayenne in very
easy way. Of course there is still a place for DAO for more
advanced situations.
Is anyone else working on some similar approach? I would like to share
experiences/ideas.
David