IS-907 Java EE Introduction to JPA
Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes, and between table rows and objects POJO (plain old java object) based Portable Even Åby Larsen IS-102 Introduksjon2
Object-Relational-Mapping Description of how an object model “maps” to a relational data model Class to table(s) Object properties (attributes) to columns or relations The mapping can be described as java source code annotations, or in xml mapping files xml mapping files override the source code annotations IS Course Info3
Persistence Unit A named configuration of entity classes their mapping to tables in a database transaction model Defined in persistence.xml, and source code annotations, or orm.xml mapping file Represented in java by an EntityManagerFactory object Even Åby Larsen IS-102 Introduksjon4
Persistence Context a managed set of entity objects (a “session”) keeps track of changes accessed through EntityManager objects Even Åby Larsen IS-102 Introduksjon5
Transactions define when entities are synchronized with the database bound to a persistence context accessible through UserTransaction objects Even Åby Larsen IS-102 Introduksjon6
Annotations Annotations provide data about a program that is not part of the program itself. Example uses: Information for the compiler Compile-time and deployment-time processing Runtime processing You can think of annotations as a special kind of comment that is intended for the computer rather than for humans. Annotations are java interfaces, so it is necessary to import them before they can be used IS Course Info7
JPA annotations Class annotations provide information about the class Class annotations appear before the class declaration (normally just before the public public class Employee {... } Property annotations provide information about class properties. Property annotations appear before the attribute declaration (just before private), or before the get-method declaration (just before private long empNo; public long getEmpNo() {... } (but not in both places – you must consistently use one variant) IS Course Info8
JPA Entity annotations To mark a class as a persistent entity (i.e. that objects of the class can be stored in a database), use annotation: import public class Employee {... } JPA will attempt to use the classname as a table name, and the attribute names as column names IS Course Info9
JPA Id annotations annotations is used to specify the primary key. annotation can be used to specify automatic generation of the primary key values import public class private long empNo;... } IS Course Info10
Interacting with the database To interact with the database we use an instance of EntityManager The EntityManager manages a set of entities (roughly the contents of a specific database). This set is called a persistence context. The EntityManager has methods for adding objects to the managed set (persisting), and for retrieving objects from it. Once an object is managed by the EntityManager any changes to it will automatically be saved to the database when the associated transaction ends. IS Course Info11
Getting the EntityManagerFactory and EntityManager We use annotation to get an EntityManagerFactory Then we call createEntityManager() to get the EntityManagerFactory emf ; EntityManager em = emf.createEntityManager(); IS Course Info12
Persisting objects New entities are persisted using the persist() method. EntityManager em; Employee emp = new Employee("Kongen", ); em.persist(emp); IS Course Info13
Retrieving objects using primary key Objects can be looked up by primary key with the find() method: EntityManager em;... Employee emp = em.find(Employee.class, 3); The parameters are the class of object to look for, and the object’s primary key. find() returns null if there is no object of the specified class with the specified primary key IS Course Info14