apache cayenne for wo devs

60
Apache Cayenne for WO Devs by Andrus Adamchik, ObjectStyle LLC

Upload: wo-community

Post on 19-May-2015

2.714 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Apache Cayenne for WO Devs

Apache Cayenne for WO Devsby Andrus Adamchik, ObjectStyle LLC

Page 2: Apache Cayenne for WO Devs

• 2001 - inception

• 2002 - first “alpha” release and a large production deployment

• 2006 - Cayenne becomes “Apache Cayenne”

• still active and evolving...

History

Page 3: Apache Cayenne for WO Devs

• A top-level project at Apache Software Foundation

• 17 committers

• 9 PMC members

• Majority of PMC have WO/EOF background

• ... not all are active ...

Project Structure and Governance

Page 4: Apache Cayenne for WO Devs

Releases

• 3.0 - current stable

• 3.1 - Beta, recommended for all users

• 3.2 - in development; used for this presentation

Page 5: Apache Cayenne for WO Devs

Mapping Structure

Page 6: Apache Cayenne for WO Devs

Separate DB and Object Layers

Page 7: Apache Cayenne for WO Devs

DB layer: DbEntities containing DbAttributes and connected with DbRlationships

• DbEntity - models a table or a view

• DbAttribute - models a column

• DbRelationship - models PK/FK relationship going in one direction

Page 8: Apache Cayenne for WO Devs

Object layer: ObjEntities containing ObjAttributes and connected with ObjRlationships

• ObjEntity - models a persistent Java class

• ObjAttribute - models a “simple” property of a Java class

• ObjRelationship - models a “relationship” property, i.e. a property of type that is another ObjEntity (can be to-one or to-many). Going in one direction only.

Page 9: Apache Cayenne for WO Devs

Connecting Obj to Db Layer

• ObjEntity references a single DbEntity

Page 10: Apache Cayenne for WO Devs

Connecting Obj to Db Layer

• (simple attribute) ObjAttribute references a DbAttribute

• (flattened attribute) ObjAttribute references a “dbpath” across one or more DbRelationships ending with an attribute

Page 11: Apache Cayenne for WO Devs

Connecting Obj to Db Layer

• (simple relationship) ObjRelationship references a DbRelationship

• (flattened relationship) ObjRelationship references a “dbpath” across 2 or more DbRelationships

Page 12: Apache Cayenne for WO Devs

CayenneModeler

Page 13: Apache Cayenne for WO Devs

Cross-platform natively-packaged tool to work on Cayenne mapping projects

Page 14: Apache Cayenne for WO Devs

Tools > Reengineer Database Schema

Page 15: Apache Cayenne for WO Devs

Tools > Generate Database Schema

Page 16: Apache Cayenne for WO Devs

Tools > Migrate Schema

Page 17: Apache Cayenne for WO Devs

Tools > Generate Classes

Page 18: Apache Cayenne for WO Devs

Tools > Import EOModel

Page 19: Apache Cayenne for WO Devs

Modeling Workflow

Page 20: Apache Cayenne for WO Devs

Challenge - keeping these in sync:

Page 21: Apache Cayenne for WO Devs

Maven and Ant Tools

• cgen - generates classes from the ORM model

• cdbimport - generates model from DB

• cdbgen - generates DB from the model

Page 22: Apache Cayenne for WO Devs

Modeler Workflow... on project start:

Page 23: Apache Cayenne for WO Devs

Modeler Workflow... on each iteration:

Page 24: Apache Cayenne for WO Devs

Modeler-Free Workflow... on project start:

Page 25: Apache Cayenne for WO Devs

Modeler-Free Workflow... on each iteration:

Page 26: Apache Cayenne for WO Devs

Modeler-free Workflow - Maven

<plugin> <groupId>org.apache.cayenne.plugins</groupId> <artifactId>maven-cayenne-plugin</artifactId> <configuration> <map>${project.basedir}/src/main/resources/my.map.xml</map> <destDir>${project.basedir}/src/main/java</destDir> <defaultPackage>com.example.cayenne</defaultPackage> <superPkg>com.example.cayenne.auto</superPkg> <url>jdbc:mysql://127.0.0.1/mydb</url> <username>user</username> <password>secret</password> <driver>com.mysql.jdbc.Driver</driver> <excludeTables>migrations</excludeTables> </configuration> <executions> <execution> <id>default-cli</id> <goals> <goal>cdbimport</goal> <goal>cgen</goal> </goals> </execution> </executions></plugin>

Page 27: Apache Cayenne for WO Devs

When to use Modeler-free workflow?

• when DB objects have sane naming

• when DB has FK constraints defined

• when no per-entity model tweaking is needed (optimistic locking, PK generation, exclusion of attributes, etc.)

Page 28: Apache Cayenne for WO Devs

Writing Apps with Cayenne

Page 29: Apache Cayenne for WO Devs

Starting Cayenne

// create reusable ‘runtime’ instanceServerRuntime runtime = new ServerRuntime("cayenne-myproject.xml");

Page 30: Apache Cayenne for WO Devs

Stopping Cayenne

runtime.shutdown();

Page 31: Apache Cayenne for WO Devs

ObjectContext

Page 32: Apache Cayenne for WO Devs

Obtaining ObjectContext

// regular contextObjectContext context = runtime.newContext();

// nested contextObjectContext nestedContext = runtime.newContext(context);

Page 33: Apache Cayenne for WO Devs

ObjectContext

• An isolated “session” to access Cayenne

• Has its own copy of each object

• Doesn’t require explicit shutdown (can be simply thrown away when no longer in use)

• Can be serialized

• Can be scoped differently based on app requirements

Page 34: Apache Cayenne for WO Devs

Under the Hood ServerRuntime is a DI container

Page 35: Apache Cayenne for WO Devs

Overriding a DI service to create custom ObjectContexts

public class MyContextFactory implements ObjectContextFactory { @Inject private EventManager eventManager;

@Override public ObjectContext createContext() { return new MyContext(eventManager); } @Override public ObjectContext createContext(DataChannel parent) { return new MyContext(parent, eventManager); }}

Module myModule = new Module() { @Override public void configure(Binder binder) { binder.bind(ObjectContextFactory.class).to(MyContextFactory.class); }};

// override built-in services via a custom moduleServerRuntime runtime = new ServerRuntime("cayenne-myproject.xml", myModule);

Page 36: Apache Cayenne for WO Devs

DI Container Highlights

• Very small - 40K

• Intended to make Cayenne runtime modular and customizable

• Does not interfere with or care about application DI

• Can be used outside Cayenne, but this was not the goal

Page 37: Apache Cayenne for WO Devs

Queries

Page 38: Apache Cayenne for WO Devs

SelectQuery

• Most commonly used query

• Data rows option

• Fetch offset/limit

• Prefetching (separate strategies)

• Result caching, pagination

• Iterated result

Page 39: Apache Cayenne for WO Devs

SelectQuery

ObjectContext context = ...

SelectQuery<Artist> query = new SelectQuery<Artist>(Artist.class);List<Artist> allArtists = context.select(query);

Page 40: Apache Cayenne for WO Devs

SelectQuery with qualifier

ObjectContext context = ...

SelectQuery<Artist> query = new SelectQuery<Artist>(Artist.class);query.andQualifier(Artist.NAME.like("A%"));query.andQualifier(Artist.DATE_OF_BIRTH.lt(new Date()));

List<Artist> someArtists = context.select(query);

INFO: SELECT t0.NAME, t0.DATE_OF_BIRTH, t0.ID FROM ARTIST t0 WHERE (t0.NAME LIKE ?) AND (t0.DATE_OF_BIRTH < ?) [bind: 1->NAME:'A%', 2->DATE_OF_BIRTH:'2013-05-09 20:51:12.759']INFO: === returned 1 row. - took 2 ms.

Page 41: Apache Cayenne for WO Devs

Other Queries

• EJBQLQuery

• SQLTemplate

• ProcedureQuery

• ObjectIdQuery, RelationshipQuery, QueryChain

• Custom queries...

Page 42: Apache Cayenne for WO Devs

Caching Query Results

Page 43: Apache Cayenne for WO Devs

Can be used with any Query and is fairly transparent

SelectQuery<Artist> query = new SelectQuery<Artist>(Artist.class);query.andQualifier(Artist.ARTIST_NAME.like("A%"));

// This is all it takes to cache the result. // There’s no need to calculate a cache key. // Cayenne will do that for youquery.useLocalCache();

List<Artist> someArtists = context.select(query);

Page 44: Apache Cayenne for WO Devs

Lifecycle Events

• PostAdd, PrePersist, PreUpdate, PreRemove, PostPersist, PostUpdate, PostRemove, PostLoad

• Persistent objects can declare callback methods to be notified about own events

• Any non-persistent object can be a listener for various entity events.

Page 45: Apache Cayenne for WO Devs

Lifecycle Events

• Higher-level “workflows” are implemented by matching entities with listeners using custom annotations and providing operation context via DataChannelFilter

Page 46: Apache Cayenne for WO Devs

Remote Object Persistence (ROP)

Page 47: Apache Cayenne for WO Devs

ROP Highlights

• Same as nested ObjectContext, only child context lives in a remote JVM

• Full ORM API and behavior available on the client:

• Queries, lazy relationships, caching, pagination, etc.

• Client objects based on the same model as server

• Client objects are using a different superclass

Page 48: Apache Cayenne for WO Devs

Other ORM Choices

• Hibernate

• JPA

Page 49: Apache Cayenne for WO Devs

Cayenne Difference - Object Design

Others enhanced/proxied POJO ORM Annotations

Cayenne framework superclass annotations used for other things

Advantage

faster startup times “WYSIWYG” objects generic persistent objects less code clutter

Page 50: Apache Cayenne for WO Devs

Cayenne Difference - Runtime

Others explicit transactions

Cayenne on-demand implicit transactions

Advantage less code, cleaner code seamless relationship navigation

Page 51: Apache Cayenne for WO Devs

EOF Analogies

“All characters and events – even those based on real people – are entirely fictional.”

Page 52: Apache Cayenne for WO Devs

Persistent Objects

• EOEnterpriseObject : Persistent, DataObject, CayenneDataObject

• EOGlobalID : ObjectId

• EOGenericRecord : CayenneDataObject

Page 53: Apache Cayenne for WO Devs

Mapping

• EOModel : DataMap

• EOModelGroup : EntityResolver

• EOEntity: DbEntity / ObjEntity

Page 54: Apache Cayenne for WO Devs

Query

• EOFetchSpecification : SelectQuery

• EOQualifier : Expression

• EOSortOrdering : Ordering

Page 55: Apache Cayenne for WO Devs

Runtime

• EOEditingContext : ObjectContext

• EOAdapter : DataNode

• JDBCPlugin : DbAdapter

Page 56: Apache Cayenne for WO Devs

Cayenne != EOF

Page 57: Apache Cayenne for WO Devs

What You Might Miss from EOF

• Quirky Vertical Inheritance

• No Horizontal Inheritance

• No prototypes

• No Eclipse plugin

Page 58: Apache Cayenne for WO Devs

What You Get in Return• Multithreading

• IDE-independent Modeler

• Internal dependency injection

• Query abstraction beyond SelectQuery

• EJBQL and aggregated queries

• Iterated query

• Transparent Query result cache

• Transparent Query result pagination

• Auto-increment PK

• Automated DB type detection

• Prefetch strategies

• Lifecycle events

Page 59: Apache Cayenne for WO Devs

A chance to influence things!