Apache Persistence Layers

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Apache Persistence Layers - Presentation Transcript

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

    + Henning SchmiedehausenHenning Schmiedehausen, 2 years ago

    custom

    1493 views, 3 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1493
      • 1493 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 96
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories