This document provides an overview of Java persistence concepts including multitier application architecture, the Java Persistence API, transaction management, entity relationships and mappings, queries, and integration with web frameworks. It describes architectural layers, transaction handling, entity manager methods, JPQL queries, and a sample application with DAOs and domain models.
This document provides an overview of Java persistence concepts including multitier application architecture, the Java Persistence API, transaction management, entity relationships and mappings, queries, and integration with web frameworks. It describes architectural layers, transaction handling, entity manager methods, JPQL queries, and a sample application with DAOs and domain models.
This document provides an overview of Java persistence concepts including multitier application architecture, the Java Persistence API, transaction management, entity relationships and mappings, queries, and integration with web frameworks. It describes architectural layers, transaction handling, entity manager methods, JPQL queries, and a sample application with DAOs and domain models.
This document provides an overview of Java persistence concepts including multitier application architecture, the Java Persistence API, transaction management, entity relationships and mappings, queries, and integration with web frameworks. It describes architectural layers, transaction handling, entity manager methods, JPQL queries, and a sample application with DAOs and domain models.
Java Persistence API Revisited Transaction Management EntityManager Detailed JPA Queries Added to DAO Cooperation with the Web Tier Page 1 Lufthansa Systems Hungria Kft - Budapest Multitier Application Architecture composite structure Sample III Web Container JNDI HTTP Servlet1 HTTPServl et Sessi on1 Sessi on2 EJB Container JNDI RMI/IIOP Servlet2 HTTPServl et Sessi on2 Sessi on3 S Bean1 Sessi on1 E 1 S Bean2 Sessi on2 E 1 E 2 S Bean3 Sessi on3 E 2 Entity1 E/H 1 JPA Entity2 E/H 2 JPA ORM Implementation JPA JDBC Relational Database Management System JAVA IF JDBC HTTP Client HTTP RMI Client RMI/IIOP Page 2 Lufthansa Systems Hungria Kft - Budapest Java Persistence API Description POJO (Plain Old Java Object) based standard Defines Object Relational Mapping for data persistence (entities) Use Java 5 annotations for persistence metadata Features Java Persistence API is part of EJB 3.0 can also integrate with J2SE applications Can use different persistence providers (Hibernate, Toplink, iBatis) Declarative transaction management Object Relational Mapping Transformation of the data between the class objects and (relational) databases Supported by automated tools (Object Model <-> DB model) Objects are transformed to records (entities), properties are mapped to fields, identity JPQL for Defining Queries Queries are formulated in term of objects and properties (relations) Parameter passing by name and by position Named and dynamic queries Updates are handed on result set Page 3 Lufthansa Systems Hungria Kft - Budapest Transaction Management Transaction Management Features Provides a consistent programming model across different transaction APIs (JTA, JDBC, Hibernate, JPA) Supports declarative transaction management Provides a simpler API for programmatic transaction management than JTA Rollback on exception, rollback rules can be defined Atomicity, Consistency, Isolation, Durability Isolation Level Definitions READ_UNCOMMITTED - dirty reads, non-repeatable reads and phantom reads can occur READ_COMMITTED - dirty reads are prevented REPEATABLE_READ - dirty reads and non-repeatable reads are prevented SERIALIZABLE - dirty reads, non-repeatable reads and phantom reads are prevented Transaction Propagation REQUIRED - support a current transaction, create a new one if none exists - DEFAULT MANDATORY - support a current transaction, throw an exception if none exists NESTED - subtransaction can be rolled back, behave like PROPAGATION_REQUIRED else NEVER - execute non-transactionally, throw an exception if a transaction exists NOT_SUPPORTED - execute non-transactionally, suspend the current transaction if one exists REQUIRES_NEW-create a new transaction, suspend the current transaction if one exists SUPPORTS - support a current transaction, execute non-transactionally if none exists Page 4 Lufthansa Systems Hungria Kft - Budapest Declarative vs. Programmatic Transaction Management Declarative @Transactional(rollbackFor=ApplicationException.class) public void processData { ... if( problem ) { throw new ApplicationException(It went wrong) ; // Rolled back } } Programmatic public void processData { EntityTransaction userTransaction = entityManager.getTransaction(); try{ userTransaction.begin(); // If everthing goes well, make a commit here. userTransaction.commit(); } catch(Exception exception) { // Exception has occurred, roll-back the transaction. userTransaction.rollback(); } } Page 5 Lufthansa Systems Hungria Kft - Budapest EntityManager find() vs. getReference() find() MobileEntity mobile = entityManager.find(MobileEntity.class, ABC-123); If (mobile != null) { // mobile object may or may not be null // Process the object } else { // Do something when ABC-123 not exist } getReference() try { MobileEntity mobile = entityManager.getReference(MobileEntity.class, ABC-123); // mobile object may not contain the actual state values for model, manufacturer // and imei number, the states may be loaded during the first access. String model = mobile.getModel(); // The persistence engine may fetch the model value for the mobile here // at this particular point of time ... } catch(EntityNotFoundException ex) { // Do something when ABC-123 not exist } Page 6 Lufthansa Systems Hungria Kft - Budapest EntityManager merge(), flush() and refresh() merge() MobileEntity mobile = entityManager.find(MobileEntity.class, ABC-123); // Transaction ends. mobile.set() // Updating the mobile object.
// New transaction begins
entityManager.merge(mobile); // The values in the object are merged into database state flush() MobileEntity mobile = mobile.set(); // Update the state values for the mobile object. entityManager.flush(); // Calling this flush method will synchronize the database with the values of the entity object. refresh() MobileEntity mobile = mobile.set(); // The state values for the mobile object is updated. entityManager.refresh(); // The refresh() method will refresh the entity object with the values taken from the database. // All the updates that are done are lost. Page 7 Lufthansa Systems Hungria Kft - Budapest Java Persistence Query Language (JPQL) General Form SELECT [<result>] [FROM <candidate-class(es)>] [WHERE <filter>] [GROUP BY <grouping>] [HAVING <having>] [ORDER BY <ordering>] DELETE FROM [<candidate-class>] [WHERE <filter>] UPDATE [<candidate-class>] SET item1=value1, item2=value2 [WHERE <filter>] Named Parameters Query q = em.createQuery("SELECT p FROM Person p WHERE p.lastName = :surname AND p.firstName = :forename"); q.setParameter("surname", theSurname); q.setParameter("forename", theForename); Numbered Parameters Query q = em.createQuery("SELECT p FROM Person p WHERE p.lastName = ?1 AND p.firstName = ?2"); q.setParameter(1, theSurname); q.setParameter(2, theForename); Range of Results Query q = em.createQuery("SELECT p FROM Person p WHERE p.age>20"); q.setFirstResult(20); q.setMaxResults(10); Page 8 Lufthansa Systems Hungria Kft - Budapest Finding Entities in the Database Named Query @NamedQuery( name="findAllCustomersWithName", query=select c from Customer c where c.name like :custName" ) Query query = em.createNamedQuery("findAllCustomersWithName"); List<Customer> customers = query.setParameter("custName", "Smith") .getResultList(); Dynamic Query Query query = em.createNamedQuery(select c from Customer c where c.name like :custName" ); List<Customer> customers = query.setParameter("custName", "Smith") .getResultList(); or Query query = em.createNamedQuery(select c from Customer c where c.name like :custName" ); Customer customer = query.setParameter("custName", "Smith") .getSingleResult(); Updating Queried Entities query.setFlushMode(FlushModeType.COMMIT); customer.setAddress(3. Dirty street); Page 9 Lufthansa Systems Hungria Kft - Budapest Direct Integration with Struts In struts-config.xml <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation value="/WEB-INF/training-servlet.xml/> </plug-in> Struts action class public class ListCourseAction extends ActionSupport { public ActionForward execute( ... ) { ApplicationContext context = getWebApplicationContext(); CourseService courseService = (CourseService) context.getBean("courseService"); Set allCourses = courseService.getAllCourses(); ... } } In training-servlet.xml <bean id=courseService class=com.myapp.CourseService/> Page 10 Lufthansa Systems Hungria Kft - Budapest Sample Application Session and DAOs class session E:extends DataEntity<K> K:extends Serializable AbstractDao - cl azz: Cl ass<E> # em: Enti tyManager + AbstractDao(Cl ass<E>) + fi ndById(K) : E + fi ndByNamedParams(Stri ng, Map<Stri ng, Obj ect>) : Li st<E> + remove(E) : voi d + save(E) : voi d + merge(E) : E + refresh(E) : voi d + rel oad(E) : E + fl ush() : voi d ReservationDao + Reservati onDao() + fi ndByLastName(Stri ng) : Li st<Reservati on> PassengerDao + PassengerDao() session::ReservationSession - reservati onDao: Reservati onDao - segmentDao: SegmentDao + Reservati onSessi on() + getReservati onDao() : Reservati onDao + setReservati onDao(Reservati onDao) : voi d + getSegmentDao() : SegmentDao + setSegmentDao(SegmentDao) : voi d + newReservati on(Reservati on) : voi d + getReservati on(l ong) : Reservati on + addSegment(Reservati on, Segment) : voi d + del eteSegment(Reservati on, Segment) : voi d + fi ndByLastName(Stri ng) : Li st<Reservati on> SegmentDao + SegmentDao() -segmentDao -reservati onDao Page 11 Lufthansa Systems Hungria Kft - Budapest Sample Application Modified Data Model class data Passenger - i d: Long - ti tl e: Ti tl e - l astName: Stri ng - fi rstName: Stri ng + Passenger() + getId() : Long + setId(Long) : voi d + getTi tl e() : Ti tl e + setTi tl e(Ti tl e) : voi d + getLastName() : Stri ng + setLastName(Stri ng) : voi d + getFi rstName() : Stri ng + setFi rstName(Stri ng) : voi d + toStri ng() : Stri ng Reservation - i d: Long - ai rl i ne: Stri ng - passenger: Passenger - segments: Set<Segment> + Reservati on() + getId() : Long + setId(Long) : voi d + getAi rl i ne() : Stri ng + setAi rl i ne(Stri ng) : voi d + getPassenger() : Passenger + setPassenger(Passenger) : voi d + getSegments() : Set<Segment> + setSegments(Set<Segment>) : voi d + addSegment(Segment) : voi d + del eteSegment(Segment) : voi d + toStri ng() : Stri ng Segment - i d: Long - reservati on: Reservati on - departure: Date - fl i ght: Stri ng - booki ngCl ass: Booki ngCl ass + Segment() + getId() : Long + setId(Long) : voi d + getDeparture() : Date + setDeparture(Date) : voi d + getFl i ght() : Stri ng + setFl i ght(Stri ng) : voi d + getBooki ngCl ass() : Booki ngCl ass + setBooki ngCl ass(Booki ngCl ass) : voi d + getReservati on() : Reservati on + setReservati on(Reservati on) : voi d + toStri ng() : Stri ng 0..* -passenger 1 0..* - segments 1