Chapter 2. Starting a project

In this chapter

  • Overview of Hibernate projects
  • “Hello World” with Hibernate and Java Persistence
  • Configuration and integration options

In this chapter, you’ll start with Hibernate and Java Persistence using a step-by-step example. You’ll see both persistence APIs and how to benefit from using either native Hibernate or standardized JPA. We first offer you a tour through Hibernate with a straightforward “Hello World” application. Before you start coding, you must decide which Hibernate modules to use in your project.

2.1. Introducing Hibernate

Hibernate is an ambitious project that aims to provide a complete solution to the problem of managing persistent data in Java. Today, Hibernate is not only an ORM service, but also a collection of data management tools extending well beyond ORM.

The Hibernate project suite includes the following:

2.2. “Hello World” with JPA

Path: /model/src/main/resources/META-INF/persistence.xml

Path: /model/src/main/java/org/jpwh/model/helloworld/

Path: /examples/src/est/java/org/jpwh/helloworld/

EntityManagerFactory emf =

Path: /examples/src/test/java/org/jpwh/helloworld/

Path: /examples/src/test/java/org/jpwh/helloworld/

2.3. Native Hibernate configuration

Path: /examples/src/test/java/org/jpwh/helloworld/

SessionFactory sessionFactory = new MetadataSources(
    new StandardServiceRegistryBuilder()

Path: /examples/src/test/java/org/jpwh/helloworld/

Path: /examples/src/test/java/org/jpwh/helloworld/

Path: /examples/src/test/java/org/jpwh/helloworld/

Metadata metadata =;
assertEquals(metadata.getEntityBindings().size(), 1);
SessionFactory sessionFactory = metadata.buildSessionFactory();

Path: /examples/src/ test/java/org/jpwh/helloworld/

Path: /examples/src/test/java/org/jpwh/helloworld/

2.4. Summary