Book Image

Java Hibernate Cookbook

Book Image

Java Hibernate Cookbook

Overview of this book

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.
Table of Contents (15 chapters)
Java Hibernate Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Declaring a class as an entity and creating a table in the database – @Entity and @Table


We need a class to be declared as an entity for hibernate to use it. Hibernate considers the class as a persistent class if it is annotated with the @Entity annotation.

How to do it…

Perform the following steps to declare a class as a hibernate entity:

  1. Enter the following code on your editor:

    @Entity
    public class Employee {
      // Fields and getter/setter
    }

    Here, we annotate a class, Employee, with the @Entity annotation. As a result, hibernate considers the current class eligible to be persisted.

    Note

    If you build a session factory with the preceding code and the table name is not given, hibernate will create a table with the name employee, which is equal to the class name.

  2. If we want a user-defined table name rather than a default name, we can use the @Table annotation. The following code shows us how to achieve this:

    @Entity
    @Table(name="tbl_employee")
    public class Employee {
      // Fields and getter/setter
    ...