CHAPTER 2 Starting a project // Load settings (Sri lanka web server)
CHAPTER 2 Starting a project // Load settings from hibernate.properties AnnotationConfiguration cfg = new AnnotationConfiguration(); // … set other configuration options programmatically cfg.addClass(hello.Message.class); cfg.addPackage(”hello”); SessionFactory sessionFactory = cfg.buildSessionFactory(); Let s take this one step further and replace the native Hibernate code that loads and stores messages with code that uses JPA. With Hibernate Annotations and Hibernate EntityManager, you can create portable and standards-compliant mappings and data access code. 2.2.2 Using Hibernate EntityManager Hibernate EntityManager is a wrapper around Hibernate Core that provides the JPA programming interfaces, supports the JPA entity instance lifecycle, and allows you to write queries with the standardized Java Persistence query language. Because JPA functionality is a subset of Hibernate s native capabilities, you may wonder why you should use the EntityManager package on top of Hibernate. We ll present a list of advantages later in this section, but you ll see one particular simplification as soon as you configure your project for Hibernate EntityManager: You no longer have to list all annotated classes (or XML mapping files) in your configuration file. Let s modify the Hello World project and prepare it for full JPA compatibility. Basic JPA configuration A SessionFactory represents a particular logical data-store configuration in a Hibernate application. The EntityManagerFactory has the same role in a JPA application, and you configure an EntityManagerFactory (EMF) either with configuration files or in application code just as you would configure a SessionFactory. The configuration of an EMF, together with a set of mapping metadata (usually annotated classes), is called the persistence unit. The notion of a persistence unit also includes the packaging of the application, but we want to keep this as simple as possible for Hello World ; we ll assume that you want to start with a standardized JPA configuration and no special packaging. Not only the content, but also the name and location of the JPA configuration file for a persistence unit are standardized. Create a directory named WORKDIR/etc/META-INF and place the basic configuration file named persistence.xml, shown in listing 2.11, in that directory:
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.