Java Hibernate Cookbook
()
About this ebook
Related to Java Hibernate Cookbook
Related ebooks
Spring MVC Cookbook Rating: 0 out of 5 stars0 ratingsApache Maven Cookbook Rating: 0 out of 5 stars0 ratingsSpring Cookbook Rating: 0 out of 5 stars0 ratingsMeteor: Full-Stack Web Application Development Rating: 0 out of 5 stars0 ratingsMultithreading in C# 5.0 Cookbook Rating: 0 out of 5 stars0 ratingsSelenium Testing Tools Cookbook Rating: 3 out of 5 stars3/5Mastering Hibernate Rating: 0 out of 5 stars0 ratingsJava 9 Concurrency Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Java EE Development with WildFly Rating: 0 out of 5 stars0 ratingsLearning Reactive Programming with Java 8 Rating: 0 out of 5 stars0 ratingsSpring Essentials Rating: 0 out of 5 stars0 ratingsSpring Boot 2: How To Get Started and Build a Microservice - Third Edition Rating: 5 out of 5 stars5/5Java Testing with Spock Rating: 0 out of 5 stars0 ratingsBuilding a RESTful Web Service with Spring Rating: 5 out of 5 stars5/5Angular Development with TypeScript Rating: 0 out of 5 stars0 ratingsMastering Concurrency Programming with Java 8 Rating: 0 out of 5 stars0 ratingsJava Persistence with Hibernate Rating: 4 out of 5 stars4/5Spring Microservices Rating: 0 out of 5 stars0 ratingsRESTful Java Web Services - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Unit Testing Using Mockito and JUnit Rating: 0 out of 5 stars0 ratingsSpring MVC Beginner’s Guide Rating: 2 out of 5 stars2/5Java Multithreading Interview Questions And Answers Rating: 0 out of 5 stars0 ratingsModern Java in Action: Lambdas, streams, functional and reactive programming Rating: 0 out of 5 stars0 ratingsMastering Spring 5.0 Rating: 5 out of 5 stars5/5Building Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsOCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 Rating: 5 out of 5 stars5/5Angular in Action Rating: 0 out of 5 stars0 ratingsTesting with JUnit Rating: 0 out of 5 stars0 ratingsSpring in Action, Sixth Edition Rating: 5 out of 5 stars5/5
Software Development & Engineering For You
Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsHand Lettering on the iPad with Procreate: Ideas and Lessons for Modern and Vintage Lettering Rating: 4 out of 5 stars4/5Python For Dummies Rating: 4 out of 5 stars4/5Agile Project Management: Scrum for Beginners Rating: 4 out of 5 stars4/5Beginning C++ Programming Rating: 3 out of 5 stars3/5Photoshop For Beginners: Learn Adobe Photoshop cs5 Basics With Tutorials Rating: 0 out of 5 stars0 ratingsAgile: What You Need to Know About Agile Project Management, the Kanban Process, Lean Thinking, and Scrum Rating: 5 out of 5 stars5/5Lua Game Development Cookbook Rating: 0 out of 5 stars0 ratingsHacking for Beginners: Mastery Guide to Learn and Practice the Basics of Computer and Cyber Security Rating: 0 out of 5 stars0 ratingsHow to Write Effective Emails at Work Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Level Up! The Guide to Great Video Game Design Rating: 4 out of 5 stars4/5Adobe Illustrator CC For Dummies Rating: 5 out of 5 stars5/5Tiny Python Projects: Learn coding and testing with puzzles and games Rating: 5 out of 5 stars5/5Learning Python Rating: 5 out of 5 stars5/5Android App Development For Dummies Rating: 0 out of 5 stars0 ratingsReversing: Secrets of Reverse Engineering Rating: 4 out of 5 stars4/5Beginning Programming For Dummies Rating: 4 out of 5 stars4/5How Do I Do That in Photoshop?: The Quickest Ways to Do the Things You Want to Do, Right Now! Rating: 4 out of 5 stars4/53D Printing For Dummies Rating: 4 out of 5 stars4/5Thinking Beyond Coding Rating: 5 out of 5 stars5/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5How Do I Do That In InDesign? Rating: 5 out of 5 stars5/5Ry's Git Tutorial Rating: 0 out of 5 stars0 ratingsData Analytics with Python: Data Analytics in Python Using Pandas Rating: 3 out of 5 stars3/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5
Reviews for Java Hibernate Cookbook
0 ratings0 reviews
Book preview
Java Hibernate Cookbook - Prajapati Yogesh
Table of Contents
Java Hibernate Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Setting Up Hibernate
Introduction
Getting the required libraries for hibernate
Manually downloading
How to do it…
Using Maven
How to do it…
Creating a hibernate persistent class
Getting ready
How to do it...
There's more…
Providing an XML-based hibernate mapping
How to do it…
How it works…
There's more…
Providing an annotation-based hibernate mapping
How to do it…
How it works…
Declaring a class — Table for the database
Declaring an ID — The primary key for the table
Referencing an object
There's more…
Providing a hibernate configuration using an XML file
How to do it...
How it works...
There's more…
Providing a hibernate configuration using the properties file
How to do it…
How it works…
Configuring hibernate programmatically
How to do it…
How it works…
2. Understanding the Fundamentals
Introduction
Building a SessionFactory
How to do it…
How it works…
There's more…
Creating a generic SessionFactory provider class
How to do it…
How it works…
Opening a new session
Getting ready
How to do it…
How it works…
There's more…
Opening a stateless session
How to do it…
Saving an object to the database
How to do it…
How it works…
There's more…
Fetching an object from the database
How to do it…
How it works…
There's more…
Scenario 1
Code for scenario 1
Output for scenario 1
Explanation for scenario 1
Scenario 2
Code for scenario 2
Output for scenario 2
Explanation for scenario 2
Scenario 3
Code for scenario 3
Output for scenario 3
Explanation for scenario 3
Removing an object from the database
How to do it…
Code
Output
How it works…
Updating an object
How to do it…
Code
Output
How it works…
There's more...
Creating a criteria
How to do it...
Code
Output
How it works…
Restricting the results using a criteria
How to do it…
Code
Output
How it works…
Pagination using a criteria
How to do it...
Code
Output
How it works…
Sorting the results
How to do it…
Code
Output
How it works…
Transforming a result
How to do it...
Scenario 1: Converting a result to List
Scenario 2: Converting a result to Map
Code
Output
Scenario 3: Converting a result to user-defined Bean
Code
Output
Using basic projection
How to do it...
Scenario 1:
Code
Output
Scenario 2:
Code
Output
3. Basic Annotations
Introduction
Declaring a class as an entity and creating a table in the database – @Entity and @Table
How to do it…
How it works…
Creating a column in the table – @Column
How to do it…
There's more…
length
nullable
unique
columnDefinition
scale and precision
insertable and updatable
See also…
Creating a primary key and composite primary key column – @Id and @IdClass
How to do it…
Creating an autogenerator column
How to do it…
Default generation strategy
Sequence generator
Table generator
There's more…
Attributes available in the @GeneratedValue annotation
Strategy
Generator
Attributes available in @TableGenerator annotation
name
table
pkColumnName
valueColumnName
pkColumnValue
initialValue
allocationSize
4. Working with Collections
Introduction
Persisting List
Getting ready
Creating tables
Creating a class
How to do it…
Inserting a record
Code
Output
Retrieving a record
Code
Output
Updating a record
Code
Output
Deleting a record
Code
Output
How it works…
There's more…
Creating classes
Creating tables
Inserting a record
Code
Output
Retrieving a record
Code
Output
Persisting Set
Getting ready
Creating a class
How to do it…
Inserting a record
Code
Output
Retrieving a record
Code
Output
Updating a record
Code
Output
Deleting a record
Code
Output
How it works…
Persisting Map
Getting ready
Creating tables
Creating a class
How to do it…
Inserting a record
Code
Output
Retrieving a record
Code
Output
Updating a record
Code
Output
Deleting a record
Code
Output
How it works…
Persisting Array
Getting ready
How to do it…
Inserting a record
Code
Output
Retrieving a record
Code
Output
5. Working with Associations
Introduction
One-to-one mapping using foreign key association
Getting ready
Creating the tables
Creating the classes
How to do it…
Inserting a record
Code
Output
How it works...
There's more…
Creating the tables
Creating the classes
Inserting a record
Code
Output
Retrieving a record using the parent record
Code
Output
Retrieving a record using the child record
Code
Output
One-to-one mapping using a common join table
Getting ready
Creating the tables
Creating the classes
How to do it…
Inserting a record
Code
Output
How it works…
One-to-one mapping using a common primary key
Getting ready
Creating the tables
Creating the classes
How to do it…
Inserting a record
Code
Output
How it works…
One-to-many mapping or many-to-one mapping
Getting ready
Creating the tables
Creating the classes
How to do it…
Inserting a record
Code
Output
Retrieving a record – many-to-one-mapping
Code
Output
Retrieving a record – one-to-many-mapping
Code
Output
How it works…
Many-to-many mapping
Getting ready
Creating the tables
Creating the classes
How to do it…
Inserting a record
Code
Output
Retrieving a record using Developer with Technology
Code
Output
Retrieving a record using Technology with Developers
Code
Output
How it works…
6. Querying
Introduction
Working with an alias
How to do it…
How it works…
There's more…
Performing aggregate operations
Getting ready
Creating the tables
Creating the classes
Inserting data in the tables
How to do it…
Sum
Code
Output
Avg
Code
Output
Min
Code
Output
Max
Code
Output
Count
Code
Output
Executing a subquery using a criteria
How to do it…
Code
Output
How it works…
Executing a native SQL query
How to do it…
Scalar queries
Code
Output
Entity queries
Code
Output
Executing a query using HQL
How to do it…
The FROM clause
Code
Output
Code
Output
The SELECT clause
Code
Output
How it works…
There's more…
The WHERE clause
The ORDER BY clause
The GROUP BY clause
Using formula in hibernate
How to do it…
Code
Output
How it works…
There's more…
Code
Output
Working with NamedQuery
How to do it…
name
query
Code
Output
There's more…
7. Advanced Concepts
Introduction
Working with a first-level cache
Getting ready
Creating the classes
Creating the tables
How to do it…
Code
Output
How it works…
There's more…
evict (Object object)
Code
Output
clear()
Code
Output
Working with a second-level cache
Getting ready
Enabling a second-level cache
Adding a caching strategy using a POJO class
How to do it…
Code
Output
How it works…
Working with a query cache
Getting ready
Enabling a query cache:
How to do it…
How it works…
There's more...
Code
Output
Working with the table per class hierarchy strategy of inheritance
Getting ready
Creating the classes
Creating the tables
Annotations used in Employee.java
Annotations used in ContractualEmployee.java
Annotations used in PermanentEmployee.java
How to do it…
Code
Output
How it works…
Working with the table per subclass strategy of inheritance
Getting ready
Creating the classes
Creating the tables
How to do it…
Code
Output
How it works…
Working with the table per concrete class strategy of inheritance
Getting ready
Creating the classes
Creating the tables
Annotations used in Employee.java
Annotations used in PermanentEmployee.java and ContractualEmployee.java
How to do it…
Code
Output
How it works…
Working with the versioning of objects
Getting ready
Creating the classes
Creating the tables
How to do it…
Code
Output
How it works…
Code
Output
Maintaining the history of an object
Getting ready
Creating the classes
Creating the tables
How to do it…
Code
Output
How it works…
There's more…
Changing the suffix by changing the property in the configuration file
Changing the prefix by changing the property in the configuration file
Renaming the REV column
Renaming the REVTYPE column
Working with an interceptor
Getting ready
Creating the classes
Creating the tables
How to do it…
Code
Output:
How it works…
The working of InterceptorTest.java
The working of CustomInterceptor.java
The working of onSave Method
The working of preFlush Method
The working of postFlush Method
Working with batch processing
Getting ready
Creating the classes
How to do it…
How it works…
8. Integration with Other Frameworks
Introduction
Integration with Spring
Getting ready
The project dependencies
Creating the tables
Creating a model class
Creating an interface – DAO
The Spring configuration
Reviewing the project structure
How to do it…
The DAO implementations
Code
Output
How it works…
Integration with Struts
Getting ready
The project dependencies
The hibernate configuration
Creating the tables
Creating a model class
Creating an interface – DAO
The Struts configuration
The deployment descriptor
Creating an Action class
The Struts Action mapping
Reviewing the project structure
How to do it…
Implementing a DAO
Creating view – JSP
How it works…
See also…
Index
Java Hibernate Cookbook
Java Hibernate Cookbook
Copyright © 2015 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 authors, 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: September 2015
Production reference: 1110915
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-190-4
www.packtpub.com
Credits
Authors
Yogesh Prajapati
Vishal Ranapariya
Reviewers
Mathieu Nayrolles
Ravi Sharma
Umamaheswaran T.G
Commissioning Editor
Taron Pereira
Acquisition Editor
Kevin Colaco
Content Development Editor
Samantha Gonsalves
Technical Editor
Siddhesh Ghadi
Copy Editor
Shruti Iyer
Project Coordinator
Sanchita Mandal
Proofreader
Safis Editing
Indexer
Monica Ajmera Mehta
Graphics
Disha Haria
Production Coordinator
Nilesh R. Mohite
Cover Work
Nilesh R. Mohite
About the Authors
Yogesh Prajapati is a Java programmer. He has more than 4 years of experience in implementing enterprise web applications using J2EE technologies. Yogesh has experience in technologies and frameworks such as Spring, Hibernate, RESTful Web Services, and MongoDB.
He pursued a BCA from Ganpat University, Kherva, Gujarat, India. In 2011, Yogesh obtained a master's degree in computer application from Gujarat University, Gujarat, India.
Yogesh's expertise is not limited to a particular domain; he has worked with clients in the transportation, finance, and health care domains. Apart from this, he has had experience in GIS development and has also worked in the hospitality industry.
Yogesh has a blog, http://kode12.com, where he shares his knowledge of Java and other technologies. He is interested in contributing to open source technologies and is enthusiastic about building new products and services. You can follow Yogesh on his blog and on Linkedin at http://in.linkedin.com/in/yogeshmprajapati.
First, I would like to thank all those who helped me personally and professionally to complete this book.
I would also like to thank my wife, Varsha, for her valuable support as she understood me and helped me finish this book. I would like to extend a big thank you to my baby boy, Aarush. Apart from these two people, I would like to thank my parents, Manoj and Vina Prajapati, who continuously motivated me while writing. Also, I would like thank my sisters, Sonal and Shital, who always support me to fulfill my dreams.
Lastly, a special shout-out goes to god, who gives me the power to accomplish such tasks. Last but not least, I would like to extend a very special thanks to the Java and Hibernate technologies for making life easier.
Vishal Ranapariya is a Java developer. He has more than 4 years of experience in implementing enterprise web applications using J2EE technology. He has experience with technologies and frameworks such as Java, J2EE, Spring, Hibernate, RESTful web services, MongoDB, and Core Hadoop.
Vishal pursued a BCA from Gujarat University, Gujarat, India. In 2011, he obtained a master's degree in computer application from Gujarat University, Gujarat, India.
Vishal has blogs at http://kode12.com, where he shares his knowledge of Java-related open source technologies. He is interested in contributing to open source technologies. Vishal is enthusiastic about building new products and services. You can follow Vishal on his blog and on LinkedIn at https://in.linkedin.com/in/vishalranapariya.
Firstly, I would like to thank all those who helped me personally and professionally to complete this book. I would also like to thank my parents, who continuously motivated me while writing this book.
About the Reviewers
Mathieu Nayrolles was born in France and lived in a small village in Côte d'Azur for almost 15 years. He began his studies in computer science in France and continued with them in Montréal, Canada, where he now lives with his wife. Mathieu holds two master's degrees in eXia.Cesi (software engineering) and UQAM (computer science) respectively. He is now a PhD student at Concordia University (electrical and computer engineering), Montréal, Canada, under the supervision of Dr. Wahab Hamou-Lhadj.
Despite his academic journey, Mathieu has been consulting as a Magento performances specialist since the release of Magento 1.6 (August 2011) and has also worked for companies worldwide, such as Eurocopter and Saint-Gobain, where he learned how important good technical resources are.
You can discover some of Mathieu's work through his books, Instant Magento Performance Optimization How-to, Magento Site Performance Optimization, Mastering Apache Solr, and Xamarin Studio for Android Programming: A C# Cookbook or its latest editions, https://bumper-app.com, https://mindup.io/ and https://toolwatch.io/.
You can follow Mathieu on Twitter at @MathieuNls.
Ravi Sharma is a software professional with over 12 years of experience, working with various companies such as Oracle, Yahoo, and Tier-1 Banks and in the mobile advertising sector.