Chapter 11. Implementing conversations

You've tried the examples in previous chapters and stored and loaded objects inside transactions. Very likely you've noticed that code examples of five lines are excellent to help you understand a particular issue and learn an API and how objects change their state. If you take the next step and try to apply what you've learned in your own application, you'll probably soon realize that you're missing two important concepts.

The first concept we'll show you in this chapter—persistence context propagation—is useful when you have to call several classes to complete a particular action in your application and they all need database access. So far, we had only a single method that opened and closed a persistence context (a Session or an EntityManager) internally. Instead of passing the persistence context between classes and methods manually, we'll show you the mechanisms in Hibernate and Java Persistence that can take care of propagation automatically. Hibernate can help you to create more complex units of work.

The next design problem you'll run into is that of application flow when your application user has to be guided through several screens to complete a unit of work. You must create code that controls the navigation from screen to screen—however, this is outside of the scope of persistence, and we won't have much to say about it in this chapter. What is partly the responsibility of the persistence mechanism is the atomicity and isolation of data access for a unit of work that spans possible user think-time. We call a unit of work that completes in several client/server request and response cycles a conversation. Hibernate and Java Persistence offer several strategies for the implementation of conversations, and in this chapter we show you how the pieces fit together with realistic examples.

We start with Hibernate and then, in the second half of the chapter, discuss JPA conversations. Let's create more complex data access examples first, to see how several classes can reuse the same persistence context through automatic propagation.

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

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