Java Reference
In-Depth Information
How It Works
1.
The class
MyDBApp3
starts by importing the JPA
javax.persistence.*
package.
2.
An
EntityManagerFactory
object
emf
is defined for the database in
C:\objectdb-2.5.6_04\
objectdb-2.5.6_04\db\employeeadm.odb
using a static factory method of the JPA bootstrap
class
Persistence
. If the database does not exist, which is the case in this example, then a new
one will be created. Note that an URL combined with a username and a password can also be used
when creating the
EntityManagerFactory
object. The connection to the database is then repre-
sented by the
EntityManager
object
em
, which is created by the
emf
object. The
emf
object may
create multiple
EntityManager
objects in case multiple database connections are needed, which
can then be efficiently pooled by the
emf
object. The method
em.getTransaction().begin();
then initiates a database transaction. The next lines of Java code then create two new
Project
objects and a new
Employee
object to which the
Project
objects are assigned using a
HashSet
object called
projects
. The
Employee
object is then stored in ObjectDB using the method
em.
persist(Myemp);
and the transaction is committed by
em.getTransaction().commit();
. The
Employee
object has now been made persistent and can now be queried. Note how easy this is
here, compared to the Hibernate approach where a configuration file had to be defined to specify
the mapping details.
3.
In JPA, queries can be implemented using the (old)
Query
or (new)
TypedQuery
interface.
The
Query
interface can be used when the result type of the query is unknown, whereas the
TypedQuery
interface is used when a known result type is expected. Note that queries are always
formulated in JPQL.
4.
The statement
Query q1 = em.createQuery("SELECT COUNT(emp) FROM Employee emp");
creates a JPQL query object called
q1
, which counts the number of employees in the Employee
Entity Class in ObjectDB. The result of the query is a single value and is written to the console
using the method
System.out.println("Total Employees: " + q1.getSingleResult());
.
5.
A JPQL query will now retrieve all
Project
objects and list them on the console. The statement
TypedQuery<Project> query = em.createQuery("SELECT proj FROM Project proj",
Project.class);
creates a
TypedQuery
object whereby the return type of the query, i.e.
Project
,
is now explicitly specified. The statement
List<Project> results = query.getResultList();
then executes the query and stores the results in a list of
Project
objects. A
for
loop is then used
to iterate through this list and display the relevant information to the console. Finally, the
em
and
emf
objects are closed using the methods
em.close();
and
emf.close();
.
6.
Finally, the object tree for the
Employee
and
Project
objects is displayed.
comparing Java dataBase access technologies
In this chapter, you have seen several ways to access databases from Java: JDBC, SQLJ, Hibernate
as an ORM framework example, and OpenDB as an OODBMS example. In the industry, the most
popular approaches currently adopted are JDBC and Hibernate. Despite their intrinsic advantages,
SQLJ and OODBMSs are only seldom used because both are often perceived (rightfully or not) as
complex to work with.
Search WWH ::
Custom Search