A persistence unit is a pairing of your domain model class mappings with a database connection, plus some other configuration settings. It is the starting point in JPA. The persistence unit is usually defined in persistence.xml.

<persistence 
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    version="2.1" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence         
    http://xmlns.jcp.org/xml/ns/persistence_2_1.xsd">
   <persistence-unit name="HelloWorldPU">
      <jta-data-source>myDS</jta-data-source>
      <class>me.naveen.helloworld.User</class>
      <exclude-unlisted-classes>true</exclude-unlisted-classes>
      <properties>
         <property 
            name="javax.persistence.schema-generation.database.action" 
            value="drop-and-create" 
         />
         <property name="hibernate.format_sql" value="true" />
         <property name="hibernate.use_sql_comments" value="true" />
      </properties>
   </persistence-unit>
</persistence>
  • The persistence.xml file configures at least one persistence unit; each unit must have a unique name.
  • Each persistence unit must have a database connection. Here you delegate to an existing java.sql.DataSource . Hibernate will find the data source by name with a JNDI lookup on startup.
  • A persistent unit has persistent (mapped) classes. You list them here.
  • Standard or vendor-specific options can be set as properties on a persistence unit. Any standard properties have the javax.persistence name prefix; Hibernate’s settings use hibernate .
  • The JPA engine should drop and re-create the SQL schema in the database automati- cally when it boots. This is ideal for automated testing, when you want to work with a clean database for every test run.
  • An application could have more than one persistence units.

Let's now see how to map an Object to a table.