JPA+Tutorial+1+-+Persistence+Unit

We now need to create the Persistent Unit definition. Create a file called persistence.xml in the src/META-INF directory with the following contents:



persistence.xml
code format="xml"   

  

   code

The Steps

 * 1) Expand your project (**JpaTutorial1**)
 * 2) Select the **src/META-INF** directory
 * 3) Right click and select **new:File**
 * 4) Enter **persistence.xml** for the name and press **Finish** (Note: all lowercase. It won't make a difference on Windows XP but it will on Unix.)
 * 5) Copy the contents (above) into the file and save it.

Verify This Works
code format ="java5" package entity;
 * 1) Select the **test**
 * 2) Right-click on **entity** and select **new:Class**
 * 3) Enter **PersonTest** and click **Finish**
 * 4) Enter the example code below:

import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence;

import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test;

public class PersonTest { private EntityManagerFactory emf;

private EntityManager em;

@Before public void initEmfAndEm { BasicConfigurator.configure; Logger.getLogger("org").setLevel(Level.ERROR);

emf = Persistence.createEntityManagerFactory("examplePersistenceUnit"); em = emf.createEntityManager; }

@After public void cleanup { em.close; }

@Test public void emptyTest { } } code

code format="java5" Logger.getLogger("org").setLevel(Level.ERROR); code
 * 1) When you're finished and it all compiles, right-click within the source pane, select **Run As:JUnit Test**
 * 2) You should see all green
 * 3) If you do not, comment out the following line and review the console output