I am not able to execute a simple helloworld program using EntityManager.
Please tell me whats the problem :
Here is my META-INF/persistence.xml in src/main/resource :
Code:
<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.xml -->
<persistence-unit name="helloworld">
<provider>org.hibernate.ejb.HibernatePeristence</provider>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:krsna"/>
<property name="hibernate.connection.username" value="scott"/>
<property name="hibernate.connection.password" value="tiger"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Following is my java code where I am first displaying CLASSPATH to confirm that hibernate-entitymanager.jar as well as META-INF/persistence.xml ( which resides in target/classes ) are on the CLASSPATH.
Quote:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import arun.eveningtea.domainobjects.Employee;
import java.net.URL;
import java.net.URLClassLoader;
public class EMPersistDemo {
static Log log = LogFactory.getLog(EMPersistDemo.class);
public static void main(String[] args) throws Exception{
printClassPath();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
// First unit of work
EntityManager em = emf.createEntityManager();
EntityTransaction etx = em.getTransaction();
etx.begin();
Employee emp = new Employee();
emp.setEname("JPA Candidate");
emp.setAge(57L);
em.persist(emp);
etx.commit();
em.close();
// Second unit of work
em = emf.createEntityManager();
etx = em.getTransaction();
etx.begin();
List<Employee> employees = em.createQuery("select e from Employee e order by e.name asc").getResultList();
log.info( employees.size() + " employees found. ");
for( Employee e : employees){
log.info( e );
}
etx.commit();
em.close();
}
private static void printClassPath() {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
//Get the URLs
URL[] urls = ((URLClassLoader)systemClassLoader).getURLs();
for(int i=0; i< urls.length; i++)
{
System.out.println(urls[i].getFile());
}
}
}
Output :
/D:/arun/mavenprojects/hibernateHawa/target/classes//C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/commons-logging/commons-logging/1.0.4/commons-logging-1.0.4.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/hsqldb/hsqldb/1.8.0.7/hsqldb-1.8.0.7.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/org/hibernate/hibernate/3.2.2.ga/hibernate-3.2.2.ga.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/net/sf/ehcache/ehcache/1.2.3/ehcache-1.2.3.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/commons-collections/commons-collections/2.1.1/commons-collections-2.1.1.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/javax/transaction/jta/1.0.1B/jta-1.0.1B.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/asm/asm-attrs/1.5.3/asm-attrs-1.5.3.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/antlr/antlr/2.7.6/antlr-2.7.6.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/cglib/cglib/2.1_3/cglib-2.1_3.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/asm/asm/1.5.3/asm-1.5.3.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/org/hibernate/hibernate-annotations/3.2.1.ga/hibernate-annotations-3.2.1.ga.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/javax/persistence/persistence-api/1.0/persistence-api-1.0.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/org/hibernate/hibernate-entitymanager/3.2.1.ga/hibernate-entitymanager-3.2.1.ga.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/jboss/javassist/3.3.ga/javassist-3.3.ga.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/jboss/jboss-archive-browsing/5.0.0alpha-200607201-119/jboss-archive-browsing-5.0.0alpha-200607201-119.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/org/hibernate/hibernate-tools/3.2.0.beta9a/hibernate-tools-3.2.0.beta9a.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/freemarker/freemarker/2.3.4/freemarker-2.3.4.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/org/hibernate/jtidy/r8-20060801/jtidy-r8-20060801.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/org/springframework/spring/2.5.6.SEC02/spring-2.5.6.SEC02.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/c3p0/c3p0/0.9.1/c3p0-0.9.1.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/commons-dbcp/commons-dbcp/1.2.2/commons-dbcp-1.2.2.jar
/C:/Documents%20and%20Settings/arkandre.ST-USERS/.m2/repository/commons-pool/commons-pool/1.3/commons-pool-1.3.jar
Feb 21, 2011 8:44:35 PM org.hibernate.ejb.Version <clinit>
INFO: Hibernate EntityManager 3.2.1.GA
Feb 21, 2011 8:44:35 PM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.2.1.GA
Feb 21, 2011 8:44:35 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.2
Feb 21, 2011 8:44:35 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Feb 21, 2011 8:44:35 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Feb 21, 2011 8:44:35 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named [b]helloworld
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
at EMPersistDemo.main(EMPersistDemo.java:25)[/b]
Quote:
I tried my best to find out why I am getting this error.
Most of the google results show that either I am missing META-INF/persistence.xml in CLASSPATH or I am not having hibernate-entitymanager.jar in CLASSPATH.
But it is very clear that I have target\classes\META-INF\persistence.xml and target\classes is in CLASSPATH as is evident from above.
It is also clear that hibernate-entitymanager.jar is in the CLASSPATH. I dont know why I am still getting this error.
Please some one help me.
POM.xml :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>persistence-deps</groupId>
<artifactId>persistence-deps</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.2.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.2.1.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.2.1.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>3.2.0.beta9a</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>[2.5,3)</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>hello.HelloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<!--
<repositories>
<repository>
<id>scriptlandia-repo</id>
<name>Scriptlandia Maven2 repository</name>
<url>http://scriptlandia-repository.googlecode.com/svn/trunk/tools</url>
</repository>
</repositories>
-->
</project>