Download presentation
Presentation is loading. Please wait.
Published byThomasine Fletcher Modified over 9 years ago
1
IS-907 Java EE Introduction to JPA
2
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 (even.larsen@uia.no) IS-102 Introduksjon2
3
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-202 - Course Info3
4
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 (even.larsen@uia.no) IS-102 Introduksjon4
5
Persistence Context a managed set of entity objects (a “session”) keeps track of changes accessed through EntityManager objects Even Åby Larsen (even.larsen@uia.no) IS-102 Introduksjon5
6
Transactions define when entities are synchronized with the database bound to a persistence context accessible through UserTransaction objects Even Åby Larsen (even.larsen@uia.no) IS-102 Introduksjon6
7
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-202 - Course Info7
8
JPA annotations Class annotations provide information about the class Class annotations appear before the class declaration (normally just before the public modifier) @Entity 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 public): @Id private long empNo; or @Id public long getEmpNo() {... } (but not in both places – you must consistently use one variant) IS-202 - Course Info8
9
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 the @Entity annotation: import javax.persistence.Entity; @Entity public class Employee {... } JPA will attempt to use the classname as a table name, and the attribute names as column names IS-202 - Course Info9
10
JPA Id annotations The @Id annotations is used to specify the primary key. The @GeneratedValue annotation can be used to specify automatic generation of the primary key values import javax.persistence.*; @Entity public class Employee { @Id @GeneratedValue private long empNo;... } IS-202 - Course Info10
11
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-202 - Course Info11
12
Getting the EntityManagerFactory and EntityManager We use the @PersistenceUnit annotation to get an EntityManagerFactory Then we call createEntityManager() to get the EntityManager @PersistenceUnit EntityManagerFactory emf ; EntityManager em = emf.createEntityManager(); IS-202 - Course Info12
13
Persisting objects New entities are persisted using the persist() method. EntityManager em; Employee emp = new Employee("Kongen", 5000000); em.persist(emp); IS-202 - Course Info13
14
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-202 - Course Info14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.