Spring Persistence with Hibernate Build robust and reliable persistence solutions for your enterprise Java application Ahmad Reza Seddighi BIRMINGHAM - MUMBAI This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 Spring Persistence with Hibernate Copyright © 2009 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: November 2009 Production Reference: 1201109 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849510-56-1 www.packtpub.com Cover Image by Vinayak Chittar ([email protected]) This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 Credits Author Editorial Team Leader Ahmad Reza Seddighi Akshara Aware Reviewer Project Team Leader Luca Masini Priya Mukherji Acquisition Editor Project Coordinator Sarah Cullington Zainab Bagasrawala Development Editor Proofreader Rakesh Shejwal Joel T. Johnson Technical Editor Graphics Pallavi Kachare Nilesh Mohite Indexer Production Coordinator Hemangini Bari Shantanu Zagade Cover Work Shantanu Zagade This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 About the Author Ahmad Reza Seddighi is an author, speaker, and consultant in architecting and developing enterprise software systems. He is an IT graduate from the University of Isfahan, Iran, and has ten years experience in software development. He currently lives in Tehran, where he works with a number of small but growing IT companies. He loves teaching so he grabs any teaching opportunities. He is also the author of three other books: Core Java Programming, Java Web Development, and Open Source J2EE Development, all in Farsi. As the book was going to publish by Apress, and prepared and made ready by that, I'd like to thank the Apress crew at first. In particular, Steve Anglin, acquisitions editor; Kylie Johnston, project manager; Matthew Moodie, reviewing editor; Sumit Pal, technical reviewer; Nancy Holzner, copyeditor; Elizabeth Berry, production editor; Brenda Miller, indexer; Gina Rexrode, compositor; April Eddy, proofreader; and April Milne, artist. I should also thank the Packt crew, Sarah Cullington, acquisition editor; Priya Mukherji, project team leader; Rakesh Shejwal, development editor; Luca Massini, technical reviewer; Pallavi Kachare, technical editor; Zainab Bagasrawala, project coordinator; Akshara Aware, editorial team leader; Joel T. Johnson, proofreader; Nilesh Mohite, graphic organizer; Shantanu Zagade, production coordinator; Hemangini Bari, indexer; and Shantanu Zagade, cover work. Special thanks to Sepehr Fatemi for his support in writing and reviewing the book, and sincere regards to my family, and specially my brother, Rohollah, who has always offered excellent support and encouragement. In the end, I express my gratitude to all my friends, whom I've not named here and who have always supported me. Thank you all. This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 About the Reviewer Luca Masini is a Senior Software Engineer and Architect, born as a game developer for Commodore 64 (Footbal Manager) and Commodore Amiga (Ken il guerriero), soon he converted to Object-Oriented programming and for that, from his beginning in 1995, he was attracted by the Java language. He worked on this passion as a consultant for the major Italian banks, developing and integrating the main software projects for which he has often taken the technical leadership. He was able to lead adoption of Java Enterprise in an environment where COBOL was the flagship platform, converting them from mainframe centric to distributed. He then shifted his eyes toward open-source, starting from Linux and then with enterprise frameworks, with which he was able to introduce, with low impact, some concept like IoC, ORM, MVC. For that he was an early adopter of Spring, Hibernate, Struts, and an entire host of other technologies that in the long run have given his customers a technological advantage, and therefore development costs cuts. Lately, however, his attention is completely directed towards the simplification and standardization of development with Java EE, and for this he is working at the ICT of a large Italian company to introduce advanced build tools (Maven and Continuous Integration), archetypes of project and "Agile Development" with plain standards. Dedicated to my friend Enzo, and our common passion. This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 To my parents This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 Table of Contents Preface 1 Chapter 1: An Introduction to Hibernate and Spring 7 Persistence management in Java 8 The mismatch problem 9 Identity and equality mismatch 9 Mapping object inheritance mismatch 15 Mapping more complicated objects 17 Object relational mapping 20 Hibernate as an O/R Mapping solution 20 Other O/R Mapping solutions 22 Hibernate versus other frameworks 23 Hibernate architecture 23 What is Spring 25 Inversion of Control container 25 Aspect-oriented programming framework 26 Data access abstraction 26 Transaction abstraction 27 MVC web framework 27 Testing support 27 Summary 28 Chapter 2: Preparing an Application to Use Spring with Hibernate 29 Setting up the database 30 Getting and installing HSQLDB 30 Configuring and running HSQLDB 30 HSQLDB server modes 31 Managing HSQLDB 32 Getting a Hibernate distribution 34 Getting a Spring distribution 34 This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776 Table of Contents Setting up the project hierarchy 34 Put the required libraries in the lib directory 35 Setting up Ant or Maven in the project 36 Summary 37 Chapter 3: A Quick Tour of Hibernate and Spring 39 Getting started with Hibernate 40 Designing and implementing persistent classes 41 Creating Database Tables 42 Creating mapping metadata 44 A simple client 47 Configuring Hibernate 48 Obtaining a session object 49 Starting a transaction 49 Performing the operation 50 Committing/rolling back the transaction 50 Hibernate declarative configuration 51 Some issues in mapping 53 Caching 53 Querying objects 54 Getting started with Spring 55 A simple case 55 Applying IoC 60 Remove object instantiation and implement the setter method 60 Configure the Student object 61 Obtain the Student instance from Spring 63 Hibernate with Spring 64 Summary 64 Chapter 4: Hibernate Configuration 65 Basic configuration information 65 Hibernate dialect 67 Configuring Hibernate 69 Programmatic configuration 69 Declarative configuration 71 Using a properties file 71 Using an XML file 72 Using a single instance of SessionFactory 74 JPA configuration 76 Summary 78 Chapter 5: Hibernate Mappings 81 Persistent entity classes 82 Having a zero-argument constructor (mandatory) 85 Providing accessors to access class properties (optional) 86 [ ii ] This material is copyright and is licensed for the sole use by Alison Voyvodich on 4th December 2009 12593 80th Avenue N, , Seminole, , 33776