Spring Persistence with Hibernate

Spring Persistence with Hibernate

von: Paul Fisher, Brian D. Murphy

Apress, 2011

ISBN: 9781430226338 , 264 Seiten

Format: PDF, OL

Kopierschutz: Wasserzeichen

Windows PC,Mac OSX geeignet für alle DRM-fähigen eReader Apple iPad, Android Tablet PC's Online-Lesen für: Windows PC,Mac OSX,Linux

Preis: 39,99 EUR

Mehr zum Inhalt

Spring Persistence with Hibernate


 

Contents at a Glance

5

Table of Contents

6

About the Authors

13

About the Technical Reviewer

14

Acknowledgments

15

Preface

16

CHAPTER 1 Architecting Your Application with Spring, Hibernate, and Patterns

18

The Benefit of a Consistent Approach

18

The Significance of Dependency Injection

19

A Synergistic Partnership

19

The Story of Spring’s and Hibernate’s Success

20

A Better Approach for Integration

20

Best Practices for Architecting an Application

21

The Layers of a Persistence Tier

22

The Domain Model

22

The Data Access Object (DAO) Layer

23

The Service Facade

24

Leveraging Declarative Transactions

26

Understanding Aspect Oriented Programming (AOP)

26

Simplifying Transactions

27

The Benefit of Coding to Interfaces

27

Testing your Persistence Tier

27

Advanced Features and Performance Tuning

28

Hibernate-Search

28

Building a REST Web Service

28

Other Persistence Design Patterns

29

The Template Pattern

29

The Active-Record Pattern

31

Summary

32

CHAPTER 2 Spring Basics

34

Exploring Spring’s Architecture

35

The Application Context

35

Beans, Beans, the Magical Fruit

37

The Spring Life Cycle

37

Understanding Bean Scopes

39

Dependency Injection and Inversion of Control

41

Setter-Based Dependency Injection

41

Constructor-Based Dependency Injection

42

Instance Collaboration

43

Coding to Interfaces

44

Dependency Injection via Autowiring

45

@Annotation-Based Dependency Injection

46

Set It and Forget It!

47

Injecting Code Using AOP and Interceptors

48

Summary

49

CHAPTER 3 Basic Application Setup

50

Application Management with Maven

50

Managed Dependencies

50

Standard Directory Structure

52

POM Deconstruction

52

Spring Configuration

54

Namespace Support

55

Externalizing Property Configurations

55

Component Scanning

55

Import Statements

56

Database Integration

57

JDBC Support

57

Integration with JNDI

58

Web Application Configuration

60

Servlet Definition

61

Spring MVC

62

Summary

64

CHAPTER 4 Persistence with Hibernate

65

The Evolution of Database Persistence in Java

65

EJB, JDO, and JPA

66

How Hibernate Fits In

68

JPA Interface Hierarchy

68

The Art Gallery Domain Model and DAO Structure

70

An @Entity-Annotated POJO

71

Simplified DAO Pattern with Generics

72

The Life Cycle of a JPA Entity

78

JPA Configuration

80

Bare-Bones JPA Setup

80

Spring Integration

82

Summary

84

CHAPTER 5 Domain Model Fundamentals

85

Understanding Associations

85

Building the Domain Model

87

Convention over Configuration

90

Managing Entity Identifiers

91

Using Cascading Options to Establish Data Relationships

92

Adding Second-Level Caching

93

Using Polymorphism with Hibernate

94

Summary

100

CHAPTER 6 DAOs and Querying

101

A Basic Hibernate DAO Implementation

101

Building a DAO

102

Using Spring’s Hibernate Support Classes

103

Enabling Query Caching with the HibernateTemplate

104

Going Template-less

105

Querying in Hibernate

108

Loading an Entity

109

Querying for a Particular Type

109

Using Named Parameters

110

Querying Using Core Hibernate

111

Using Named Queries

112

Working with Polymorphic Queries

112

Persisting Data with Hibernate

113

Saving and Updating Data

113

Handling Binary Data

113

Understanding the Benefits of the Criteria API

114

Using the JPA 2.0 Criteria API

115

Summary

123

CHAPTER 7 Transaction Management

125

The Joy of ACID

126

Understanding Isolation Levels

127

Serializable

128

Repeatable Read

128

Read Committed

129

Read Uncommitted

129

Controlling ACID Reflux

129

Platform Transaction Management

130

Declarative Transaction Management

131

Transactional Annotations

131

Declarative Transactions via XML

135

Programmatic Transaction Management

136

Transactional Examples

137

Creating a Batch Application

137

Using Two Datasources

138

Summary

139

CHAPTER 8 Effective Testing

141

Unit, Integration, and Functional Testing

142

Using JUnit for Effective Testing

143

Unit Testing with Mocks

144

Spring Dependency Injection and Testing

148

Testing with a Database

150

Summary

152

CHAPTER 9 Best Practices and AdvancedTechniques

153

Lazy Loading Issues

153

The N+1 Selects Problem

153

Less Lazy Mappings

155

Batching for Performance

156

Lazy Initialization Exceptions

157

Now Open Late: Keeping EntityManager Open Past Its Bedtime

158

Applying the Open EntityManager Filter

159

Caching

159

Integrating a Caching Implementation

160

Determining Caching Rules

162

Configuring Cache Regions

163

Caching Your Queries

165

Caching in a Clustered Configuration

166

Cluster Caching and Replication Mechanics

167

Configuring Replication

167

Summary

169

CHAPTER 10 Integration Frameworks

171

RESTful Web Services with Spring

171

Nouns, Verbs, and Content-Types

172

Serializing the Object Graph

173

Using the Dreaded DTO Pattern

174

Bootstrapping Dozer

175

Building the DTO Layer

175

Configuring Dozer with Spring

180

Making the Mapping Happen

183

Leveraging Spring 3’s REST Support

184

Marshaling Data with Spring OXM

186

Handling Concurrency

188

Optimistic Locking

188

Pessimistic Locking

188

Free-Text Search

189

Introducing Lucene

190

Indexing with Lucene

190

Querying with Lucene

191

Introducing Hibernate Search

192

Integrating Hibernate Search

193

Adding Hibernate Search Annotations

194

Putting Lucene and Hibernate in Sync

200

Building a Domain-Specific Search

201

Summary

202

CHAPTER 11 GORM and Grails

204

A Crash Course in Groovy

204

Letting Your Types Loose

206

GStrings—Strings on Steroids

206

Default Constructors in Groovy

206

Closures in Groovy

206

Getting Grails Running

208

Installing Grails

208

Creating a Grails Application

208

Configuring Your Application

211

Configuring Your Datasource

212

Mapping URLs

213

Defining the Grails Domain Model

214

Adding Constraints and Validation

215

Defining Associations and Properties

216

Customizing Domain Class Hibernate Mappings

218

Using Active Record As an Alternative to DAOs

219

Looking Under the Hood of GORM

220

Working with Dynamic Finder Methods

220

Creating Advanced Query Methods

225

Using the Criteria API

225

Handling Associations in Grails

226

Scaffolding and Building Your Grails Application

227

Defining a Transactional Service Layer in Grails

228

Summary

229

CHAPTER 12 Spring Roo

230

What Roo Is (and What It Is Not)

230

Creating a Domain Model with Roo

232

Getting Started with Roo

233

Creating a New Project

235

Adding Entities

236

Adding Fields

240

Exploring the Automatically Generated Testing Infrastructure

241

Mapping Associations

243

Modeling Inheritance

243

Adding Spring MVC

245

Adding Service Layers and DAOs

246

Now You See Me, Now You Don’t—Removing Roo

248

Summary

249

Index

250