Java Hibernate Cookbook
eBook - ePub

Java Hibernate Cookbook

  1. 250 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Java Hibernate Cookbook

About this book

Over 50 recipes to help you build dynamic and powerful real-time Java Hibernate applications

About This Book

  • Learn to associate JDBC and Hibernate with object persistence
  • Manage association mappings, implement basic annotations and learn caching
  • Get to grips with Hibernate fundamentals from installation to developing a business application with this step-by-step guide

Who This Book Is For

This is book for Java developers who now want to learn Hibernate. Good knowledge and understanding of Java is preferred to allow efficient programming of the core elements and applications; it would be helpful if readers are familiar with the basics of SQL.

What You Will Learn

  • Set up and install Hibernate on your system and explore different ways in which Hibernate can be configured
  • Learn the basic concepts and fundamentals of Java Hibernate
  • Define mappings without a use of XML file using Annotations
  • Persist collection elements such as list, map, set and array
  • Explore the various mapping options and learn to work with Hibernate associations
  • Understand advanced Hibernate concepts such as caching and inheritance
  • Develop an engaging and robust real-world hibernate application based on a common business scenario
  • Integrate Hibernate with other frameworks to develop robust enterprise applications

In Detail

Hibernate is a database independent technology, so the same code will work for all databases. It helps a Java developer write a query by mapping Java bean to database tables and help create tuned queries that boost performance. Even with limited SQL knowledge one can easily perform database operations. This makes the development faster and more accurate than JDBC. Hibernate supports useful features like connection pooling, caching, and inheritance etc.

This book will provide a useful hands-on guide to Hibernate to accomplish the development of a real-time Hibernate application.

We will start with the basics of Hibernate, which include setting up Hibernate – the pre-requisites and multiple ways of configuring Hibernate using Java. We will then dive deep into the fundamentals of Hibernate such as SessionFactory, session, criteria, working with objects and criteria. This will help a developer have a better understanding of how Hibernate works and what needs to be done to run a Hibernate application. Moving on, we will learn how to work with annotations, associations and collections. In the final chapters, we will see explore querying, advanced Hibernate concepts and integration with other frameworks.

Style and approach

This book is a practical guide filled with carefully organized step-by-step instructions. All recipes are arranged in an easy-to understand and clear manner allowing you to apply the solutions to other situations.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Java Hibernate Cookbook


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
...

Table of contents

  1. Java Hibernate Cookbook

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Java Hibernate Cookbook by Yogesh Prajapati, Vishal Ranapariya in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.