Top Banner
Entity Persistence with JPA [email protected]
19
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Entity Persistence with JPA

Entity Persistence with [email protected]

Page 2: Entity Persistence with JPA

What’s Inside

• JPA• JPA Architecture• Entity• Object/Relational Mapping• Entity Relationship• Persistence Unit • Persistence Context• Entity Manager• Entity Manager API • Query API• Annotations• Transactions• Next Steps

Page 3: Entity Persistence with JPA

JPA

What JPA is just a specification from Sun, which is released under JEE 5

specification. A feature-rich implementation of the persistence part of Enterprise

Java Beans 3.0. JPA defines an interface to perform CRUD operations between POJO's and a data store.

Why JPA is not a new technology; rather, it has collected the best ideas

from existing persistence technologies like Hibernate, TopLink etc . The result is a standardized specification that helps you build a persistence layer that is independent of any particular persistence provider.

Page 4: Entity Persistence with JPA

JPA Architecture

Page 5: Entity Persistence with JPA

Entity

• Lightweight persistent domain object – the thing you persist

• Restrictions must have a public or protected no-arg constructor cannot be final cannot have final methods or final instance variables that are to be

persisted can be abstract or concrete class must have a primary key Example (Entity)

Page 6: Entity Persistence with JPA

Object/Relational Mapping

Page 7: Entity Persistence with JPA

Entity Relationship

• Defined by relationships in the database schema ManyToOne OneToOne OneToMany ManyToMany

Page 8: Entity Persistence with JPA

Entity Relationship

Page 9: Entity Persistence with JPA

Persistence Unit The set of all classes mapped to a single database for the application Defined in META-INF/persistence.xml An application can have multiple persistence units

<?xml version="1.0" encoding="UTF-8"?><persistence version="1.0"xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name=“subin-persistence-unit“ transaction-type="JTA"><jta-data-source>jndi/jdbc/traceds</jta-data-source></persistence-unit><persistence-unit name=“subin-persistence-unit-test“ transaction-type="RESOURCE_LOCAL"><provider>org.hibernate.ejb.HibernatePersistence</provider>

<class>com.subin.entity.HistoryBean</class><!-- <class>com.subin.entity.ServiceBean</class><class>com.subin.entity.LookupBean</class> --><properties><property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /><property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /><property name="hibernate.show_sql" value="true" /><property name="hibernate.format.sql" value="true" /><property name="hibernate.connection.username" value="root" /><property name="hibernate.connection.password" value="root" /><property name="hibernate.connection.url" value="jdbc:mysql://192.168.160.107/testdb" /></properties></persistence-unit> </persistence>

Page 10: Entity Persistence with JPA

Persistence Context Abstraction representing a set of “managed” entity instances

Entities keyed by their persistent identity Only one entity with a given persistent identity may exist in the PC Entities are added to the PC, but are not individually removable

(“detached”) Controlled and managed by EntityManager

Contents of PC change as a result of operations on EntityManager API

“local working copy” of persistent objects

Page 11: Entity Persistence with JPA

Persistence ContextApplication Persistence

Context

Entities

MyEntity A

MyEntity B

MyEntity C

MyEntity a

EntityManager

MyEntity b

Entity state

Page 12: Entity Persistence with JPA

Entity Manager API for interacting with the Entity Can think of it as a proxy to a persistence context

May access multiple different persistence contexts throughout its lifetime

The EntityManagerFactory is used to create an instance of EntityManager.

Example (Persistence Context,EntityManagerFactory,EntityManager)

Page 13: Entity Persistence with JPA

Entity Manager API persist()- Insert the state of an entity into the db remove()- Delete the entity state from the db refresh()- Reload the entity state from the db merge()- Synchronize the state of detached entity with the pc find()- Execute a simple PK query createQuery()- Create query instance using dynamic JP QL createNamedQuery()- Create instance for a predefined query createNativeQuery()- Create instance for an SQL query contains()- Determine if entity is managed by pc flush()- Force synchronization of pc to database

Page 14: Entity Persistence with JPA

Query API getResultList() – execute query returning multiple results getSingleResult() – execute query returning single result executeUpdate() – execute bulk update or delete setFirstResult() – set the first result to retrieve setMaxResults() – set the maximum number of results to retrieve setParameter() – bind a value to a named or positional parameter setHint() – apply a vendor-specific hint to the query setFlushMode()– apply a flush mode to the query when it gets run

Page 15: Entity Persistence with JPA

Annotations

Core @Entity @Table @Id @Basic @Column @Transient @Enumerated @Temporal

Relationships @ManyToOne @OneToOne @OneToMany @JoinColumn

Page 16: Entity Persistence with JPA

TransactionsTwo types

Default to JTA in a Java EE environment Default to RESOURCE_LOCAL in a Java SE environment

@TransactionAttribute Annotation

TransactionAttributeType.REQUIRED TransactionAttributeType.REQUIRES_NEW TransactionAttributeType.MANDATORY TransactionAttributeType.NOT_SUPPORTED TransactionAttributeType.NEVER TransactionAttributeType.SUPPORTS

How can we specify

Page 17: Entity Persistence with JPA

Next Steps Dynamic quries Named quries JPA Queries and JPQL (http://www.objectdb.com/java/jpa/query)

Page 18: Entity Persistence with JPA

References

Entity

http://www.javabeat.net/articles/5-introduction-to-java-persistence-apijpa-1.html

http://www.roseindia.net/jpa http://www.cs.bilkent.edu.tr/~ccelik/cs412/jpatransaction.ppt

http://www.javamug.org/mainpages/presentations/JPAtheEnterpriseStandard.ppt

Page 19: Entity Persistence with JPA

Thank You