cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Marko <dma...@tiscali.cz>
Subject Cayenne ActiveRecord like interface
Date Tue, 09 Oct 2007 12:21:47 GMT
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


Mime
View raw message