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/Message.java

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

EntityManagerFactory emf =
    Persistence.createEntityManagerFactory("HelloWorldPU");

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

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

2.3. Native Hibernate configuration

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

SessionFactory sessionFactory = new MetadataSources(
    new StandardServiceRegistryBuilder()
        .configure("hibernate.cfg.xml").build()
).buildMetadata().buildSessionFactory();

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

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

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

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

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

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

2.4. Summary

sitemap