Java Reference
In-Depth Information
3.
Then the
Employee
class was created. It contains the same four attributes as in the MySQL
database:
EmployeeID
,
Name
,
Gender
, and
DNR
. The mapping to the Employee MySQL table is
performed using the
@Entity
and
@Id
annotations. The
@Entity
annotation is included before
the class definition and maps the class to the relational MySQL Employee table. The
@Id
annota-
tion then defines the identifier of the class that will be mapped to the primary key of the MySQL
Employee table. Note that the Java class may have a different name than the MySQL table. In
this case, the
@Table
annotation can be used to define the mapping. If no
@Table
annotation is
used, the name of the class is used, which is fine in this case since it is identical to the name of the
MySQL table. Likewise, the
@Column
annotation can be used to map Java properties to MySQL
4.
In Step 7 of the Try It Out, the
myDBApp
class was created to perform some basic database opera-
tions. It starts with creating a new employee object
Myemp
and setting its characteristics. Then a
sessionFactory
object was created, which will in turn allow the creation of a
session
object.
The latter represents a single-threaded wrapped JDBC connection with the database. Note that
Hibernate often introduces new classes and methods to create both
sessionFactory
and
session
5.
The
session
object serves as a factory for creating database transactions. The statement
session.
beginTransaction();
initiates a database transaction that represents an atomic unit of database
operations. If needed, multiple transactions per session can be created. The
Myemp
employee object
will then be made persistent in the MySQL database using the method
session.save(Myemp)
fol-
lowed by
session.getTransaction().commit()
. Then the insert is verified by issuing the HQL
query:
Query query = session.createQuery("from Employee where EmployeeID = 6")
. The
results of the query are stored in a list object
List<?> list = query.list()
. The query will return
one result, which will be stored in the object
emp
as follows:
Employee emp = (Employee)list.get(0)
.
This is followed by retrieving the various attributes and displaying them on the console. The program
finishes by closing the
session
and
sessionFactory
objects.
6.
Since the
show_SQL
property was set to
true
in the configuration file, you can see the SQL state-
ments that are being generated by Hibernate as follows:
Hibernate: insert into Employee (DNR, gender, name, employeeID) values (?, ?, ?, ?)
Hibernate: select employee0_.employeeID as employee1_0_, employee0_.DNR as DNR2_0_,
employee0_.gender as gender3_0_, employee0_.name as name4_0_
from Employee employee0_
where EmployeeID=6
The result of the query is then:
Hibernate dude
Male
2
This is also confirmed by inspecting the table directly in MySQL.
In most applications, Java objects and their corresponding relational tables will be associated using
relationships. Hibernate also has facilities for managing the object relational mapping of these asso-
ciations. Obviously, the mapping depends on the type and the multiplicity of the association.
Search WWH ::
Custom Search