Java Persistence With Hibernate Second Edition Torrent

Beginning Java and Flex - Migrating Java, Spring, Hibernate, and Maven Developers to Adobe Flex.pdf 7.81MB DarksideRG.txt 23B Doc Create Time: 2017-08-11 Files: 3 Total size: 7.81MB Seeders: 0 Leechers: 0. Okay, first up on my list is Hibernate.I’ve used Hibernate with several projects already but since this is one of the two frameworks I will be teaching to my Java EE class, the other one is Spring MVC, I better read “Java Persistence With Hibernate 2nd Edition by Christian Bauer and Gavin King” and have a refresher about the basics.

Details
Written by Nam Ha Minh
Last Updated on 17 July 2019 | Print Email
In this Java Hibernate & JPA tutorial, you will learn how to code a simple database program using Hibernate framework with JPA annotations. Besides, you will also learn how to setup a Java Maven project in Eclipse, and work with MySQL database.This tutorial supposes that you already installed the following software programs on your computer:

- Java Development Kit (JDK 1.8 or above)

- MySQL, includes MySQL database server, MySQL Command Line Client, and MySQL Workbench tool (MySQL 5.5 or above)

- Eclipse IDE (Neon or later)

Here are the steps you will follow in this tutorial:

1. Overview of JPA and Hibernate

Let’s understand some fundamentals about JPA and Hibernate before writing code.

Java Persistence API (JPA):

JPA is a Java API specification for relational data management in applications using Java SE and Java EE. JPA defines a standard way for simplifying database programming for developers, reduce development time and increase productivity.When using JPA, you have to import interfaces and classes from the package javax.persistence.

JPA defines Java Persistence Query Language (JPQL) which is an object-oriented query language. The syntax of JPQL is similar to SQL but it operates against Java objects rather than directly with database tables.Remember JPA is a specification, and Hibernate is one of its implementations, among others such as EclipseLink and OpenJPA.

Hibernate Framework:

Hibernate is a popular Object Relational Mapping (ORM) framework that aims at simplifying database programming for developers.Hibernate was developed before JPA. And after JPA becomes a standard, Hibernate restructures itself to become an implementation of JPA.The Hibernate framework consists of several components: Hibernate ORM, Hibernate Search, Hibernate Validator, Hibernate CGM and Hibernate Tools.In this tutorial, we use Hibernate ORM which is the core component of the Hibernate framework, for mapping Java model classes to tables in a relational database.

2. Create MySQL Database

Use the following statement to create a database named usersdbusing MySQL Workbench or MySQL Command Line Client:Then create a table name users with 4 columns: user_id, fullname, email and password, using the following script:Using desc users command in MySQL Command Line Client, the structure of the table looks like this:Note that the column user_id is the table’s primary key and it is auto-increment.

3. Setup Java Maven Project in Eclipse

In Eclipse IDE, click File > New > Project… and select Maven > Maven Project in the New Project dialog. Then click Next.In the next screen, check the option ‘Create a simple project (skip archetype selection)’, and then click Next.In the New Maven Project screen, enter the project’s information as follows:

- Group Id: net.codejava.hibernate

- Artifact Id: HibernateJPABeginner

Leave other things as they are and click Finish. In the Project Explorer view, you see the project gets created with the following structure:

Configure Maven Dependencies:

Next, we need to add dependencies in Maven’s Project Object Model (pom.xml) for Hibernate, JPA and MySQL Connector Java. Open the pom.xml file in XML mode and insert the following XML just before the </project> tag:You see, here we add two dependencies for the project: hibernate-core and mysql-connector-java. Maven automatically downloads the required JAR files which are shown under the Maven Dependencies node in the project:You see, we just specify the dependency hibernate-core, but Maven can analyze and download all the dependencies of hibernate-core as well. That’s really helpful, right?Create a new Java package name net.codejava.hibernate under the src/main/java folder. We’ll put our Java classes in this package.

4. Code Model Class

Next, let’s create a domain model class named User. Then we will use JPA annotations to map this table to the corresponding table in the database.Here’s the initial code of the User class:You see, this is just a POJO (Plain Old Java Object) class with some instance fields and its getter and setter methods. Now, let’s use some annotations provided by JPA to map this model class to the users table in the database.@EntityJava Persistence With Hibernate Second Edition TorrentThis annotation indicates that the class is mapped to a database table. By default, the ORM framework understands that the class name is as same as the table name. The @Entity annotation must be placed before the class definition:@TableThis annotation is used if the class name is different than the database table name, and it is must placed before the class definition. Since the class name is User and the table name is Users, we have to use this annotation:@ColumnThis annotation is used to map an instance field of the class to a column in the database table, and it is must placed before the getter method of the field. By default, Hibernate can implicitly infer the mapping based on field name and field type of the class. But if the field name and the corresponding column name are different, we have to use this annotation explicitly. ThisIn our model class, the field name id is different than the column user_id, so we have to use the @Column annotation as follows:The other fields (fullname, email and password) have identical names as the corresponding columns in the table so we don’t have to annotate those fields.@IdThis annotation specifies that a field is mapped to a primary key column in the table. Since the column user_id is a primary key, we have to use this annotation as follows:@GeneratedValueIf the values of the primary column are auto-increment, we need to use this annotation to tell Hibernate knows, along with one of the following strategy types: AUTO, IDENTITY, SEQUENCE, and TABLE. In our case, we use the strategy IDENTITY which specifies that the generated values are unique at table level, whereas the strategy AUTO implies that the generated values are unique at database level.Therefore, the getter method of the field id is annotated as follows:Finally, we have the model class User is annotated as follows:

5. Create JPA Configuration File (persistence.xml)

Next, we need to create an XML configuration file for JPA called persistence.xml, in order to tell Hibernate how to connect to the database. This file must be present in the classpath, under the META-INF folder.Under the src/main/resources folder, create a new folder named META-INF (Right-click, select New > Other… > Folder).Right click on the newly created folder META-INF, select New > Other… > XML > XML File. Enter the file name as persistence.xml. And paste the following XML code:The root element <persistence> specifies the version of JPA to be used, and as you can see, we use JPA version 2.1.The element <persistence-unit> specifies a unit of persistence with a name. The name (UsersDB) will be looked up by Java code.Then we specify several properties for database connection information:

- javax.persistence.jdbc.url: specifies the JDBC URL points to the database.

- javax.persistence.jdbc.user: specifies the username of the account having privilege to access to the database.

- javax.persistence.jdbc.password: specifies the password of the user.

- javax.persistence.jdbc.driver: specifies the class name of the JDBC driver to be used. Here we use MySQL Connector Java so the name is com.mysql.jdbc.Driver.

- hibernate.show_sql: tells Hibernate to show SQL statements in standard output.

- hibernate.format_sql: tells Hibernate to format the SQL statements.

So you may need to change the values for url, user, and password accordingly.

6. Understand EntityManager and EntityManagerFactory

Finally, we need to code a test program to check if everything we’ve done so far is correct or not. But let’s understand a couple of key interfaces in JPA first.

EntityManager:

An EntityManager instance is associated with a persistence context, and it is used to interact with the database.A persistence context is a set of entity instances, which are actually the objects or instances of the model classes.So we use the EntityManager to manage entity instances and their life cycle, such as create entities, update entities, remove entities, find and query entities.

EntityManagerFactory:

An EntityManagerFactory is used to create an EntityManager. And EntityManagerFactory is associated with a persistence unit. In Java SE environments, an EntityManagerFactory can be obtained from the Persistence class.And here are the typical steps to manage entity instances via JPA:

- Create an EntityManagerFactory from a persistence unit

- Create an EntityManager from the EntityManagerFactory

- Begin a transaction

- Manage entity instances (create, update, remove, find, query, etc)

- Commit the transaction

- Close the EntityManager and EntityManagerFactory

Let’s see the code details below.

7. Code a Test Program

Now, let’s write some code to create, update, find, query and remove User entity instances using JPA. Create a new Java class under src/main/java folder called UserDAOTest.java, with the main() method.

Persist an entity instance:

In the main() method, add the following code to create an EntityManager and begin the transaction:And write the following code to saves a new User object to the database:As you can see, we call the persist(Object) method of the EntityManager class to save the User object to the underlying database.And finally commit the transaction and close the EntityManager and EntityManagerFactory:So the complete program should look like this:Run this program, and you should see the following output in the Console view:You see, Hibernate prints the SQL statement which is nicely formatted. That means the program has been executed successfully. You can check the result by typing the command select * from users in MySQL Command Line Client tool:You see, a new row created and you don’t have to write any SQL statement, right? That’s the power of using Hibernate/JPA for database programming.Let’s see how to perform other operations.

Update an entity instance:

The following code snippet updates an entity instance which is already persisted in the database:As you can see, we need to specify the ID of the object and use the merge(Object) method of the EntityManager class.

Find an entity instance:

To find an entity from the database, we use the method find(Class<T> entityClass, Object primaryKey) of the EntityManager class. For example:This code finds the user with ID = 1 in the database.

Execute a query:

The following code snippet shows you how to execute a query (JPQL):Note that the query looks similar to traditional SQL syntax but it is not. The difference is JPQL operates against entity instances (Java objects) rather than tables in database.

Remove an entity instance:

And the following code demonstrates how to delete an entity instance:As you can see, this code removes the User object with ID = 1 from the database, first by looking up a reference based on the class type (User.class) and primary key value (1), then remove the reference.That’s how to get started with Hibernate/JPA with Eclipse and MySQL. We hope this tutorial is helpful for you. Happy coding!

References:

You can watch the video version of this tutorial here:

Other Hibernate Tutorials:


About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.

Description

Duration: 5 Days

Description

This course teaches students the Java specification for persisting objects to the database. It does so through Hibernate – the most popular JPA implementation and object-relational mapping framework for Java environments. Object relational mapping in large enterprise applications is difficult. The task is so difficult that the problem of storing and retrieving objects to a relational database has its own name – impedance mismatch.

In this class, students learn object-relational mapping concepts and the various issues and options available in Java to address object persistence. With these fundamentals, the course then explores the basics of JPA/Hibernate object persistence and configuration. It also digs into the details of mapping, queries, transactions, and concurrency.

Not just a class that focuses on theory, this course is loaded with practical labs and deals with maintenance and performance issues. After taking this class, developers will be able to build faster, more flexible and easier to maintain application persistence layers and overcome impedance mismatch with JPA implementations like the Hibernate framework.

Learning Objectives

  • Understand the challenges of mapping objects to relational databases
  • Learn the architecture of Hibernate and JPA
  • Know how to setup and configure Hibernate and/or JPA for a Java project
  • Learn to map Java classes and object associations to relational database tables with JPA and Hibernate annotations
  • Learn to map Java classes and object associations to relational database tables with Hibernate or JPA XML mapping files
  • Study strategies for mapping Java inheritance trees to relational database tables
  • Learn the Hibernate Query Language, Java Persistence Query Language and Criteria for retrieving Java objects
  • Explore how to manage database transactions
  • Look at optimistic and pessimistic concurrency options provided by Hibernate and JPA

Audience

This class is designed for Java programmers with a need to understand the Java persistence provided by Hibernate or JPA framework and API.

Prerequisites

Students should have a good understanding of the Java programming language. A basic understanding of relational databases and SQL is very helpful.

Course Outline

Object Persistence

  • Persistence
  • Object Persistence
  • Object/Relational Paradigms
  • Impedance Mismatch
  • Object Relational Mapping (ORM)
  • Hibernate
  • Why an ORM framework? Why Hibernate?
  • Java Persistence API (JPA)
  • Hibernate Projects
  • Java ORM/Persistent Frameworks

Hibernate

  • A Quick Hibernate Example
  • Hibernate Architecture and API
  • Hibernate Installation/Setup
  • Configuration
  • Configuration Properties
  • Mapping Files

JPA

Java Persistence With Hibernate Second Edition Torrent Free

  • A Quick JPA Example
  • JPA Architecture and API
  • Hibernate Installation/Setup with JPA
  • Persistence Configuration
  • Mapped Entities and Mapping Files

Entity Mapping by Annotation

  • Persistent Classes
  • POJOs
  • JavaBeans
  • Annotation Metadata
  • Annotation Configuration
  • Basic Entity Mapping via Annotations
  • Basic Property Mapping via Annotation
  • ID Mapping via Annotation
  • Composite Keys (and Natural Keys)
  • Access Type
  • Hibernate Mapping Annotations

Entity Mapping by XML

  • XML Metadata
  • XML vs. Annotatin – Pros/Cons
  • Hibernate XML Metadata Basics
  • JPA XML Metadata Basics
  • Basic Entity Mapping via XML
  • Basic Property Mapping via XML
  • Hibernate Types
  • ID Mapping via XML
  • Identifier Exposure
  • Access Type

Persistence Context

  • Entity Lifecycle
  • New or Transient State
  • Managed or Persistent State
  • Entity Updates and Automatic Dirty Checking
  • Flushing
  • Detached State
  • Detached State – Why?
  • Remove/Delete
  • Object Identifiers Revisited
  • Detached & Merge Revisited
  • Refresh

Embeddables and Components

  • Domain Model
  • Entities and Values
  • Embeddables and Components
  • Embeddable by Annotation
  • JPA Embeddable by XML
  • Hibernate Component by XML
  • Component Bidirectional Association
  • Issues and Warnings for Embeddables/Components
  • Embeddable Composite Id
  • Purpose of Embeddables/Components

Entity Associations

  • Many-to-one
  • Many-to-One by Annotation
  • Many-to-One by XML mapping
  • Bidirectional Many-to-One
  • Bidirectional Many-to-One by Annotation
  • Bidirectional Many-to-One by XML
  • Other Collection Types
  • Many-to-One List
  • Many-to-One Bag
  • Many-to-One Map

More Associations

  • One-to-one
  • One-to-one by Annotation
  • One-to-one by XML
  • Bidirectional One-to-One
  • Bidirectional One-to-One by Annotation
  • Bidirectional One-to-One by XML
  • Many-to-many
  • Many-to-many by Annotation
  • Many-to-many by XML
  • Bidirectional Many-to-Many
  • Bidirectional Many-to-Many with Annotations
  • Bidirectional Many-to-Many with XML

Value Type Collections

  • Collections of Value Types
  • Collections of Embeddables/Components

Inheritance

  • Inheritance Mapping Strategies
  • Table per Hierarchy
  • Table per Subclass
  • Table per Concrete Class
  • Table per Concrete Class with Mapped Superclass
  • Inheritance Mapping Pros/Cons
  • Polymorphism
  • Table per Concrete Class with Unions
  • Choosing an Inheritance Strategy

Cascading/Loading

Java Persistence With Hibernate Second Edition Torrent Pirate Bay

  • Cascading/Transitive Persistence
  • Lazy Loading
  • Proxies
  • Eager Loading
  • What’s a Proxy?
  • Detached Objects and Proxies
  • Polymorphic Associations Revisited

Queries

  • Persistent Object Fetching Options
  • Hibernate Query
  • HQL
  • HQL Parameters
  • JPA Query and JPQL
  • Named Queries
  • Native SQL

Criteria

Java Persistence With Hibernate Second Edition Torrent Kickass

  • Criteria
  • Criteria Select
  • Criteria Inner/Outer Joins
  • Criteria Parameters
  • Criteria Where
  • Criteria Order
  • Metamodel
  • Pagination and Query Hints
  • Hibernate Scrolling
  • Query Option Pros/Cons

Hibernate With Java Core

Transactions and Concurrency

Java Persistence With Hibernate Second Edition Torrent Kickass

  • The Java Transaction
  • Hibernate Transaction Configuration
  • Hibernate Transaction API
  • JPA Transaction Configuration
  • JPA Transaction API
  • Optimistic Locking
  • Versioning
  • Hibernate Optimistic Locking without Versioning
  • Pessimistic Locking API