1 / 14

CSE446 Software Quality Management

CSE446 Software Quality Management. Orhan Ba şar Evren. Spring 2014. Yazılım ve Uyguluma Geliştirme Yöneticisi. 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.

ulani
Download Presentation

CSE446 Software Quality Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE446 Software Quality Management Orhan Başar Evren Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi

  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<User> 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 EntityManagerem; @Resource private UserTransactionutx; 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(); } CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

  14. JPA – Entity Life Cycle • When instance of an entity class created it is in the new state. • Entity becomes managedwhen it is persisted with EntityManager. • On transaction commit, EntityManager stores the entity on database. CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş

More Related