Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.

Similar presentations


Presentation on theme: "CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren."— Presentation transcript:

1 CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren

2 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ş

3 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ş

4 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ş

5 JPA – Entities and Metadata JPA maps java objects to a database using metadata JPA managed java objects are called as Entities, marked with @Entity annotation. Metadata can also be defined in a XML file. CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

6 JPA – Entities and Metadata JPA maps java objects to a database using metadata JPA managed java objects are called as Entities, marked with @Entity 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ş

7 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ş

8 JPA – Annotations @Entity : Define classes that will map to database @Id : Each entity should have to define the primary key in the database. @Column: Optional annotation is used to define the name of the column name and other properties of the column on database @Transient: To declare a field to not persist CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

9 JPA – Entity Example @Entity public class User { @Id private int id; @Column private String username; @Column private String email; … // getters and setters } CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

10 JPA – Entity Relationships Unidirectional or Bidirectional @OneToMany @ManyToOne @ManyToMany @OneToOne CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

11 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, EAGER) @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) List users; CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

12 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ş

13 JPA – Entitiy Manager @PersistenceContext private EntityManager em; @Resource 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ş

14 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.


Download ppt "CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren."

Similar presentations


Ads by Google