Intro to JPA 1.0

download Intro to JPA 1.0

of 20

Transcript of Intro to JPA 1.0

  • 8/14/2019 Intro to JPA 1.0

    1/20

    Introduction to the Java Persistence

    API (JPA) 1.0

    Edem Morny

    Genkey Africa Ltd

    http://edemmorny.wordpress.com

  • 8/14/2019 Intro to JPA 1.0

    2/20

    What are we discussing?

    Justification and history of Java O/R Mapping JPA and current implementations

    The Persistence Unit

    Entity

    Mapping relationships

    The EntityManager

    JPA QL

    Cascades

    Etc

  • 8/14/2019 Intro to JPA 1.0

    3/20

    Justification and History of O/RMapping

    Imagine trying to design the underlying model for aschool.

    School

    Student Course etc.

    Imagine thinking in terms of classes and not in terms ofhow they map to a database structure.

    Object/Relational Mapping

    Hibernate, JDO,

    Recent convert : .Net Entit Framework

  • 8/14/2019 Intro to JPA 1.0

    4/20

    JPA 1.0 and Current Implementations

    JPA 1.0 released with JEE 5

    Heavily influenced by Hibernate

    Current JPA Persistence Providers are

    Hibernate(JBoss)

    Ibatis

    TopLink(Oracle)

    EclipseLink (IBM)

  • 8/14/2019 Intro to JPA 1.0

    5/20

    The Persistence Unit

    org.hibernate.ejb.HibernatePersistence

    org.jaccra.jpaexample.School

    org.jaccra.jpaexample.Student

    Persistence

    provider

    Entities

    Db

    configuration

    details etc

  • 8/14/2019 Intro to JPA 1.0

    6/20

    JPA Managed Entity

    @Entity

    public class School implements Serializable {

    private Long id;private String name;

    private String address;

    public String getId()

    public void setId(Long id) ...

    Annotations

    JPA

    recomnended

    Follows

    JavaBean

    convention

  • 8/14/2019 Intro to JPA 1.0

    7/20

    JPA Managed Entity - Annotations

    @Entity@Table(name=skool)

    public class School implements Serializable {

    private Long id;

    private String name;

    @Id@GeneratedValue(strategy = GenerationType.AUTO)

    public String getId()

    @Column(name=first_name,nullable=false,length=20)

    ublic Strin etName ...

  • 8/14/2019 Intro to JPA 1.0

    8/20

    Mapping Relationships

    Supports definition of RDBMS relationships and isexpressed through annotations

    Single-valued :@OneToOne

    Multi-valued @ManyToOne

    @ManyToMany

    Supports both join tables and non-join table

    Expressed through any Collection i.e. Set or List, aswell as using a Map.

  • 8/14/2019 Intro to JPA 1.0

    9/20

    Mapping Relationships - Annotations@Entity

    public class Student implements Serializable {

    private School school;

    @ManyToOne

    @JoinColumn(nullable=false)

    public School getSchool() {return school;

    }

    ...

  • 8/14/2019 Intro to JPA 1.0

    10/20

    Mapping Relationships - Annotations

    @Entity

    public class School implements Serializable {

    private List students;

    @OneToMany(mappedBy = "school")

    public List getStudents() {

    return students;}

    ...

  • 8/14/2019 Intro to JPA 1.0

    11/20

    The EntityManager

    Liason between your objects and the persistencelayer.

    It enables us to in a safe way

    Save new objects (persist)

    Update existing objects (merge)

    Delete persistent object (remove)

    Find persistent objects (find) Query persistent object for single objects,

    Collections and scalar results.

    etc.

  • 8/14/2019 Intro to JPA 1.0

    12/20

    The EnityManager -Continued

    EntityManagerFactory emf =Persistence.createEntityManagerFactory("jpaexamplePU");

    EntityManager em = emf.createEntityManager();

    EntityTransaction tx = em.getTransaction();

    try{

    School school = new School();school.setName("MEST");

    em.persist(school);

    tx.commit();

    }catch....

    School school = em.find(School.class,Long(1));

    System.out.println('The name of the school is: '+school.getName());

    To get an

    EM, you

    need to ask

    from the

    factory

    Start a

    transaction

    on the EM

    Commit the

    transaction

    Find objects

  • 8/14/2019 Intro to JPA 1.0

    13/20

    Cascades

    Defines actions to be taken when changes are madeto objects related to each other

    Cascade types

    ALL, MERGE,PERSIST,REMOVE,REFRESH

    @OneToMany(mappedBy = "school", cascade =CascadeType.ALL)

    public List getStudents() {

    In the above cascade, if any school object is deleted,all it's orphaned students will be deleted.

  • 8/14/2019 Intro to JPA 1.0

    14/20

    Query your model JPA QL

    Enables us to express in object terms what we wantto fetch from persistence.

    Allows the passing of parameters to suchexpressions.

    Provides optimisation of queries through preloadingNamedQueries.

    Enables us to fetch an object, a Collection or scalar

    results as well as all types of joins.

  • 8/14/2019 Intro to JPA 1.0

    15/20

    JPA QL - Continued Simple query to select a school by name 'MEST'

    Query q = em.createQuery("Select s from School s where s.name = 'MEST'") Same query with parameters

    Query q = em.createQuery("Select s from School s where s.name =:name").setParameter("name", "MEST");

    Expecting only one result?School school = (School) q.getSingleResult();

    Or a collection?

    List schools = q.getResultList();

    Or a scalar result

    Long count = (Long) em.createQuery("Select count(s) from Schools).getSingleResult();

  • 8/14/2019 Intro to JPA 1.0

    16/20

    JPA QL - Continued

    Projection Returns a list containing Object[]

    Select s from School s , st from Student st

    Joins

    Select s from School s join s.students st where st.name='Francis';

    Subselects

    Advanced querying: Group by, order by etc,

    Etc

  • 8/14/2019 Intro to JPA 1.0

    17/20

    JPA Other Features

    Embeddable classes

    Inheritance and Polymorphism

    Override annotations with XML

    Native SQL invocation

    Locking : Optimistic and Persismistic

  • 8/14/2019 Intro to JPA 1.0

    18/20

    The Future - JPA 2.0 (JSR 317)

    Currently at Proposed Final Draft stage Features proposed in specification

    Metamodel API (for spec implementors)

    Integration with Bean Validation (JSR 303)

    Criteria Query API

    Many many more annotations

    @Access, @OrderBy, @MapKeyClass, @Cacheable

    And a whole bunch of improvements to JPA 1.0

  • 8/14/2019 Intro to JPA 1.0

    19/20

    Further references

    Enterprise JavaBeans, v. 3.0. Java Persistence API.

    JSR-307: JPA 2. http://jcp.org/en/jsr/detail?id=307

    JSR-303: Bean Validation.http://jcp.org/en/jsr/detail?id=303

    JDBC 4.0 Specification. http://java.sun.com/products/jdbc.

    Java Persistence with Hibernate Manning Publishers

    http://edemmorny.wordpress.com

    http://jcp.org/en/jsr/detail?id=307http://jcp.org/en/jsr/detail?id=303http://java.sun.com/products/jdbchttp://edemmorny.wordpress.com/http://edemmorny.wordpress.com/http://java.sun.com/products/jdbchttp://jcp.org/en/jsr/detail?id=303http://jcp.org/en/jsr/detail?id=307
  • 8/14/2019 Intro to JPA 1.0

    20/20