geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Viet Nguyen" <vhng...@gmail.com>
Subject Re: Trying to create EntityManager in web application
Date Wed, 28 May 2008 13:22:29 GMT
Hi,

I will make comments inline...

On Wed, May 28, 2008 at 8:37 AM, Phani Madgula
<phanibalaji.madgula@gmail.com> wrote:
> Hi,
>
> I was trying to obtain javax.persistence.EntityManager in a web application
> as follows.
>
> 1. Created persistence.xml in META-INF folder.

Are you sure the persistence.xml is in the {ejb jar}/META-INF folder
and not the another META-INF?

> __________________
> <?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"
> version="1.0"
>                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
> http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
>     <persistence-unit name="CurrencyRateUnit">
>         <description>JPA JSF Sample</description>
>
> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
>         <class>sample.jpa.currency.Currency</class>
>         <properties>
>             <property name="openjpa.ConnectionURL"
> value="jdbc:derby:CurrencyDB" />
>             <property name="openjpa.ConnectionDriverName"
> value="org.apache.derby.jdbc.EmbeddedDriver" />
>             <property name="ConnectionUserName" value="app" />
>             <property name="openjpa.jdbc.SynchronizeMappings" value="false"
> />
>         </properties>
>     </persistence-unit>
> </persistence>
>
> ______________________________________________________________________
>
> 2. I have a utility class in the web application which tries to obtain
> EntityManager as follows.
>
> ________________
> public class CurrencyUtil {
>
>     //@PersistenceContext(unitName="CurrencyRateUnit")
>     private EntityManager em;

You can also try

@PersistenceUnit(unitName="CurrencyRateUnit")
private EntityManagerFactory emf;

>
>     private Collection currencies;
>
>
>     public CurrencyUtil()
>     {
>         EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("CurrencyRateUnit");
>         if(emf == null) System.out.println("emf is null!!!");
>         em = emf.createEntityManager();
>         if(em == null) System.out.println("em is null!!!");
>     }
> ________________________________________
>
> I have also tried with the above code in a servlet.
>
> In all the cases I get the following error.
>
> __________________________________________________________________________________________
> 18:02:36,328 ERROR [[Test]] Servlet.service() for servlet Test threw
> exception
> <openjpa-1.0.1-r420667:592145 fatal user error>
> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or
> DataSource class name must be specified in the ConnectionDriverName
> property.
>     at
> org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
>     at
> org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:803)
>     at
> org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:568)
>     at
> org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1221)
>     at
> org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:476)
>     at
> org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:401)
>     at
> org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:102)
>     at
> org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:82)
>     at
> org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:861)
>     at
> org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:852)
>     at
> org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:630)
>     at
> org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:169)
>     at
> org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
>     at
> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
>     at
> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
>     at
> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
>     at sample.jpa.currency.Test.doGet(Test.java:33)
>     at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
>     at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
>     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>     at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>     at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
>     at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
>     at
> org.apache.geronimo.tomcat.valve.DefaultSubjectValve.invoke(DefaultSubjectValve.java:56)
>     at
> org.apache.geronimo.tomcat.GeronimoStandardContext$SystemMethodValve.invoke(GeronimoStandardContext.java:406)
>     at
> org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke(GeronimoBeforeAfterValve.java:47)
>     at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
>     at
> org.apache.catalina.ha.session.JvmRouteBinderValve.invoke(JvmRouteBinderValve.java:209)
>     at
> org.apache.catalina.ha.tcp.ReplicationValve.invoke(ReplicationValve.java:347)
>     at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
>     at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
>     at
> org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
>     at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
>     at
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
>     at
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
>     at
> org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
>     at java.lang.Thread.run(Thread.java:803)
> _______________________________________________________________________________________________
>
> Looks like, JPA is not able to find persistence.xml and read the value of
> "ConnectionDriverName" property.
>
> I have also tried to use @PersistenceContext(unitName="CurrencyRateUnit")
> annotation but failed. It throws the below exception
>
> ____________________
>  javax.faces.FacesException: java.lang.InstantiationException: Some objects
> to be injected were not found in jndi: [javax.naming.NotContextException:
> sample.jpa.currency.CurrencyUtil/em]
>
> _________________________
>
> How do we get the proper EntityManager Instance in the web application??
>
> Thanks in advance!!
> Phani
>
>

Thanks,
Viet

Mime
View raw message