Chapter 1. Entity and Session

In this chapter, we will take an in-depth look at sessions and entities and their lifecycles. It is important to understand the concepts of session and entity when we talk about design concepts, such as session-per-request, session-per-conversation, stateless sessions, and contextual sessions, and discuss the various states of an entity. After all, the only way to master anything is by paying attention to the details. We also explore entities beyond their JPA characteristics and look at Hibernate entities to see the benefits of one over the other. Furthermore, we discuss proxy objects and how they are used.

In this chapter, we will cover the following topics:

  • Why this book?
  • Quick Hibernate
  • Hibernate session:
    • Session internals
    • Contextual sessions
    • Sessions per request, per conversation, and per operation
    • Stateless sessions
  • Hibernate entities:
    • Entity lifecycle
    • Types of entities
    • Identity crisis
    • Beyond JPA
  • Proxy objects
  • Batch processing:
    • Manual batch management
    • Setting the size of the batch
  • Using a stateless session

Why this book?

Java developers solve problems using object-oriented concepts. We design applications using classes to model business entities. Furthermore, we utilize inheritance to imply that a class is another kind of class, or is composed of primitive fields and other classes, and visualize the application data as objects or object graphs. However, we also have a persistence problem.

Traditionally, the storage unit is implemented using structured records (tuples), which are stored in tables (relations) that may or may not be associated with each other. This concept is supported by a declarative language, which is limited in scope and is primarily for data creation and data manipulation. Tuples and objects have a lot in common; they both have attributes (columns and fields), and the attributes have data types (int, char, and so on), but the persistence problem becomes evident when you look at the differences between tuples and objects, such as identity, equality, or inheritance.

Object-Relational Mapping is a hard problem. Luckily, Hibernate makes this easy. You probably discovered this by reading the first few chapters of the Hibernate online documents or another book; and as you have to meet tight deadlines, you reactively solve your problems when they surface by swiftly paging through a book, searching or posting on stackoverflow, or other online forums or blogs. You spent half a day trying to find your answer and then moved on until the next problem surfaced. I have done it, you have done it; we ALL do it.

However, what if you knew about the internals of Hibernate and how it works? You wouldn't need to know everything about Hibernate, but you would know exactly where to look quickly to find your answer, such as a dictionary.

This book was written to explore the fundamental concepts of Hibernate and discuss them in detail, so that next time you run into a problem, you can identify the issue and find the answer that you want quickly. For example, whether a problem is a mapping problem or just improper use of an annotation. Furthermore, you will design better software once you understand the internals of any framework that you decide to use.

The main objectives of this book are to help you understand Hibernate beyond the basics, make you appreciate the ORM problem, and show you why Hibernate is one of the best solutions that exists today. We focus more on the Hibernate API and occasionally explore the JPA counterpart. This book assumes that you have a basic understanding of Hibernate and have used it in the past, or you are currently using it. If this is not the case for you, please visit the Hibernate documentation online, as it offers guides to get started with Hibernate and more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.120