Your SlideShare is downloading. ×
0
Cayenne, OpenJPA, iBatis  Apache Persistence Layers ApacheCon EU 2008 Henning Schmiedehausen [email_address]
The O/R impedance mismatch Objects (Java Knowledge) RDBMS ?
In a nutshell… <ul><li>“ Object-Relational mapping (O/RM), </li></ul><ul><li>is a programming technique that links </li></...
Selection criterias <ul><li>Data access (CRUD) </li></ul><ul><li>Developers knowledge  </li></ul><ul><li>Database language...
Can we stop here?  I just need to query data for my (web) application. I could use the  “industry standard”, right?
Yeah, right <ul><li>JDO 1.0, 2.0, 2.1 ? </li></ul><ul><li>EJB 2 (Ieek!) or 3 ? </li></ul><ul><li>JPA ? </li></ul><ul><li>J...
Meanwhile, outside Apache… <ul><li>Hibernate </li></ul><ul><ul><li>„ de facto standard“ </li></ul></ul><ul><ul><li>(L)GPL ...
Ok, ok, ok… So, why Apache?
Because we are at ApacheCon…
<ul><li>Apache Software License V2 </li></ul><ul><ul><li>Open Source </li></ul></ul><ul><ul><li>Commercial friendly </li><...
<ul><li>Strong community </li></ul><ul><ul><li>Many contributors </li></ul></ul><ul><ul><li>Meritocracy </li></ul></ul><ul...
Apache Persistence Layers <ul><li>Apache Cayenne </li></ul><ul><li>Apache OpenJPA </li></ul><ul><li>Apache iBatis </li></u...
Common Ground <ul><li>JDBC, JTA, JNDI </li></ul><ul><li>Many popular DBs supported: </li></ul><ul><ul><li>MySQL, PostgreSQ...
Where are we? <ul><li>Apache Cayenne </li></ul><ul><ul><li>2.0.4 (Oct 12th, 2007) </li></ul></ul><ul><ul><li>3.0M3 </li></...
<ul><li>Swing GUI tool for modeling </li></ul><ul><li>Ant support, maven through ant tasks </li></ul><ul><li>concepts rela...
Cayenne <ul><li>Pros: </li></ul><ul><ul><li>GUI modeler included </li></ul></ul><ul><ul><li>Good documentation </li></ul><...
<ul><li>implements JPA (JSR-220) </li></ul><ul><li>Ant support, maven through ant tasks </li></ul><ul><li>Command line too...
<ul><li>Pros: </li></ul><ul><ul><li>Standards based (JPA) </li></ul></ul><ul><ul><li>POJO support </li></ul></ul><ul><ul><...
Example code <ul><li>This talk can only scratch the concepts </li></ul><ul><li>Example code is available from my homepage,...
A sample relation
Cayenne property <ul><li>class People extends CayenneDataObject { </li></ul><ul><li>void setFirstName(String  firstName ) ...
Cayenne relation <ul><li>class People extends CayenneDataObject { </li></ul><ul><li>void addToContacts(Contact  contact ) ...
Cayenne mapping <ul><li><obj-entity name=&quot;People&quot;  className=&quot; om.People &quot; dbEntityName=&quot; people ...
OpenJPA property <ul><li>@Entity @Table(name=&quot;people&quot;) </li></ul><ul><li>public class People { </li></ul><ul><li...
OpenJPA relation <ul><li>@Entity @Table(name=&quot;people&quot;) </li></ul><ul><li>public class People { </li></ul><ul><li...
Accessing the Mapper <ul><li>Cayenne: </li></ul><ul><ul><li>DataContext  dataContext  = DataContext.createDataContext(); <...
Retrieving an Object by PK <ul><li>Cayenne: </li></ul><ul><ul><li>People user = (People) DataObjectUtils  .objectForPK( da...
Changing an Object <ul><li>Cayenne: </li></ul><ul><ul><li>People people = retrieve();  </li></ul></ul><ul><ul><li>people.s...
And Now… …for Something Completely Different
iBATIS <ul><li>Data mapper framework </li></ul><ul><li>couples SQL queries to Java objects </li></ul><ul><li>Ant support, ...
iBATIS <ul><li>Pro: </li></ul><ul><ul><li>fast, lightweight, unintrusive </li></ul></ul><ul><ul><li>Other Languages: Ruby,...
A mapped query <ul><li><select id=&quot; getPeople &quot;  parameterClass=&quot;people&quot;  </li></ul><ul><li>resultMap=...
Query examples <ul><li>Querying a single object </li></ul><ul><ul><li>People  select = new   People (); </li></ul></ul><ul...
Caveat <ul><li>iBatis is not an O/R mapper, it is a data mapper!  </li></ul><ul><li>calling  retrieve()  twice returns: </...
Which one to use? <ul><li>This table is highly subjective! YMMV! </li></ul>…  when standards compliance is a concern OpenJ...
Where to go from here? <ul><li>http://henning.schmiedehausen.org/or-mappers/ </li></ul><ul><ul><li>Talk slides and Example...
?
Thanks for your attention!
Upcoming SlideShare
Loading in...5
×

Apache Persistence Layers

3,078

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,078
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
130
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • Transcript of "Apache Persistence Layers"

    1. 1. Cayenne, OpenJPA, iBatis Apache Persistence Layers ApacheCon EU 2008 Henning Schmiedehausen [email_address]
    2. 2. The O/R impedance mismatch Objects (Java Knowledge) RDBMS ?
    3. 3. In a nutshell… <ul><li>“ Object-Relational mapping (O/RM), </li></ul><ul><li>is a programming technique that links </li></ul><ul><li>databases to object-oriented language </li></ul><ul><li>concepts, creating (in effect) a ‘virtual </li></ul><ul><li>object database’.” </li></ul><ul><li>– Wikipedia </li></ul>
    4. 4. Selection criterias <ul><li>Data access (CRUD) </li></ul><ul><li>Developers knowledge </li></ul><ul><li>Database languages (SQL, DDL) </li></ul>
    5. 5. Can we stop here? I just need to query data for my (web) application. I could use the “industry standard”, right?
    6. 6. Yeah, right <ul><li>JDO 1.0, 2.0, 2.1 ? </li></ul><ul><li>EJB 2 (Ieek!) or 3 ? </li></ul><ul><li>JPA ? </li></ul><ul><li>J2SE ? J2EE ? </li></ul><ul><li>And wait, there is more… </li></ul><ul><ul><li>… Legacy code? Optimized SQL? </li></ul></ul><ul><ul><li>… Transparent or explicit persistence? </li></ul></ul>
    7. 7. Meanwhile, outside Apache… <ul><li>Hibernate </li></ul><ul><ul><li>„ de facto standard“ </li></ul></ul><ul><ul><li>(L)GPL licensed </li></ul></ul><ul><ul><li>driven by a single company </li></ul></ul><ul><li>TopLink Essentials </li></ul><ul><ul><li>JSR 220 Reference Implementation </li></ul></ul><ul><ul><li>CDDL + GPLv2 </li></ul></ul><ul><ul><li>driven by a single company </li></ul></ul>
    8. 8. Ok, ok, ok… So, why Apache?
    9. 9. Because we are at ApacheCon…
    10. 10. <ul><li>Apache Software License V2 </li></ul><ul><ul><li>Open Source </li></ul></ul><ul><ul><li>Commercial friendly </li></ul></ul><ul><ul><li>Non discriminatory </li></ul></ul>
    11. 11. <ul><li>Strong community </li></ul><ul><ul><li>Many contributors </li></ul></ul><ul><ul><li>Meritocracy </li></ul></ul><ul><ul><li>Commercial support available </li></ul></ul>
    12. 12. Apache Persistence Layers <ul><li>Apache Cayenne </li></ul><ul><li>Apache OpenJPA </li></ul><ul><li>Apache iBatis </li></ul><ul><li>not in this talk: </li></ul><ul><li>Apache Torque (http://db.apache.org/torque/) </li></ul><ul><li>Apache OJB (http://db.apache.org/ojb/) </li></ul>
    13. 13. Common Ground <ul><li>JDBC, JTA, JNDI </li></ul><ul><li>Many popular DBs supported: </li></ul><ul><ul><li>MySQL, PostgreSQL, Oracle, HSQLDB, Apache Derby </li></ul></ul><ul><li>XML mapping definition files </li></ul><ul><li>1:1, 1:n, m:n mappings </li></ul><ul><li>native and generated primary keys </li></ul>
    14. 14. Where are we? <ul><li>Apache Cayenne </li></ul><ul><ul><li>2.0.4 (Oct 12th, 2007) </li></ul></ul><ul><ul><li>3.0M3 </li></ul></ul><ul><li>Apache OpenJPA </li></ul><ul><ul><li>1.0.2 (Feb 18th, 2008) </li></ul></ul><ul><ul><li>1.1.0-SNAPSHOT </li></ul></ul><ul><li>Apache iBatis </li></ul><ul><ul><li>2.3.1 Beta (Mar 25th, 2008) </li></ul></ul><ul><ul><li>3.0 in planning state </li></ul></ul>
    15. 15. <ul><li>Swing GUI tool for modeling </li></ul><ul><li>Ant support, maven through ant tasks </li></ul><ul><li>concepts related to WebObjects EOF </li></ul>
    16. 16. Cayenne <ul><li>Pros: </li></ul><ul><ul><li>GUI modeler included </li></ul></ul><ul><ul><li>Good documentation </li></ul></ul><ul><li>Cons: </li></ul><ul><ul><li>all data objects inherit DataObject (3.0 will have POJO support) </li></ul></ul><ul><ul><li>no standards compliant API (3.0 will be JPA compliant) </li></ul></ul>
    17. 17. <ul><li>implements JPA (JSR-220) </li></ul><ul><li>Ant support, maven through ant tasks </li></ul><ul><li>Command line tools included </li></ul><ul><li>based on Kodo, donated by BEA </li></ul>
    18. 18. <ul><li>Pros: </li></ul><ul><ul><li>Standards based (JPA) </li></ul></ul><ul><ul><li>POJO support </li></ul></ul><ul><ul><li>Good documentation </li></ul></ul><ul><li>Cons: </li></ul><ul><ul><li>Some learning effort required </li></ul></ul><ul><ul><li>Only command line tools included </li></ul></ul>
    19. 19. Example code <ul><li>This talk can only scratch the concepts </li></ul><ul><li>Example code is available from my homepage, along with examples for other O/R tools: </li></ul><ul><li>http://henning.schmiedehausen.org/or-mappers/ </li></ul><ul><li>http://svn.softwareforge.de/svn/opensource/talks/or-mappers/ </li></ul>
    20. 20. A sample relation
    21. 21. Cayenne property <ul><li>class People extends CayenneDataObject { </li></ul><ul><li>void setFirstName(String firstName ) { writeProperty ( &quot;firstName&quot; , firstName ); </li></ul><ul><li>} </li></ul><ul><li>String getFirstName() { return (String) readProperty ( &quot;firstName&quot; ); </li></ul><ul><li>} </li></ul>
    22. 22. Cayenne relation <ul><li>class People extends CayenneDataObject { </li></ul><ul><li>void addToContacts(Contact contact ) { addToManyTarget ( &quot;contacts&quot; , contact , true); </li></ul><ul><li>} </li></ul><ul><li>void removeFromContacts(Contact contact ) { removeToManyTarget ( &quot;contacts&quot; , contact , true); </li></ul><ul><li>} </li></ul><ul><li>List getContacts() { return (List) readProperty ( &quot;contacts&quot; ); </li></ul><ul><li>} </li></ul>
    23. 23. Cayenne mapping <ul><li><obj-entity name=&quot;People&quot; className=&quot; om.People &quot; dbEntityName=&quot; people &quot;> </li></ul><ul><li><obj-attribute name=&quot; firstName &quot; type=&quot;java.lang.String&quot; db-attribute-path=&quot; people_firstname &quot;/> </li></ul><ul><li></obj-entity> </li></ul><ul><li><obj-relationship name=&quot;people&quot; source=&quot; Contact &quot; target=&quot; People &quot; db-relationship-path=&quot;people&quot;/> </li></ul>Class/DB Map Prop. / Col Map Obj Relation
    24. 24. OpenJPA property <ul><li>@Entity @Table(name=&quot;people&quot;) </li></ul><ul><li>public class People { </li></ul><ul><li>private String firstName ; </li></ul><ul><li>@Basic @Column(name=&quot;people_firstname&quot;) </li></ul><ul><li>public String getFirstName() { </li></ul><ul><li>return firstName ; </li></ul><ul><li>} </li></ul><ul><li>public void setFirstName(String firstName) { </li></ul><ul><li>this. firstName = firstName; </li></ul><ul><li>} </li></ul>Class/DB Map Prop. / Col Map
    25. 25. OpenJPA relation <ul><li>@Entity @Table(name=&quot;people&quot;) </li></ul><ul><li>public class People { </li></ul><ul><li>private Collection<Contact> contacts ; </li></ul><ul><li>@OneToMany(mappedBy = &quot;people&quot;, cascade={CascadeType.PERSIST, CascadeType.REMOVE}) </li></ul><ul><li>public Collection<Contact> getContacts() { </li></ul><ul><li>return contacts ; </li></ul><ul><li>} </li></ul><ul><li>public void setContacts( Collection<Contact> contacts) { </li></ul><ul><li>this. contacts = contacts; </li></ul><ul><li>} </li></ul>Class/DB Map Obj Relation
    26. 26. Accessing the Mapper <ul><li>Cayenne: </li></ul><ul><ul><li>DataContext dataContext = DataContext.createDataContext(); </li></ul></ul><ul><li>OpenJPA: </li></ul><ul><ul><li>EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(); </li></ul></ul><ul><ul><li>EntityManager em = entityManagerFactory .createEntityManager(); </li></ul></ul>
    27. 27. Retrieving an Object by PK <ul><li>Cayenne: </li></ul><ul><ul><li>People user = (People) DataObjectUtils .objectForPK( dataContext , People.class, 2); </li></ul></ul><ul><li>OpenJPA: </li></ul><ul><ul><li>People user = (People) em .find(People.class, 2L); </li></ul></ul>
    28. 28. Changing an Object <ul><li>Cayenne: </li></ul><ul><ul><li>People people = retrieve(); </li></ul></ul><ul><ul><li>people.setMember(true); </li></ul></ul><ul><ul><li>dataContext. commitChanges() ; </li></ul></ul><ul><li>OpenJPA: </li></ul><ul><ul><li>Transaction tx = em .getTransaction(); </li></ul></ul><ul><ul><li>tx.begin() ; </li></ul></ul><ul><ul><li>People people = retrieve(); </li></ul></ul><ul><ul><li>people.setMember(true); </li></ul></ul><ul><ul><li>tx.commit() ; </li></ul></ul>
    29. 29. And Now… …for Something Completely Different
    30. 30. iBATIS <ul><li>Data mapper framework </li></ul><ul><li>couples SQL queries to Java objects </li></ul><ul><li>Ant support, maven through ant tasks </li></ul><ul><li>Abator tool / Eclipse plugin </li></ul>
    31. 31. iBATIS <ul><li>Pro: </li></ul><ul><ul><li>fast, lightweight, unintrusive </li></ul></ul><ul><ul><li>Other Languages: Ruby, .NET </li></ul></ul><ul><ul><li>POJO support </li></ul></ul><ul><ul><li>Eclipse plugin </li></ul></ul><ul><li>Cons: </li></ul><ul><ul><li>SQL knowledge required </li></ul></ul><ul><ul><li>„ not mainstream“ concept </li></ul></ul>
    32. 32. A mapped query <ul><li><select id=&quot; getPeople &quot; parameterClass=&quot;people&quot; </li></ul><ul><li>resultMap=&quot;peopleResult&quot; > </li></ul><ul><li>select * from people </li></ul><ul><li><dynamic prepend=&quot;where&quot;> </li></ul><ul><li><isNotNull prepend=&quot;and&quot; property=&quot;id&quot;> </li></ul><ul><li>people_id = #id# </li></ul><ul><li></isNotNull> </li></ul><ul><li></dynamic> </li></ul><ul><li></select> </li></ul>
    33. 33. Query examples <ul><li>Querying a single object </li></ul><ul><ul><li>People select = new People (); </li></ul></ul><ul><ul><li>select.setId(2L); </li></ul></ul><ul><ul><li>People user = (People) sqlMap .queryForObject(&quot; getPeople &quot;, select); </li></ul></ul><ul><li>Querying a list </li></ul><ul><ul><li>List select = sqlMap .queryForList(&quot; getPeople &quot;, null); </li></ul></ul>
    34. 34. Caveat <ul><li>iBatis is not an O/R mapper, it is a data mapper! </li></ul><ul><li>calling retrieve() twice returns: </li></ul><ul><ul><li>Cayenne, OpenJPA: The same object </li></ul></ul><ul><ul><li>iBatis: Two different objects </li></ul></ul>
    35. 35. Which one to use? <ul><li>This table is highly subjective! YMMV! </li></ul>… when standards compliance is a concern OpenJPA … if you need J2EE integration All of them … if your SQL is better than your Java / .NET iBATIS … if your Java is better than your SQL Cayenne, OpenJPA When What
    36. 36. Where to go from here? <ul><li>http://henning.schmiedehausen.org/or-mappers/ </li></ul><ul><ul><li>Talk slides and Example code </li></ul></ul><ul><li>O/R Mappers Homepages: </li></ul><ul><ul><li>http://cayenne.apache.org/ </li></ul></ul><ul><ul><li>http://openjpa.apache.org/ </li></ul></ul><ul><ul><li>http://ibatis.apache.org/ </li></ul></ul><ul><ul><li>Other ASF persistence mappers: </li></ul></ul><ul><ul><li>http://db.apache.org/torque / </li></ul></ul><ul><ul><li>http://db.apache.org/ojb / </li></ul></ul>
    37. 37. ?
    38. 38. Thanks for your attention!
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×