140 likes | 355 Views
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.
E N D
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 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 with @Entity 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 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ş
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 – 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ş
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ş
JPA – Entity Relationships • Unidirectional or Bidirectional • @OneToMany • @ManyToOne • @ManyToMany • @OneToOne 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; 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 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ş
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ş