Java Reference
In-Depth Information
<property name="JEmpID" column="EmployeeID" type="int"/>
<property name="JName" column="Name" type="string"/>
<property name="JGender" column="Gender" type="string"/>
<property name="JDNR" column="DNR" type="int"/>
</class>
</hibernate-mapping>
This example clearly illustrates how the various class properties are mapped to relational database
columns.
When using annotations, the mapping is directly specified in the POJO class definition itself.
Table 9-6 provides an overview of commonly used annotations available in Hibernate. See
http://hibernate.org/ for a comprehensive overview.
taBle 9-6: Common Hibernate Annotations
annotation
description
Declares a persistent POJO class.
@Entity
Allows you to explicitly specify the name of the relational table
to map the persistent POJO class to, in case both names are
different.
@Table
Allows you to explicitly specify the name of the relational table
column in case it is different from the persistent POJO class
field.
@Column
Maps a persistent POJO class field to a primary key of a rela-
tional table.
@ID
Allows you to define POJO class fields that are transient and
thus should not be made persistent.
@Transient
As opposed to XML, annotations are directly compiled to bytecode and thus provide better
performance. This is a key reason why most developers nowadays use annotations to perform
the mapping. A disadvantage of both XML and annotations is that the persistence details are
spread across different classes or XML files instead of being centralized, which may hamper the
maintenance.
Although Hibernate allows you to directly embed SQL queries in Java, it also comes with a new
full-fledged, database agnostic query language called Hibernate Query Language (HQL). HQL is
object-oriented and works with classes and properties instead of tables and columns. It is less ver-
bose than SQL and includes support for inner/outer joins, polymorphism, subqueries, aggregation,
grouping, and ordering. Queries can be formulated by navigating object relations rather than rela-
tional tables. All HQL queries are translated to relational SQL queries at runtime. JPQL is based on
HQL, since JPA was introduced after Hibernate was created.
In the “Try It Out” that follows, you develop your first Hibernate application.
Search WWH ::




Custom Search