Listing 9-26. Testing Delete Operation
// Delete contact
contact = contactDao.findById(1l);
contactDao.delete(contact);
contacts = contactDao.findAllWithDetail();
listContactsWithDetail(contacts);
The previous listing retrieves the contact with an ID of 1 and then calls the delete method to delete
the contact information. Running the program will produce the following output (other output was
omitted):
Listing
contacts with details:
Contact
- Id: 4, First name: Michael, Last name: Jackson, Birthday: 2011-10-13
Contact
Tel Detail - Id: 4, Contact id: 4, Type: Home, Number: 1111111111
Contact
Tel Detail - Id: 5, Contact id: 4, Type: Mobile, Number: 2222222222
Contact - Id: 3, First name: John, Last name: Smith, Birthday: 1964-02-28
Contact - Id: 2, First name: Scott, Last name: Tiger, Birthday: 1990-11-02
Contact Tel Detail - Id: 3, Contact id: 2, Type: Home, Number: 1234567890
Hobby :Swimming
You can see that the contact with an ID of 1 was deleted.
Considerations of Using Hibernate
As you saw in the examples in this chapter, once all the object-to-relational mapping, associations, and
queries are properly defined, Hibernate can provide an environment for you to focus on programming
with the object model, rather than composing SQL statements for each operation. In the past few years,
Hibernate has been evolving quickly and has been widely adopted by Java developers as the data access
layer library, both in the open source community and in enterprises.
However, there are some points you need to bear in mind. First, because you don't have control
over the generated SQL, you should be very careful when defining the mappings, especially the
associations and their fetching strategy. Then observe the SQL statements generated by Hibernate to
verify that all perform as you expect.
Understanding the internal mechanism of how Hibernate manages its session is also very
important, especially in batch job operations. Hibernate will keep the managed objects in session and
will flush and clear them regularly. Poorly designed data access logic may cause Hibernate to flush the
session too frequently and greatly impact the performance. If you want absolute control over the query,
you can use a native query, which we will discuss in next chapter.
Finally, the settings (batch size, fetch size, etc.) also play an important role in tuning Hibernate's
performance. You should define them in your session factory and adjust them while load testing your
application to identify the optimal value.
After all, Hibernate, and its excellent JPA support that we will discuss in next chapter, is a natural
decision for Java developers looking for an OO way to implement data access logic.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home