Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Java Hibernate Cookbook

You're reading from  Java Hibernate Cookbook

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781784391904
Pages 250 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Java Hibernate Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Setting Up Hibernate 2. Understanding the Fundamentals 3. Basic Annotations 4. Working with Collections 5. Working with Associations 6. Querying 7. Advanced Concepts 8. Integration with Other Frameworks Index

Building a SessionFactory


First, we will discuss SessionFactory and how to create it in detail. As the name suggests, a SessionFactory is a factory of sessions.

A SessionFactory has the following features:

  • It's an interface implemented using the singleton pattern.

  • It's created using the configuration provided by the configuration file.

  • It's thread-safe, so it's created once during the application's lifetime, and multiple users or threads can access it at the same time without any concurrency issue.

  • As a SessionFactory object is immutable, changes made to the configuration will not affect the existing factory object.

  • It's a factory class, and its main duty is to create, manage, and retrieve a session on request. A Session is used to get a physical connectivity with the database.

How to do it…

If you are using a version of hibernate that is earlier than 4, use the following code to create a SessionFactory:

/* Line 1 */ Configuration cfg = new Configuration();
/* Line 2 */ cfg = cfg.configure();
/*...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}