apache persistence layers
DESCRIPTION
TRANSCRIPT
![Page 2: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/2.jpg)
The O/R impedance mismatch
Objects (Java Knowledge) RDBMS
firstName: Stringname: Stringmember: Boolean
People
type: Integerinfo: String
Contact
0..n
1
?
![Page 3: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/3.jpg)
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
![Page 4: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/4.jpg)
Selection criterias
• Data access (CRUD)
• Developers knowledge
• Database languages (SQL, DDL)
![Page 5: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/5.jpg)
Can we stop here? I just need to query data for
my (web) application.
I could use the “industry standard”, right?
![Page 6: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/6.jpg)
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?
![Page 7: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/7.jpg)
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
![Page 8: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/8.jpg)
Ok, ok, ok…
So, why Apache?
![Page 9: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/9.jpg)
Because we are at
ApacheCon…
![Page 10: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/10.jpg)
• Apache Software License V2
– Open Source
– Commercial friendly
– Non discriminatory
![Page 11: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/11.jpg)
• Strong community
– Many contributors
– Meritocracy
– Commercial support available
![Page 12: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/12.jpg)
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/)
![Page 13: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/13.jpg)
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
![Page 14: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/14.jpg)
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
![Page 15: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/15.jpg)
• Swing GUI tool for modeling
• Ant support, maven through ant tasks
• concepts related to WebObjects EOF
![Page 16: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/16.jpg)
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)
![Page 17: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/17.jpg)
• implements JPA (JSR-220)
• Ant support, maven through ant tasks
• Command line tools included
• based on Kodo, donated by BEA
![Page 18: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/18.jpg)
• Pros:– Standards based (JPA)– POJO support– Good documentation
• Cons:– Some learning effort required– Only command line tools included
![Page 19: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/19.jpg)
Example code
• This talk can only scratch the concepts
• Example code is availablefrom my homepage, alongwith examples for otherO/R tools:
http://henning.schmiedehausen.org/or-mappers/
http://svn.softwareforge.de/svn/opensource/talks/or-mappers/
![Page 20: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/20.jpg)
A sample relation
firstName: Stringname: Stringmember: Boolean
People
type: Integerinfo: String
Contact
0..n
1
![Page 21: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/21.jpg)
Cayenne propertyclass People extends CayenneDataObject {
void setFirstName(String firstName) { writeProperty("firstName", firstName);
}
String getFirstName() { return (String) readProperty("firstName");
}
![Page 22: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/22.jpg)
Cayenne relationclass 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");
}
![Page 23: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/23.jpg)
Cayenne mapping<obj-entity name="People"
className="om.People"dbEntityName="people">
<obj-attribute name="firstName" type="java.lang.String"db-attribute-path="people_firstname"/>
</obj-entity><obj-relationship name="people"
source="Contact" target="People"db-relationship-path="people"/>
Class/DB Map
Prop. / Col Map
Obj Relation
![Page 24: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/24.jpg)
OpenJPA property@Entity @Table(name="people")public class People {
private String firstName;
@Basic @Column(name="people_firstname") public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
Class/DB Map
Prop. / Col Map
![Page 25: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/25.jpg)
OpenJPA relation@Entity @Table(name="people")public class People { private Collection<Contact> contacts;
@OneToMany(mappedBy = "people", 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
![Page 26: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/26.jpg)
Accessing the Mapper• Cayenne:
DataContext dataContext = DataContext.createDataContext();
• OpenJPA:
EntityManagerFactory entityManagerFactory =Persistence.createEntityManagerFactory();
EntityManager em = entityManagerFactory.createEntityManager();
![Page 27: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/27.jpg)
Retrieving an Object by PK
• Cayenne:
People user = (People) DataObjectUtils .objectForPK(dataContext, People.class, 2);
• OpenJPA:
People user = (People) em.find(People.class, 2L);
![Page 28: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/28.jpg)
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();
![Page 29: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/29.jpg)
And Now……for Something Completely
Different
![Page 30: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/30.jpg)
iBATIS
• Data mapper framework
• couples SQL queries to Java objects
• Ant support, maven through ant tasks
• Abator tool / Eclipse plugin
![Page 31: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/31.jpg)
iBATIS• Pro:
– fast, lightweight, unintrusive– Other Languages: Ruby, .NET– POJO support– Eclipse plugin
• Cons:– SQL knowledge required– „not mainstream“ concept
![Page 32: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/32.jpg)
A mapped query<select id="getPeople" parameterClass="people"
resultMap="peopleResult">
select * from people
<dynamic prepend="where">
<isNotNull prepend="and" property="id">
people_id = #id#
</isNotNull>
</dynamic>
</select>
![Page 33: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/33.jpg)
Query examples
• Querying a single objectPeople select = new People();
select.setId(2L);
People user = (People) sqlMap .queryForObject("getPeople", select);
• Querying a listList select = sqlMap .queryForList("getPeople", null);
![Page 34: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/34.jpg)
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
![Page 35: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/35.jpg)
Which one to use?• This table is highly subjective! YMMV!
What When
Cayenne, OpenJPA …if your Java is better than your SQL
iBATIS …if your SQL is better than yourJava / .NET
All of them …if you need J2EE integration
OpenJPA … when standards compliance is a concern
![Page 36: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/36.jpg)
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/
![Page 37: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/37.jpg)
?
![Page 38: Apache Persistence Layers](https://reader038.vdocuments.net/reader038/viewer/2022110303/54c1e9344a7959ea328b45b4/html5/thumbnails/38.jpg)
Thanks for your attention!