Register Login
Internet / AI Technology University (ITU/AITU)





|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 4. Web Apps Frameworks >> 4.4. Spring with Apache Maven and Data Service Frameworks >> 4.4.5. Data Service and JPA/Hibernate frameworks in Maven Project
Current Topic: 4.4.5.1. Using JPA/Hibernate
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Using JPA/Hibernate

JPA or Java Persistence API provides fundamental frameworks over relational data. The JPA follows Hibernate footsteps.

Hibernate was one of the first and the most popular library, which provided Object-Relational Mapping (ORM) to simplify code of handling relational data in Java.

Hibernate is currently part of JBoss offering, which in its turn is under the RedHat company umbrella.

Over the years most of the Hibernate features appeared in the mainstream Java as JPA.

Hibernate is especially beneficial when a programmer deals with One-To-One relationships.
A data table can be described as a class called entity.
This is the simplest Object-Relational Mapping (ORM), which allows a programmer to manipulate by Java classes, while JPA/Hibernate deals with the data tables behind the scene.

Here is the sample of such a class Account, describing a data table Accounts with the ID, Email, and Password fields.


@Entity
@Table(name="accounts")
public class Account implements Serializable {
private int id;
private String email;
private String password;
}

It is a bit more complicated to deal with One-To-Many relationships, for example, describing multiple record types, resulting from different combinations of WHERE clauses with many JOIN in SELECT statements.

JPA/Hibernate frameworks save tremendous time, which used to be consumed by describing the flavors of different database types.

This is done behind the scene by powerful classes like javax.persistence.EntityManager, org.hibernate.Session, javax.persistence.Query and more.

Here is the extract from the constructor of the JpaDataService class:

emf = Persistence.createEntityManagerFactory(jpaUnitName, properties); // using persistence.xml
em = emf.createEntityManager();
session = em.unwrap(Session.class);


Just a reminder: the JpaDataService class is a parent class of the DataBaseService, which is a parent class of the DataService.
The properties for the EntityManagerFactory are derived from the persistence.xml (default) and can be replaced by the DB parameters, if any, in the web.xml.

The JpaDataService class keeps a table of EntityManager instances mapped to their Data Source names. Each EntityManager instance is a Singletone, created once for all users.

Here is the method that returns the JpaDataService object with the EntityManager and the Hibernate Session instances for a specific data source name.

public static JpaDataService getJds(String appName, String jpaUnitName) {
if(tableOfJpaServices == null) {
tableOfJpaServices = new Hashtable();
}
JpaDataService jds = tableOfJpaServices.get(jpaUnitName);
if(jds == null) {
jds = new JpaDataService(appName, jpaUnitName);
jds.setDsName(appName+"/"+jpaUnitName);
tableOfJpaServices.put(jpaUnitName, jds);
}
return jds;
}
Was it clear so far? Highlight the text in question Or


The methods of the JpaDataService class support the main functions of the DataService APIs, such as:


int nRecords = DataService.setData(sql, dsName);

List records = DataService.getData(sql, dsName);

// using sqlName to get SQL
String sqlName = "updatePassword"; // update password set psw=? where userId=?
// create run-time parameters to update password
String[] runTimeParams = new String[] {psw, userId};
// update
int nRecords = DataService.setPrepDataBySqlName(sqlName, runTimeParams, dsName);

// retrieve data records with sqlName and run-time parameters in where clause
List records = DataService.getPrepDataBySqlName(sqlName, runTimeParams, dsName);


The DataService framework uses a powerful Hibernate Query object to retrieve and change data.
While changing data, any method calls a Transaction object providing thread safety.

There are many convenience functions implemented by the DataService framework.
For example:


// get a string with all table names, separated by comma, for that data source
String allDsTableNames = DataService.getAllTables(dsName);

// get the lists of column names and column types for a specific table in a specific data source
List[] tableColumnNamesAndTypes = DataService.getColumnNamesAndTypes(dsName, tableName);

// get a page of data with a specific SQL from any data source
int start = 3000; // starting with the record #3000 of the selection result
int pageSize = 40; // deliver 40 records (starting from the record #3000 of the selection)
List pageOfRecords = DataService.getPageData(sql, start, pageSize, dsName);

And many more...


Assignments:
1. Create a project 4.4.5.1.Hibernate in Eclipse (similar to 4.4.5.Hibernate) and copy/paste several examples from the web.
2. Provide the links to your examples on the web in the email to dean@ituniversity.us.

We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>@Entity
<br/>@Table(name="accounts")
<br/>public class Account implements Serializable {
<br/>   private int id;
<br/>   private String email;
<br/>   private String password;
<br/>}  
<br/>

It is a bit more complicated to deal with One-To-Many relationships, for example, describing multiple record types, resulting from different combinations of WHERE clauses with many JOIN in SELECT statements.

JPA/Hibernate frameworks save tremendous time, which used to be consumed by describing the flavors of different database types.

This is done behind the scene by powerful classes like javax.persistence.EntityManager, org.hibernate.Session, javax.persistence.Query and more.

Here is the extract from the constructor of the JpaDataService class:
<br/>		  emf = Persistence.createEntityManagerFactory(jpaUnitName, properties); // using persistence.xml 
<br/>		  em = emf.createEntityManager();
<br/>		  session = em.unwrap(Session.class);
<br/>


Just a reminder: the JpaDataService class is a parent class of the DataBaseService, which is a parent class of the DataService.
The properties for the EntityManagerFactory are derived from the persistence.xml (default) and can be replaced by the DB parameters, if any, in the web.xml.

The JpaDataService class keeps a table of EntityManager instances mapped to their Data Source names. Each EntityManager instance is a Singletone, created once for all users.

Here is the method that returns the JpaDataService object with the EntityManager and the Hibernate Session instances for a specific data source name.
<br/>	public static JpaDataService getJds(String appName, String jpaUnitName) {
<br/>		if(tableOfJpaServices == null) {
<br/>			tableOfJpaServices = new Hashtable<String, JpaDataService>();
<br/>		}
<br/>		JpaDataService jds = tableOfJpaServices.get(jpaUnitName);
<br/>		if(jds == null) {
<br/>			jds = new JpaDataService(appName, jpaUnitName);
<br/>			jds.setDsName(appName+"/"+jpaUnitName);
<br/>			tableOfJpaServices.put(jpaUnitName, jds);
<br/>		}
<br/>		return jds;
<br/>	}
<br/>






Was it clear so far? Highlight the text in question

Or



The methods of the JpaDataService class support the main functions of the DataService APIs, such as:

<br/>int nRecords = DataService.setData(sql, dsName);
<br/>
<br/>List<Object[]> records = DataService.getData(sql, dsName);
<br/>
<br/>// using sqlName to get SQL 
<br/>String sqlName = "updatePassword"; // update password set psw=? where userId=?
<br/>// create run-time parameters to update password
<br/>String[] runTimeParams = new String[] {psw, userId};
<br/>// update
<br/>int nRecords = DataService.setPrepDataBySqlName(sqlName, runTimeParams, dsName);
<br/>
<br/>// retrieve data records with sqlName and run-time parameters in where clause
<br/>List<Object[]> records = DataService.getPrepDataBySqlName(sqlName, runTimeParams, dsName);
<br/>


The DataService framework uses a powerful Hibernate Query object to retrieve and change data.
While changing data, any method calls a Transaction object providing thread safety.

There are many convenience functions implemented by the DataService framework.
For example:

<br/>// get a string with all table names, separated by comma, for that data source
<br/>String allDsTableNames = DataService.getAllTables(dsName); 
<br/>
<br/>// get the lists of column names and column types for a specific table in a specific data source
<br/>List<String>[] tableColumnNamesAndTypes = DataService.getColumnNamesAndTypes(dsName, tableName);
<br/>
<br/>// get a page of data with a specific SQL from any data source 
<br/>int start = 3000; // starting with the record #3000 of the selection result
<br/>int pageSize = 40; // deliver 40 records (starting from the record #3000 of the selection)
<br/>List<Object[]> pageOfRecords = DataService.getPageData(sql, start, pageSize, dsName);
<br/>
<br/>And many more...
<br/>


Assignments:
1. Create a project 4.4.5.1.Hibernate in Eclipse (similar to 4.4.5.Hibernate) and copy/paste several examples from the web.
2. Provide the links to your examples on the web in the email to dean@ituniversity.us.


We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!


| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to AI
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy