Author: Joseph B. Ottinger, Dave Minter, Jeff Linwood
Pub Date: 2014
Format: PDF +code
Size: 10 Mb
Beginning Hibernate, Third Edition is ideal if you’re experienced in Java with databases (the traditional, or “connected,” approach), but new to open-source, lightweight Hibernate, a leading object-relational mapping and database-oriented application development framework.
This book packs in information about the release of the Hibernate 4.x persistence layer and provides a clear introduction to the current standard for object-relational persistence in Java. And since the book keeps its focus on Hibernate without wasting time on nonessential third-party tools, you’ll be able to immediately start building transaction-based engines and applications.
Experienced authors Joseph Ottinger with Dave Minter and Jeff Linwood provide more in-depth examples than any other book for Hibernate beginners. The authors also present material in a lively, example-based manner—not a dry, theoretical, hard-to-read fashion.
What you’ll learn
How to build enterprise Java-based transaction-type applications that access complex data with Hibernate
How to work with Hibernate 4
Where to integrate into the persistence life cycle
How to map using annotations, Hibernate XML files, and more
How to search and query with the new version of Hibernate
How to integrate with MongoDB using NoSQL
Who this book is for
This book is for Java developers who want to learn about Hibernate.
An Introduction to Hibernate 4.2
Hibernate as a Persistence Solution
Hibernate addresses a lot of these issues, or alleviates some of the pain where it can’t, so we’ll address the points in turn.
First, Hibernate provides cleaner resource management, which means that you do not have to worry about the actual database connections, nor do you have to have giant try/catch/finally blocks. Error conditions may occur such that you do need to handle them, of course; but these are exceptional conditions, not normal ones. (In other words, you’re handling exceptions that you actually shouldhave to handle, instead of handling every exception that you mighthave to handle.)
Hibernate handles the mapping of the object to the database table, including constructing the database schema for you if you so configure it; it doesn’t require one table per object type; you can easily map one object to multiple tables. And Hibernate also handles relationships for you; for example, if you added a list of addresses to a Person object, you could easily have the addresses stored in a secondary table, constructed and managed by Hibernate.
In addition to mapping the object to the database table, Hibernate can handle mappings of new types to the database. The geolocation can be specified as its own table, can be normalized, or can have a custom serialization mechanism such that you can store it in whatever native form you need.
Hibernate’s startup takes a little bit longer than direct JDBC code, to be sure. However, system initialization time is usually not a meaningful metric. Most applications have long runtimes and the percentage of time spent in Hibernate initialization is irrelevant to the actual performance of the application; Hibernate’s advantages in maintenance and object management more than make up for any time the application spends in configuration. As usual, the right way to consider performance is through testing and analysis of an actual application, as opposed to spitballing anecdotal evidence.
Integrating and Configuring Hibernate
Understanding Where Hibernate Fits into Your Java Application
You can call Hibernate from your Java application directly, or you can access Hibernate through another framework.
You can call Hibernate from a Swing application, a servlet, a portlet, a JSP page, or any other Java application that
has access to a database. Typically, you would use Hibernate to either create a data access layer for an application or
replace an existing data access layer.
Hibernate supports the Java Management Extensions (JMX), J2EE Connector Architecture (JCA), and Java Naming and Directory Interface (JNDI) Java language standards. Using JMX, you can configure Hibernate while it is running. Hibernate may be deployed as a JCA connector, and you can use JNDI to obtain a Hibernate session factory
in your application. In addition, Hibernate uses standard Java Database Connectivity (JDBC) database drivers to access the relational database. Hibernate does not replace JDBC as a database connectivity layer; Hibernate sits on a
level above JDBC.
In addition to the standard Java APIs, many Java web and application frameworks now integrate with Hibernate.
Hibernate’s simple, clean API makes it easy for these frameworks to support Hibernate in one way or another. The
Spring framework provides excellent Hibernate integration, including generic support for persistence objects, a generic set of persistence exceptions, and transaction management. Appendix C explains how Hibernate can be
configured within a Spring application.
Regardless of the environment into which you are integrating Hibernate, certain requirements remain constant.
You will need to define the configuration details that apply; these are then represented by a Configurationobject.
From the Configurationobject, a single SessionFactoryobject is created; and from this, Sessionobjects are instantiated, through which your application accesses Hibernate’s representation of the database.