database.tex 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. \section{JPA - Java Persistence API}
  2. \label{sec:jpa}
  3. %The persistence layer was one of the biggest problems in Salespoint 2010. We wanted a solution that acknowledges latitudes for persisting objects with relatively low effort.
  4. One of the key features of \salespoint{} is its integrated persistence layer. The goal is to allow data persistence, while minimising programming effort and training period as well as maximising flexibility for framework users.
  5. %Therefore and because of the huge community support, we decided to use the Java Persistence API (JPA). JPA is a Java Programming Language Framework managing relational data in applications. The API itself is defined in the javax.persistence package.
  6. The obvious choice was the Java Persistence API (JPA), a Java framework managing relational data in Java Standard Edition or Enterprise Edition applications. \salespoint{} uses JPA 2.0, developed under JSR 317 and finalised in Dec, 2009 \cite{jpa}.
  7. Additionally to the API itself, which is defined in the \texttt{javax.persistence} package, JPA also consists of \textit{Persistence Entities}, \textit{ORM Metadata} and the \textit{Java Persistence Query Language} (JPQL).
  8. A persistence entity is usually a \textit{Plain Old Java Object} (POJO), which is mapped to a single table in a database.
  9. A row in such a database table corresponds to a specific instance of such an entity.
  10. Relational data between entities (and therefore tables) may be specified in an XML descriptor file or as annotations in Java source code.
  11. \salespoint{} uses annotations to provide object/relational metadata.
  12. Persistence entities may be related to each other by an inheritance hierarchy.
  13. A persistence entity may have a non-persistent superclass.
  14. Fields declared by a non-persistent superclass are not stored in the database if an inheriting entity is persisted.
  15. Three schemes exist to persist entites with an inheritance relationship: \textit{single table}, \textit{join table}, and \textit{table per class}.
  16. The \textit{single table} strategy stores all instances of classes of an inheritance hierarchy in the same table.
  17. The table contains columns for every attribute a persistence entity in the hierarchy declares.
  18. The different types are distinguished by a type discriminator column.
  19. The discriminator value for each persistence entity in an inheritance hierarchy is generated automatically or can be supplied by the user.
  20. The \textit{join table} strategy uses a table for the root persistence entity of the inheritance hierarchy.
  21. Additionally, a table is addded for each persistence entity in the inheritance hierarchy.
  22. In the supplementary tables, a foreign key is used to reference a row in the table of the parent persistence entity.
  23. Each table contains only columns for fields declared by a specific persistence entity in the inheritance hierarchy, but neither for the entities children nor parents.
  24. To reconstruct an object from the database, the different tables have to be joined using this foreign key, thus the name of this strategy.
  25. The \textit{table per class} strategy creates a table for each persistence entity containing all fields of the class, including inherited fields.
  26. The inheritance strategy of an inheritance hierarchy has to be declared at the root persistence entity.
  27. The inheritance stratety may not be changes for a sub-hierarchy, because JPA 2.0 does not require this feature.
  28. JPA 2.0 only requires the \textit{single table} and \textit{join table} strategies to be implemented.
  29. \salespoint{} uses the \textit{single table} strategy exclusively.
  30. The query language JPQL, which is similar to SQL, is used to retrieve entity information from the database.
  31. In contrast to SQL, JPQL queries act on entity objects instead of database tables.
  32. JPA implementations translate a JPQL statement to SQL statements at run time.
  33. It is possible to replace the DBMS while keeping the Java classes.
  34. It is possible to interface directly with the DBMS using \textit{Native Queries}.
  35. \salespoint{} however, uses the \textit{Criteria API} \cite{jpa, eclipselink-javadoc} to facilitate type safe querying.
  36. %Some of the most popular vendors of JPA are Hibernate and EclipseLink. For developing Salespoint 2011 we used JPA 2.0 with EclipseLink without vendor specific functionality. So it should also be possible to use other JPA 2.0 vendors like Hibernate with this framework.
  37. Multiple implementations of JPA 2.0 exist, for example TopLink \cite{toplink} and EclipseLink \cite{eclipselink}.
  38. The open source persistence and ORM framework Hibernate \cite{hibernate} also supports JPA 2.0.
  39. \salespoint{} uses the JPA 2.0 reference implementation, EclipseLink.
  40. No implementation specific code is used in \salespoint{}, therefore it should be possible to interchange EclipseLink with another JPA 2.0 implementation.\footnote{This document will be updated, as soon as another JPA provider is tested with \salespoint{}.}