CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren
Today’s Overview – JPA : Java Persistence API What is JPA ? Benefits of JPA ? Entities and metadata JPA Annotations Entity Relationships Entity Manager JPA Life Cycle CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Java Persistence API The Java Persistence API (JPA) is an object- relational mapping (ORM) technology. JPA is used for automatically storing data contained in Java objects into a relational database. JPA is a specification. Followings are common JPA implementations from different vendors – EclipseLink (oracle TopLink) – Hibernate – OpenJPA CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Benefits POJO (Plain Old Java Object) Persistence Metadata-driven ORM No low-level JDBC/SQL Code No complex DAO (Data access objects) Managed transactions No vendor-specific code: any relational DB Data caching and performance optimization Available for Java SE, not just for EE JPQL : Java Persistence Query Language CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entities and Metadata JPA maps java objects to a database using metadata JPA managed java objects are called as Entities, marked annotation. Metadata can also be defined in a XML file. CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entities and Metadata JPA maps java objects to a database using metadata JPA managed java objects are called as Entities, marked annotation. Metadata can also be defined in a XML file. Entity manager is used to perform CRUD operations on an entity CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entity Class Entity classes are the model in MVC pattern. Class fields should be private and they should be accessed through getter and setter methods. Entity class should have no-argument constructor. Class fields can be primitive types, serializable class types or a collection. CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – : Define classes that will map to : Each entity should have to define the primary key in the Optional annotation is used to define the name of the column name and other properties of the column on To declare a field to not persist CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entity public class User private int private String private String ; … // getters and setters } CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entity Relationships Unidirectional CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Relationships Attributes cascade: specifies which operations to be propagated to the target relationship. (MERGE, PERSIST, REFRESH, REMOVE, ALL) fetch: specifies whether the target relation object will be fetched automatically or not (LAZY, fetch=FetchType.EAGER) List users; CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entitiy Manager PersistenceContext is the collection of managed entities EntitiyManager is the interface to access persistence context Entity beans are not managed by Enterprise container like JSF Managed Beans. They are managed by the Persistence Context Transaction is needed to modify data. (insert, update, delete) Transaction is not needed to retrieve data. (select) CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entitiy private EntityManager private UserTransaction utx; User user = new User(); List users; public void save() { utx.begin(); em.persist(user); utx.commit(); } public List findAll() { users = em.createQuery("SELECT u FROM User u").getResultList(); } CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JPA – Entity Life Cycle CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş When instance of an entity class created it is in the new state. Entity becomes managed when it is persisted with EntityManager. On transaction commit, EntityManager stores the entity on database.