14
CSE446 SOFTWARE QUALITY MANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren

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

Embed Size (px)

Citation preview

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

CSE446 SOFTWARE QUALITY MANAGEMENT

Spring 2014

Yazılım ve Uyguluma Geliştirme Yöneticisi

Orhan Başar Evren

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

CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

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

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

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

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

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

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

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 with @Entity annotation.

•Metadata can also be defined in a XML file.

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

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 with @Entity annotation.•Metadata can also be defined in a XML file.• Entity manager is used to perform CRUD

operations on an entity

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

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.

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

CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

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

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

CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

JPA – Entity Example@Entitypublic class User {

@Idprivate int id;

@Columnprivate String username;

@Columnprivate String email;

… // getters and setters}

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

CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

JPA – Entity Relationships

• Unidirectional or Bidirectional

•@OneToMany•@ManyToOne•@ManyToMany•@OneToOne

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

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, EAGER)

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)List<User> users;

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

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)

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

CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

JPA – Entitiy Manager@PersistenceContextprivate EntityManager em;

@Resourceprivate UserTransaction utx;

User user = new User(); List<User> users;

public void save() { utx.begin(); em.persist(user); utx.commit();}

public List<User> findAll() { users = em.createQuery("SELECT u FROM User u").getResultList();}

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

CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

JPA – Entity Life Cycle• 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.