27 Jul

JPA Criteria API

This tutorial will show you how to use JPA(Java Persistence API) Criteria Queries for selecting data from database.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The Criteria API is used to define queries for entities and their persistent state by creating query-defining objects. Criteria queries are written using Java programming language APIs, are typesafe, and are portable. Such queries work regardless of the underlying data store. The major advantage of Criteria API is that errors can be detected earlier during the compile time. String-based JPQL(Java Persistence Query Language) queries and JPA criteria based queries are same in performance and efficiency.

Steps to create Criteria Query

1. EntityManager instance is used to create a CriteriaBuilder object.
2. CriteriaQuery instance is used to create a query object from CriteriaBuilder object. The attributes of this object will be modified with the details of the query.
3. CriteriaQuery.form method is called to set the Root of the query.
4. CriteriaQuery.select is called to set the result list type for a particular POJO class mapped to a database table.
5. TypedQuery<T> instance is used to prepare a query for execution and specifying the type of the query result.
6. getResultList method is used on the TypedQuery<T> object for executing a query to return the result list of a collection of entities.

Read More

26 Jul

Many to Many Mapping in JPA

In this example I am going to show you how we can implement Many to Many(@ManyToMany) relationship using JPA persistence API in Java.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The JPA is used to reduce the burden of writing codes for relational object management, a programmer or developer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. The JPA being a Java framework a programmer also needs not to worry about the SQL part of the code, he/she may need to hardly concentrate on SQL coding.

You may be interested in Many to One Mapping in JPA , One to Many Mapping in JPA and One to One Mapping in JPA

Many-To-Many relationship is where one or more rows from one entity are associated with more than one rows in another entity.

In this Many to Many mapping example, we have Teacher and Period with multiple relationships.

Read More

25 Jul

One to One Mapping in JPA

In this example I am going to show you how we can implement One to One(@OneToOne) relationship using JPA persistence API in Java.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The JPA is used to reduce the burden of writing codes for relational object management, a programmer or developer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. The JPA being a Java framework a programmer also needs not to worry about the SQL part of the code, he/she may need to hardly concentrate on SQL coding.

You may be interested in Many to One Mapping in JPA , Many to Many Mapping in JPA and One to Many Mapping in JPA

In One-To-One relationship, one item can be linked to only one other item. It means each row of one entity is referred to one and only one row of another entity.

In this One to One mapping example, we have Teacher and Period relationship. A class period can be taken by only one teacher.

Read More

24 Jul

One to Many Mapping in JPA

In this example I am going to show you how we can implement One to Many(@OneToMany) relationship using JPA persistence API in Java.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The JPA is used to reduce the burden of writing codes for relational object management, a programmer or developer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. The JPA being a Java framework a programmer also needs not to worry about the SQL part of the code, he/she may need to hardly concentrate on SQL coding.

You may be interested in Many to One Mapping in JPA , Many to Many Mapping in JPA and One to One Mapping in JPA

In this One to Many mapping example, we have Teacher and Period relationship. A class period can be taken by multiple teachers at different times.

Read More

23 Jul

Many to One Mapping in JPA

In this example I am going to show you how we can implement Many to One(@ManyToOne) relationship using JPA persistence API in Java.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The JPA is used to reduce the burden of writing codes for relational object management, a programmer or developer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. The JPA being a Java framework a programmer also needs not to worry about the SQL part of the code, he/she may need to hardly concentrate on SQL coding.

You may be interested in One to Many Mapping in JPA , Many to Many Mapping in JPA and One to One Mapping in JPA

In this Many to One mapping example, we have Teacher and Period relationship. Many teachers can be assigned to a class period or in the other words, a class period can be taken by multiple teachers at different times.

Read More

21 Jul

JPA Inheritance Strategy : Table Per Concrete Class Hierarchy (TABLE_PER_CLASS)

JPA comes with a provision to create tables and populate them as per the Java classes involved in inheritance. JPA offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables.

Hibernate inheritance example

Observe, in the above hierarchy, three classes are involved where Person is the super class and Student and Teacher are sub-classes with their own properties declared as instance variables. Now the question is how many tables are required and moreover how to link the tables so that Student gets three properties of id and name(from super class), year.

You may read also JPA Inheritance Strategy : Table Per Class Hierarchy (SINGLE_TABLE)and JPA Inheritance Strategy : Table Per Sub-Class Hierarchy (JOINED) and Inheritance Strategy in Hibernate

JPA support three types of inheritance strategies: SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

I am going to give explanation and example on JPA table per concrete class hierarchy (TABLE_PER_CLASS), consider we have base class named Person and two derived classes – Student and Teacher.

In Table Per Concrete Class Hierarchy will have the number of tables in the database equals to the number of derived classes. Once we save the derived class object, then derived class data and base class data will be saved in the derived class related table in the database. We need the tables only for derived classes.

Read More

20 Jul

JPA Inheritance Strategy : Table Per Sub-Class Hierarchy (JOINED)

JPA comes with a provision to create tables and populate them as per the Java classes involved in inheritance. JPA offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables.

Hibernate inheritance example

Observe, in the above hierarchy, three classes are involved where Person is the super class and Student and Teacher are sub-classes with their own properties declared as instance variables. Now the question is how many tables are required and moreover how to link the tables so that Student gets three properties of id and name(from super class), year.

You may read also JPA Inheritance Strategy : Table Per Class Hierarchy (SINGLE_TABLE)and JPA Inheritance Strategy : Table Per Concrete Class Hierarchy (TABLE_PER_CLASS) and Inheritance Strategy in Hibernate

JPA support three types of inheritance strategies: SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

I am going to give explanation and example on JPA table per sub-class hierarchy (JOINED), consider we have base class named Person and two derived classes – Student and Teacher

In Table Per SubClass Hierarchy there will be the number of classes equals to the number of tables in the database.
If we save the Student class object, hibernate will first save the data related to super class object into the super class related table in the database and then Student object data in Student related table in the database.

Read More

19 Jul

JPA Inheritance Strategy : Table Per Class Hierarchy (SINGLE_TABLE)

JPA comes with a provision to create tables and populate them as per the Java classes involved in inheritance. JPA offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables.

Hibernate inheritance example

Observe, in the above hierarchy, three classes are involved where Person is the super class and Student and Teacher are sub-classes with their own properties declared as instance variables. Now the question is how many tables are required and moreover how to link the tables so that Student gets three properties of id and name(from super class), year.

You may read also JPA Inheritance Strategy : Table Per Concrete Class Hierarchy (TABLE_PER_CLASS) and JPA Inheritance Strategy : Table Per Sub-Class Hierarchy (JOINED) and Inheritance Strategy in Hibernate

JPA support three types of inheritance strategies: SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

I am going to give explanation and example on JPA table per class hierarchy (SINGLE_TABLE), consider we have base class named Person and two derived classes – Student and Teacher

If we save the derived class object like Student or Teacher then automatically Person class object will also be saved into the database, and in the database all the data will be stored into a single table only, which is base class table for sure.

Read More

17 Jul

JPA CRUD Example

This tutorial will show you how to use JPA(Java Persistence API) for selecting, inserting, updating and deleting data into/from database.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The JPA is used to reduce the burden of writing codes for relational object management, a programmer or developer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. The JPA being a Java framework a programmer also needs not to worry about the SQL part of the code, he/she may need to hardly concentrate on SQL coding.

In EJB 3.0 the persistence layer was separated and defined as a replacement of EJB 2 CMP Entity Beans specification.

Being JPA an open source API, various enterprise vendors such as Oracle, Redhat, Eclipse, etc. provide new products by adding the JPA persistence flavor in them. Some of these products include Hibernate, Eclipselink, Toplink, Spring Data JPA.

Read More

Share