Hibernate (Java)
Encyclopedia
Hibernate is an object-relational mapping
(ORM) library for the Java
language, providing a framework
for mapping an object-oriented
domain model
to a traditional relational database
. Hibernate solves object-relational impedance mismatch
problems by replacing direct persistence
-related database accesses with high-level object handling functions.
Hibernate is free software
that is distributed under the GNU Lesser General Public License
.
Hibernate's primary feature is mapping from Java classes to database tables (and from Java data types to SQL
data types). Hibernate also provides data query and retrieval facilities. Hibernate generates the SQL calls and attempts to relieve the developer from manual result set handling and object conversion and keep the application portable to all supported SQL databases with little performance overhead.
file or by using Java Annotations
. When using an XML file, Hibernate can generate
skeletal source code
for the persistence classes. This is unnecessary when annotations are used. Hibernate can use the XML file or the annotations to maintain the database schema
.
Facilities to arrange one-to-many
and many-to-many
relationships between classes are provided. In addition to managing associations between objects, Hibernate can also manage reflexive
associations where an object has a one-to-many relationship with other instances of its own type
.
Hibernate supports the mapping of custom value types. This makes the following scenarios possible:
s (POJOs). The only strict requirement for a persistent class is a no-argument constructor
, not necessarily public. Proper behavior in some applications also requires special attention to the equals and hashCode methods.
Collections of data objects are typically stored in Java collection objects such as Set and List. Java generics
, introduced in Java 5, are supported. Hibernate can be configured to lazy load associated collections. Lazy loading is the default as of Hibernate 3.
Related objects can be configured to cascade operations from one to the other. For example, a parent such as an Album object can be configured to cascade its save and/or delete operation to its child Track objects. This can reduce development time and ensure referential integrity
. A dirty checking feature avoids unnecessary database write actions by performing SQL updates only on the modified fields of persistent objects.
inspired language called Hibernate Query Language (HQL) which allows SQL-like queries to be written against Hibernate's data objects. Criteria Queries are provided as an object-oriented
alternative to HQL.
Java
applications and in Java EE applications using servlets
, EJB session beans, and JBI service components. It can also be included as a feature in other programming languages. For example, Adobe
integrated Hibernate into version 9 of ColdFusion
(which runs on J2EE app servers) with an abstraction layer of new functions and syntax added into CFML.
, an entity is a stand-alone object in Hibernate's persistent
mechanism which can be manipulated independently of other objects. In contrast, a component is subordinate to other entities and can be manipulated only with respect to other entities. For example, an Album object may represent an entity but the Tracks object associated with the Album objects would represent a component of the Album entity if it is assumed that Tracks can only be saved or retrieved from the database through the Album object. Unlike J2EE, it can switch databases.
Early in 2003, the Hibernate development team began Hibernate2 releases which offered many significant improvements over the first release.
JBoss, Inc.
(now part of Red Hat
) later hired the lead Hibernate developers and worked with them in supporting Hibernate.
the current version of Hibernate is Version 3.x. This version introduced new features like a new Interceptor/Callback architecture, user defined filters, and JDK 5.0 Annotations
(Java's metadata
feature). Hibernate 3 (version 3.5.0 and up) is a certified implementation of the Java Persistence API 2.0 specification via a wrapper for the Core module which provides conformity with the JSR 317 standard.
org.hibernate.
and threadsafe object creating new Hibernate sessions. Hibernate-based applications are usually designed to make use only of a single instance of the class implementing this interface (often exposed using a singleton design pattern
).
, persisted
, detached) of the objects, fetching the persisted ones from the database and the management of the transaction demarcation.
A session is intended to last as long as the logical transaction on the database. Due to the latter feature, Session implementations are not expected to be threadsafe nor to be used by multiple clients
Object-relational mapping
Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...
(ORM) library for the Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
language, providing a framework
Software framework
In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by user code, thus providing application specific software...
for mapping an object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
domain model
Domain model
A domain model in problem solving and software engineering can be thought of as a conceptual model of a domain of interest which describes the various entities, their attributes, roles and relationships, plus the constraints that govern the integrity of the model elements comprising that problem...
to a traditional relational database
Relational database
A relational database is a database that conforms to relational model theory. The software used in a relational database is called a relational database management system . Colloquial use of the term "relational database" may refer to the RDBMS software, or the relational database itself...
. Hibernate solves object-relational impedance mismatch
Object-Relational impedance mismatch
The object-relational impedance mismatch is a set of conceptual and technical difficulties that are often encountered when a relational database management system is being used by a program written in an object-oriented programming language or style; particularly when objects or class definitions...
problems by replacing direct persistence
Persistence (computer science)
Persistence in computer science refers to the characteristic of state that outlives the process that created it. Without this capability, state would only exist in RAM, and would be lost when this RAM loses power, such as a computer shutdown....
-related database accesses with high-level object handling functions.
Hibernate is free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...
that is distributed under the GNU Lesser General Public License
GNU Lesser General Public License
The GNU Lesser General Public License or LGPL is a free software license published by the Free Software Foundation . It was designed as a compromise between the strong-copyleft GNU General Public License or GPL and permissive licenses such as the BSD licenses and the MIT License...
.
Hibernate's primary feature is mapping from Java classes to database tables (and from Java data types to SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
data types). Hibernate also provides data query and retrieval facilities. Hibernate generates the SQL calls and attempts to relieve the developer from manual result set handling and object conversion and keep the application portable to all supported SQL databases with little performance overhead.
Mapping
Mapping Java classes to database tables is accomplished through the configuration of an XMLXML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
file or by using Java Annotations
Java annotation
An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated...
. When using an XML file, Hibernate can generate
Program synthesis
Program synthesis is a special form of automatic programming that is most often paired with a technique for formal verification. The goal is to automatically construct a program that provably satisfies a given high-level specification...
skeletal source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...
for the persistence classes. This is unnecessary when annotations are used. Hibernate can use the XML file or the annotations to maintain the database schema
Database schema
A database schema of a database system is its structure described in a formal language supported by the database management system and refers to the organization of data to create a blueprint of how a database will be constructed...
.
Facilities to arrange one-to-many
One-to-many
One-to-many may refer to:* Multivalued function, a one-to-many function in mathematics* Fat link, a one-to-many link in hypertext* Point-to-multipoint communication, communication which has a one-to-many relation-See also:*One-to-one...
and many-to-many
Many-to-many (data model)
In systems analysis, a many-to-many relationship is a type of cardinality that refers to the relationship between two entities A and B in which A may contain a parent row for which there are many children in B and vice versa. For instance, think of A as Authors, and B as Books...
relationships between classes are provided. In addition to managing associations between objects, Hibernate can also manage reflexive
Reflexive relation
In mathematics, a reflexive relation is a binary relation on a set for which every element is related to itself, i.e., a relation ~ on S where x~x holds true for every x in S. For example, ~ could be "is equal to".-Related terms:...
associations where an object has a one-to-many relationship with other instances of its own type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
.
Hibernate supports the mapping of custom value types. This makes the following scenarios possible:
- Overriding the default SQL type that Hibernate chooses when mapping a column to a property.
- Mapping Java EnumEnumerated typeIn computer programming, an enumerated type is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language...
to columns as if they were regular properties. - Mapping a single property to multiple columns.
- Mapping one to one or one to many entities.
Persistence
Hibernate provides transparent persistence for Plain Old Java ObjectPlain Old Java Object
In computing software, POJO is an acronym for Plain Old Java Object. The name is used to emphasize that a given object is an ordinary Java Object, not a special object...
s (POJOs). The only strict requirement for a persistent class is a no-argument constructor
Nullary constructor
In computer programming, a nullary constructor is a constructor that takes no arguments.-Object-oriented constructors:In object-oriented programming, a constructor is code that is run when an object is created...
, not necessarily public. Proper behavior in some applications also requires special attention to the equals and hashCode methods.
Collections of data objects are typically stored in Java collection objects such as Set and List. Java generics
Generics in Java
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow "a type or method to operate on objects of various types while providing compile-time type safety." A common use of this feature is when using a Java Collection...
, introduced in Java 5, are supported. Hibernate can be configured to lazy load associated collections. Lazy loading is the default as of Hibernate 3.
Related objects can be configured to cascade operations from one to the other. For example, a parent such as an Album object can be configured to cascade its save and/or delete operation to its child Track objects. This can reduce development time and ensure referential integrity
Referential integrity
Referential integrity is a property of data which, when satisfied, requires every value of one attribute of a relation to exist as a value of another attribute in a different relation ....
. A dirty checking feature avoids unnecessary database write actions by performing SQL updates only on the modified fields of persistent objects.
Hibernate Query Language (HQL)
Hibernate provides an SQLSQL
SQL is a programming language designed for managing data in relational database management systems ....
inspired language called Hibernate Query Language (HQL) which allows SQL-like queries to be written against Hibernate's data objects. Criteria Queries are provided as an object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
alternative to HQL.
Integration
Hibernate can be used both in standaloneStand-alone
Standalone software can mean:* Computer software that can work offline, i.e. does not necessarily require network connection to function* Software that is not a part of some software bundle...
Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
applications and in Java EE applications using servlets
Java Servlet
A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers...
, EJB session beans, and JBI service components. It can also be included as a feature in other programming languages. For example, Adobe
Adobe Systems
Adobe Systems Incorporated is an American computer software company founded in 1982 and headquartered in San Jose, California, United States...
integrated Hibernate into version 9 of ColdFusion
ColdFusion
In computing, ColdFusion is the name of a commercial rapid application development platform invented by Jeremy and JJ Allaire in 1995. ColdFusion was originally designed to make it easier to connect simple HTML pages to a database, by version 2 it had...
(which runs on J2EE app servers) with an abstraction layer of new functions and syntax added into CFML.
Entities and components
In Hibernate jargonJargon
Jargon is terminology which is especially defined in relationship to a specific activity, profession, group, or event. The philosophe Condillac observed in 1782 that "Every science requires a special language because every science has its own ideas." As a rationalist member of the Enlightenment he...
, an entity is a stand-alone object in Hibernate's persistent
Persistence (computer science)
Persistence in computer science refers to the characteristic of state that outlives the process that created it. Without this capability, state would only exist in RAM, and would be lost when this RAM loses power, such as a computer shutdown....
mechanism which can be manipulated independently of other objects. In contrast, a component is subordinate to other entities and can be manipulated only with respect to other entities. For example, an Album object may represent an entity but the Tracks object associated with the Album objects would represent a component of the Album entity if it is assumed that Tracks can only be saved or retrieved from the database through the Album object. Unlike J2EE, it can switch databases.
History
Hibernate was started in 2001 by Gavin King as an alternative to using EJB2-style entity beans. Its mission back then was to simply offer better persistence capabilities than offered by EJB2 by simplifying the complexities and allowing for missing features.Early in 2003, the Hibernate development team began Hibernate2 releases which offered many significant improvements over the first release.
JBoss, Inc.
JBoss (company)
JBoss is a division of Red Hat, Inc.. It specializes in open-source middleware software.The company profits from a service-based business model. JBoss employ a Professional Open Source business model where the core developers of projects make a living and offer their services...
(now part of Red Hat
Red Hat
Red Hat, Inc. is an S&P 500 company in the free and open source software sector, and a major Linux distribution vendor. Founded in 1993, Red Hat has its corporate headquarters in Raleigh, North Carolina with satellite offices worldwide....
) later hired the lead Hibernate developers and worked with them in supporting Hibernate.
the current version of Hibernate is Version 3.x. This version introduced new features like a new Interceptor/Callback architecture, user defined filters, and JDK 5.0 Annotations
Java annotation
An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated...
(Java's metadata
Metadata
The term metadata is an ambiguous term which is used for two fundamentally different concepts . Although the expression "data about data" is often used, it does not apply to both in the same way. Structural metadata, the design and specification of data structures, cannot be about data, because at...
feature). Hibernate 3 (version 3.5.0 and up) is a certified implementation of the Java Persistence API 2.0 specification via a wrapper for the Core module which provides conformity with the JSR 317 standard.
Application programming interface
The Hibernate API is provided in the Java packageJava package
A Java package is a mechanism for organizing Java classes into namespaces similar to the modules of Modula. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time...
org.hibernate.
org.hibernate.SessionFactory interface
References immutableImmutable object
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created...
and threadsafe object creating new Hibernate sessions. Hibernate-based applications are usually designed to make use only of a single instance of the class implementing this interface (often exposed using a singleton design pattern
Singleton pattern
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system...
).
org.hibernate.Session interface
Represents a Hibernate session i.e. the main point of the manipulation performed on the database entities. The latter activities include (among the other things) managing the persistence state (transientTransient (computer programming)
-Java:In the Java programming language, transient is a keyword used as a field modifier. When a field is declared transient, it would not be serialized even if the class to which it belongs is serialized...
, persisted
Java Persistence API
The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework managing relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition....
, detached) of the objects, fetching the persisted ones from the database and the management of the transaction demarcation.
A session is intended to last as long as the logical transaction on the database. Due to the latter feature, Session implementations are not expected to be threadsafe nor to be used by multiple clients
Software components
The Hibernate software includes the following components:- Hibernate Core – the base software for an object-relational mapping solution for Java environments
- Hibernate Annotations – metadata that governs the transformation of data between the object-oriented model and the relational database model according to the JSR 317 Java Persistence APIJava Persistence APIThe Java Persistence API, sometimes referred to as JPA, is a Java programming language framework managing relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition....
(JPA 2) - Hibernate EntityManager – together with Hibernate Annotations, a wrapper that implements a JSR 317 Java Persistence APIJava Persistence APIThe Java Persistence API, sometimes referred to as JPA, is a Java programming language framework managing relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition....
(JPA 2) persistence solution on top of Hibernate Core - Hibernate Envers – auditing and versioning of persistent classes
- Hibernate OGM – Object/Grid Mapper is an extension to store data in a NoSQLNosqlIn computing, NoSQL is a broad class of database management systems that differ from the classic model of the relational database management system in some significant ways. These data stores may not require fixed table schemas, usually avoid join operations, and typically scale horizontally...
store - Hibernate Shards – horizontal partitioningPartition (database)A partition is a division of a logical database or its constituting elements into distinct independent parts. Database partitioning is normally done for manageability, performance or availability reasons....
for multiple relational databases - Hibernate Search – integrates the full text library functionality from Apache Lucene in the Hibernate and JPA model
- Hibernate Tools – a set of tools implemented as a suite of EclipseEclipse (software)Eclipse is a multi-language software development environment comprising an integrated development environment and an extensible plug-in system...
plugins and Ant tasks included in JBoss Developer Studio - Hibernate Validator – the reference implementation of JSR 303 Bean ValidationBean ValidationJava Bean Validation is a framework that has been approved by the JCP as of 16 November 2009 and is accepted as part of the Java EE 6 specification. Bean Validation defines a metadata model and API for JavaBean validation. The metadata source is annotations, with the ability to override and extend...
- Hibernate Metamodel Generator – an annotation processor that creates JSR 317 Java Persistence APIJava Persistence APIThe Java Persistence API, sometimes referred to as JPA, is a Java programming language framework managing relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition....
(JPA 2) static metamodel classes using the JSR 269 Pluggable Annotation Processing APIMetadata facility for JavaThe Metadata Facility for Java is a specification for Java that defines an API for annotating fields, methods, and classes as having particular attributes that indicate they should be processed in specific ways by development tools, deployment tools, or run-time libraries.The specification was... - NHibernateNHibernate- External links :*** *NuGet...
– an object-relational mapping solution for the .NET Framework.NET FrameworkThe .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
See also
- List of JBoss software
- pureQuery
- EJB 3.0
- NHibernateNHibernate- External links :*** *NuGet...
- SerializationSerializationIn computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored and "resurrected" later in the same or another computer environment...
- MyBatisMyBatisMyBatis is a persistence framework available for Java and .NET that couples objects with stored procedures or SQL statements using an XML descriptor or annotations.MyBatis is free software that is distributed under the Apache License 2.0....
- iBATISIBATISiBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
- Service Data Object
- QuickDB ORMQuickDB ORMQuickDB is an object-relational mapping framework for the Java software platform. It was developed by Diego Sarmentero along with others and is licensed under the LGPL License...
- List of object-relational mapping software
External links
- Interview with Gavin King, founder of Hibernate
- Hibernate Tutorial
- Hibernate Reference Documentation
- Hibernate and HQL Query Tutorial
- HQL Guru - Running Hibernate Query Examples
- scale4j is highly scalable domain oriented data-distributed platform for java built on top of hibernate
- Hibernate Tutorial
- TorpedoQuery - Type safe Hibernate query builder