2 Replies Latest reply on Nov 2, 2006 5:13 PM by jantzen

    Trouble Configuring a Persistence Unit

    jantzen Newbie

      Hi, I'm experimenting with the new Java Persistence API interface to Hibernate and running into problems rather fast.

      My basic test class:

      package datatier;
      
      import javax.persistence.EntityManager;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;
      
      public class DataTierTest {
      
       public static void main(String[] args) {
       Object file = ClassLoader.getSystemClassLoader().getResource("META-INF/persistence.xml");
       System.out.println("FILE " + file);
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
       EntityManager manager = emf.createEntityManager();
       }
      


      My META-INF/persistence.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
       version="1.0">
       <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="validate"/>
       <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
       <property name="hibernate.connection.password" value="password"/>
       <property name="hibernate.connection.url" value="jdbc:postgresql:TestDB"/>
       <property name="hibernate.connection.username" value="user"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
       <property name="hibernate.default_schema" value="authority"/>
       </properties>
       </persistence-unit>
      </persistence>
      


      From within Eclipse and as a Jar running from the command line, the above results in:
      FILE file:/home/jantzen/eclipse/DataTier/bin/META-INF/persistence.xml
      Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:41)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
       at org.throbbing.datatier.DataTierTest.main(DataTierTest.java:13)
      


      I've verified that META-INF is in the classpath (which the ClassLoader.getResource() call demonstrates as well), so I'm perplexed. Something simple I'm missing probably.

      Thanks.