Java Reference
In-Depth Information
4.
The many-to-many mapping between Employee and Project is then implemented using the
@ManyToMany annotation. The option cascade=CascadeType.ALL indicates that all data
manipulations of employee objects will be immediately propagated to both the employee and
works_on MySQL tables. For example, if an employee object is removed, then Hibernate will
not only remove the corresponding tuple from the employee MySQL table, but also all refer-
ring tuples from the works_on MySQL table. The @JoinTable annotation then defines the
connection to the join table. It is always specified in the class representing the owning side,
which in this case is the Employee class. The name property specifies the corresponding join
works_on MySQL table. The joinColumns property connects to the relevant MySQL attribute
of the owning side ( Employee ) using the @JoinColumn annotation. The inverseJoinColumns
property connects to the relevant MySQL attribute of the non-owning side ( Project ), again by
using the @JoinColumn annotation. This is then followed by the setter and getter methods for
the project's attribute.
5.
In Step 6 of the Try It Out, you made sure that the Project class was now also included in the
hibernate.cfg.xml mapping file.
6.
In the next step, the class myDBApp2 was created. First, a set object projects was defined and two
new project objects were added to it. Then a new Employee object called Myemp was created and
the projects set object was added to it. As in the previous example, a sessionFactory object
was created, which then opened a session object, which in turn started a transaction. The Myemp
object was then saved, the transaction was committed, and the session and sessionFactory
objects were closed.
7.
As in the previous example, since the show_SQL property was set to true in the configuration file,
you can see the SQL statements that are being generated by Hibernate as follows:
Hibernate: select project_.projectID, project_.PName as PName2_1_
from Project project_ where project_.projectID=?
Hibernate: select project_.projectID, project_.PName as PName2_1_
from Project project_ where project_.projectID=?
Hibernate: insert into Employee (DNR, gender, name, employeeID) values (?, ?, ?, ?)
Hibernate: insert into Project (PName, projectID) values (?, ?)
Hibernate: insert into Project (PName, projectID) values (?, ?)
Hibernate: insert into works_on (EmployeeID, ProjectID) values (?, ?)
Hibernate: insert into works_on (EmployeeID, ProjectID) values (?, ?)
8.
The results can then also be confirmed by inspecting the employee , project , and works_on tables
in MySQL.
note For small-scale, single-user applications, you could also opt to adopt
SQLite (or its Java implementation SQLJet) instead of MySQL. This is an in-process
library implementing a lightweight, self-contained, server-less, zero-configuration
SQL-based RDBMS. No external client-server based communication (using some-
thing like sockets or ports) is needed since all database communication is handled
directly in the process in which the application runs. The database itself is then
stored in a simple file that can be easily accessed and moved.
Search WWH ::




Custom Search