You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by David Marko <dm...@tiscali.cz> on 2008/02/24 11:13:48 UTC
CayenneObjectManager - alternative API for using Apache Cayenne
Hello,
a few months ago a wrote about creating alternative approach for using Apache
Cayenne. Actualy I use Cayenne with Click Framework and following scenario came
from practice on several projects. The main idea is to use Cayenne as very
stable background(engine) but alter API to something closer to ActiveRecord like
approach. The result is CayenneObjectManager that implements alternative API
that, as result, generates standard Cayenne queries.
CayenneObjectManager API assumes Java5 as generics are used extensively and is
tested with latest Apache Cayenne M3. Entire package is rather small and is
intended to be extended by other methods. I'm not very experienced JAVA
developer so one thing I would like to ask is, if someone can review the concept
and correct some potencial mistakes or contribute with some ideas etc. I'm open
for correction.
Thanks,
David
See attached file with source at https://issues.apache.org/cayenne/browse/CAY-988
For quick example just image you have a simple table with Role names and imagine
how simply you can work with this:
// Load role object by its name
COManager manager=new COManagerImpl();
// init is based on usage scenario. This can be used in standalone app. for e.g.
web app usage, the context is inititalized automaticly from
DataContext.getThreadDataContext();
co_manager.setContext(DataContext.createDataContext());
Role
role_admin=co_manager.find(Role.class).byProperty("name").EQUALS("admin").first();
// all roles from allowed list as key-pair for checkbox field
Map<String,String>
roles=co_manager.find(Role.class).byProperty(Role.NAME_PROPERTY).IN(allowed_roles).to_pairs("name",
"id");
CayenneObjectsManager - alternative API for working with Apache Cayenne subsystem
// Available use cases
// find one item by ID
co_manager.find(Class).byId(id)
// find all items by property and given condition
co_manager.find(Class).byProperty("property").EQUALS(value)
// find first item by property and given condition
co_manager.find(Class).byProperty("property").BETWEEN(value1,value2).first()
co_manager.find(Class).byProperty("property").LIKE(value).first()
co_manager.find(Class).byProperty("property").IN(range).first()
// find all items
co_manager.find(Class).all()
// find all items by based on CayenneObjectsQuery
co_manager.find(Class).allByQuery(COQuery query)
// find all firts item by based on CayenneObjectsQuery
co_manager.find(Class).allByQuery(COQuery query).firts()
//find all selected items as key-pairs available e.g. for checkbox field
co_manager.find(Class).byProperty("property").EQUAL(value1).to_pairs(key,value)
// aggregate functions
co_manager.aggregate(Class).count("property")
co_manager.aggregate(Class).count("property", COQuery query)
co_manager.aggregate(Class).sum("property")
co_manager.aggregate(Class).sum("property", COQuery query)
co_manager.aggregate(Class).avg("property")
co_manager.aggregate(Class).avg("property", COQuery query)
co_manager.aggregate(Class).count("property",
Query.where("year>$year").addParam("year", 2003));
// common actions
co_manager.create(Class)
co_manager.save()
Re: CayenneObjectManager - alternative API for using Apache Cayenne
Posted by David Marko <dm...@tiscali.cz>.
Just for note, you can look to this URL, maybe we can learn something or at
least grab some idea.
http://www.thimbleware.com/projects/jrel
Re: CayenneObjectManager - alternative API for using Apache Cayenne
Posted by David Marko <dm...@tiscali.cz>.
I just read both links. I think that having some concept, that avoids using DAO
schema, would be very good. For most web applications DAO is just overhead and
require to write large infrastructure. Would be good to have flexibility of
ActiveRecord or Django ORM for many solutions where just 5 tables are involved
and developer need to design application in quick way. My implementation came
from https://activeobjects.dev.java.net/ . Author here creates entire ORM from
scratch, I rather prefer ot use existing ORM(like Cayenne) and just create
alternative API above.
BTW: I think this is why people enjoy using GORM in Grails. GORM is just higher
layer over Hibernate implemented in Groovy. People ussualy cry when using
Hibernate in JAVA but praise GORM which stays over Hibernate. So good API matters.
David
Re: CayenneObjectManager - alternative API for using Apache Cayenne
Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Ari,
thanks for pointing to that dev discussion thread. I was going to do
the same. In fact my own interest in this approach started as a result
of David posting his first example:
https://issues.apache.org/cayenne/browse/CAY-877
And I hope to reuse his work if possible (and at the minimum use some
of the ideas).
Andrus
On Feb 24, 2008, at 12:57 PM, Aristedes Maniatis wrote:
>
> On 24/02/2008, at 9:13 PM, David Marko wrote:
>
>> // find first item by property and given condition
>> co_manager
>> .find(Class).byProperty("property").BETWEEN(value1,value2).first()
>> co_manager.find(Class).byProperty("property").LIKE(value).first()
>> co_manager.find(Class).byProperty("property").IN(range).first()
>
> This looks a lot like the Fluent interface [1] Andrus has proposed
> as a possible way to generify the query API. Have a read of the
> relevant thread [2] for further info there. It is an interesting
> idea but quite hard to write documentation for (such as javadocs)
> and possibly hard for newcomers. On the other hand it is very
> compact and readable.
>
>
> Ari Maniatis
>
>
>
> [1] http://martinfowler.com/bliki/FluentInterface.html
> [2] http://markmail.org/message/jqfjtsogixpeijsn
>
>
> -------------------------->
> ish
> http://www.ish.com.au
> Level 1, 30 Wilson Street Newtown 2042 Australia
> phone +61 2 9550 5001 fax +61 2 9550 4001
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C 5EFA EF6A 7D2E 3E49 102A
>
>
>
Re: CayenneObjectManager - alternative API for using Apache Cayenne
Posted by David Marko <dm...@tiscali.cz>.
Haven't read it yet. Will look at it, thanks very much! I would really
appreciate some Cayenne standardized way, if there will be some.
David Marko
Aristedes Maniatis wrote:
>
> On 24/02/2008, at 9:13 PM, David Marko wrote:
>
>> // find first item by property and given condition
>> co_manager
>> .find(Class).byProperty("property").BETWEEN(value1,value2).first()
>> co_manager.find(Class).byProperty("property").LIKE(value).first()
>> co_manager.find(Class).byProperty("property").IN(range).first()
>
> This looks a lot like the Fluent interface [1] Andrus has proposed as a
> possible way to generify the query API. Have a read of the relevant
> thread [2] for further info there. It is an interesting idea but quite
> hard to write documentation for (such as javadocs) and possibly hard for
> newcomers. On the other hand it is very compact and readable.
>
>
> Ari Maniatis
>
>
>
> [1] http://martinfowler.com/bliki/FluentInterface.html
> [2] http://markmail.org/message/jqfjtsogixpeijsn
>
>
> -------------------------->
> ish
> http://www.ish.com.au
> Level 1, 30 Wilson Street Newtown 2042 Australia
> phone +61 2 9550 5001 fax +61 2 9550 4001
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C 5EFA EF6A 7D2E 3E49 102A
>
>
>
Re: CayenneObjectManager - alternative API for using Apache Cayenne
Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 24/02/2008, at 9:13 PM, David Marko wrote:
> // find first item by property and given condition
> co_manager
> .find(Class).byProperty("property").BETWEEN(value1,value2).first()
> co_manager.find(Class).byProperty("property").LIKE(value).first()
> co_manager.find(Class).byProperty("property").IN(range).first()
This looks a lot like the Fluent interface [1] Andrus has proposed as
a possible way to generify the query API. Have a read of the relevant
thread [2] for further info there. It is an interesting idea but quite
hard to write documentation for (such as javadocs) and possibly hard
for newcomers. On the other hand it is very compact and readable.
Ari Maniatis
[1] http://martinfowler.com/bliki/FluentInterface.html
[2] http://markmail.org/message/jqfjtsogixpeijsn
-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001 fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C 5EFA EF6A 7D2E 3E49 102A