Java Persistence API (JPA) is a collection of classes and methods to persistently store the vast amounts of data into a database which is provided by the Oracle Corporation. JPA is used for mapping Java objects to the tables of relational databases that is stable and experienced in many companies. It has Java Persistence Query Language (JPQL) that support different database dialects and allows the application has not changed code when changing database.
- JPA is a specification for which several implementations exist. NetBeans supports several JPA implementations including Toplink, Hibernate, KODO, and OpenJPA.
=> Hibernate can implement JPA api OR uses its own language (for example, HQL, Criteria)
Can Hibernate implements JDO ? Currently talking to JDO is as an ORM that is independent with hibernate. Hibernate used for RDBMS while JDO is for object DBMS.
- JDO and JPA are both specifications, not implementations
- The idea is you can swap JPA implementations, if you restrict your code to use standard JPA only
- Hibernate can be used as one such implementation of JPA.
- However, Hibernate provides a native API, with features above and beyond that of JPA.
There are two standard API's for persistence - Java Data Objects (JDO) and Java Persistence API (JPA). The former (JDO) is designed for all datastores, and the latter is designed for RDBMS datastores only. When choosing the API to use in your application you should bear the following factors in mind.
- If you are familiar with Hibernate, you might think of the JPA mappedBy as the equivalent of the Hibernate inverse attribute, which indicates which end of a relationship should be ignored.Progress of JPA development something like:
- JDBC
- Data Mappers (MyBatis, ..)
- EJB
- JDO
- JPA (1.0 ~ EJB3.0, 2.0 went final in 2009, 2.1 in 2013, JPA 2.2 (and EJB 3.2) in June 2017)
The JPA consists of four areas:
- The Java Persistence API
- The Java Persistence Criteria API
- The Query language
- Object-relational mapping metadata
JPA relational mapping
There are 4 types of relations:
- OneToOne
- ManyToOne
- OneToMany
- ManyToMany
uni/bidirectional Relations, (eagerly) or on-demand (lazily)
Every relationship has two sides:
- The owning side is responsible for propagating the update of the relationship to the database. Usually this is the side with the foreign key.
- The inverse side maps to the owning side.
In the one-to-one mapping in the example application, the Invoice object is the owning side. Listing 4 demonstrates what the inverse side -- the Order -- looks like.
JPA - Queries
- Native queries (native SQL)
- Java Persistence Query Language (object oriented version of SQL): NamedQuery /NamedQueries, TypedQuery, Query
- Using the Criteria API
Criteria API is more dynamic and strongly typed.
JPA Inheritance
There are 3 strategies to map inheritance:
- Single Table (default) - SINGLE_TABLE
- Table per concrete class - TABLE_PER_CLASS
- Joined Table - JOINE
SINGLE_TABLE: This strategy maps all classes to one single table so that a query will never has to use a join, leading to be increase performance. The disadvantage is the redundant stored data.
JOINED: In this strategy, corresponding to each type will create a separate table but it not duplicate shared data accepting primary key. We have to do joining queries in using which can decrease query speed significantly.
TABLE_PER_CLASS: the same as JOINED strategy BUT this strategy creates a separate table for each entity type. There is no need to use join queries. The share of inheritance hierarchy data will be duplicate stored in each table.
In Google App Engine, JOINED&SINGLE_TABLE strategies are not supported. The "TABLE_PER_CLASS" and "MAPPED_SUPERCLASS" strategies work as described in the DataNucleus documentation.
In JOINED strategy of normal Java EE invironment, it should add @Inheritance(strategy = InheritanceType.JOINED) into super entity class.
Hibernatge Dialect Property & Collection:
- Dialect Property: http://www.tutorialspoint.com/hibernate/hibernate_configuration.htm
- Embedded objects: http://www.thejavageek.com/2014/05/01/jpa-embeddedid-example/
- ElementCollection: https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection
LINKS:
- Database Connection using JPA
- Hibernate-JPA presentations
- Java Persistence API JPA Exam Certification 1z0-898
- JPA find() vs. getReference()
0 comments:
Post a Comment