Hi Anish,
The packaging for war files is a little different than you might think.
Instead of putting persistence.xml in ${war.location}/META-INF/ you need to
put it in ${war.location}/WEB-INF/classes/META-INF. If persistence.xml is in
a jar file then it should be in the META-INF directory of that jar file, and
the jar file should be in ${war.location}/lib/.
Here's the complete list of acceptable places for persistence.xml in a JEE
environment :
A persistence unit is defined by a persistence.xml file. The jar file or
directory whose META-INF
directory contains the persistence.xml file is termed the root of the
persistence unit. In Java EE,
the root of a persistence unit may be one of the following:
• an EJB-JAR file
• the WEB-INF/classes directory of a WAR file[40]
• a jar file in the WEB-INF/lib directory of a WAR file
• a jar file in the root of the EAR
• a jar file in the EAR library directory
• an application client jar file
<snip>
[40] The root of the persistence unit is the WEB-INF/classes directory; the
persistence.xml file is therefore contained in the
WEB-INF/classes/META-INF directory.
Hope this helps,
-mike
On Sun, Aug 3, 2008 at 4:47 PM, Anish <[EMAIL PROTECTED]> wrote:
>
> Hey Kevin,
> Persistence.xml is in the META-INF directory.... and think is package with
> the other Entities.... I have made this in eclipse so the IDE puts all
> entities in the source folder and the jsp in the WebContent folder.... i
> think all have been packaged correctly....is there anyway i can check that?
>
>
>
> Kevin Sutter wrote:
> >
> > So, it looks like an NPE is happening when attempting to use the emf (at
> > line 21):
> >
> >> 20: emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
> >> 21: em = emf.createEntityManager();
> >
> > This would indicate that the createEntityManagerFactory is returning
> null,
> > which probably indicates that the Persistence processing couldn't find
> the
> > persistence.xml file. Is your persistence.xml file in your META-INF
> > directory and properly packaged with your JSP and other Entities?
> >
> > Kevin
> >
> > On Fri, Aug 1, 2008 at 6:35 AM, Anish <[EMAIL PROTECTED]> wrote:
> >
> >>
> >> Hey i have just been trying to get openjpa and jsp to work.... and i
> seem
> >> to
> >> get some errors.... iam using eclipse for this and running it on tomcat
> >> 6.0
> >>
> >> Greeeting.java
> >> package foo;
> >>
> >> import java.io.Serializable;
> >> import javax.persistence.Basic;
> >> import javax.persistence.Entity;
> >> import javax.persistence.GeneratedValue;
> >> import javax.persistence.Id;
> >>
> >> @Entity
> >> public class Greeting implements Serializable {
> >> @Id @GeneratedValue private int id;
> >> @Basic private String message;
> >> @Basic private String language;
> >>
> >> public Greeting() {}
> >> public Greeting(String message, String language) {
> >> this.message = message;
> >> this.language = language;
> >> }
> >>
> >> public String toString() {
> >> return "Greeting id=" + id + ", message=" + message + ",
> >> language=" +
> >> language;
> >> }
> >> }
> >>
> >>
> >> HelloWorld.java
> >> package foo;
> >> import javax.persistence.EntityManager;
> >> import javax.persistence.EntityManagerFactory;
> >> import javax.persistence.Persistence;
> >>
> >>
> >>
> >> persistence.xml
> >> <?xml version="1.0" encoding="UTF-8"?>
> >> <persistence version="1.0" 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">
> >> <persistence-unit name="projectjpa">
> >> <class>foo.Greeting</class>
> >> <properties>
> >> <!--
> >> We can configure the default OpenJPA properties here.
> They
> >> happen to be commented out here since the provided
> >> examples
> >> all specify the values via System properties.
> >> -->
> >>
> >>
> >> <property name="openjpa.ConnectionURL"
> >> value="jdbc:mysql://localhost:3306/greeting"/>
> >> <property name="openjpa.ConnectionDriverName"
> >> value="com.mysql.jdbc.Driver"/>
> >> <property name="openjpa.ConnectionUserName"
> >> value="root"/>
> >> <property name="openjpa.ConnectionPassword"
> >> value="admin"/>
> >> <property name="openjpa.jdbc.SynchronizeMappings"
> >> value="buildSchema(SchemaAction='dropDB,add')"/>
> >> </properties>
> >> </persistence-unit>
> >> </persistence>
> >>
> >> open.jsp
> >> <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
> >> pageEncoding="ISO-8859-1"%>
> >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> >> "http://www.w3.org/TR/html4/loose.dtd">
> >> <[EMAIL PROTECTED] import="javax.persistence.EntityManager"%>
> >> <[EMAIL PROTECTED] import="javax.persistence.EntityManagerFactory"%>
> >> <%@ page import="data.Greeting" %>
> >> <[EMAIL PROTECTED] import="javax.persistence.Persistence"%>
> >> <html>
> >> <head>
> >> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
> >> <title>Insert title here</title>
> >> </head>
> >> <body>
> >> <%
> >> final EntityManagerFactory emf;
> >> final EntityManager em;
> >> final String PERSISTENCE_UNIT_NAME = "projectjpa";
> >>
> >>
> >> emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
> >> em = emf.createEntityManager();
> >> ////////////////////////
> >>
> >> em.getTransaction().begin();
> >> Greeting g_en = new Greeting("hello world", "en");
> >> Greeting g_es = new Greeting("hola, mundo", "es");
> >> Greeting[] greetings = new Greeting[]{g_en, g_es};
> >> for(Greeting g : greetings) {
> >> em.persist(g);
> >> }
> >> em.getTransaction().commit();
> >> ///////////////////
> >>
> >> Greeting g = (Greeting) em.createQuery(
> >
> >> "select g from Greeting g where g.language = :language")
> >> .setParameter("language", "en").getSingleResult();
> >> out.println("Query returned: " + g);
> >> ////////////////////////////////////
> >> em.close();
> >> emf.close();
> >>
> >> %>
> >>
> >> </body>
> >> </html>
> >>
> >> Errors...
> >> org.apache.jasper.JasperException: An exception occurred processing JSP
> >> page
> >> /open.jsp at line 21
> >>
> >> 18:
> >> 19:
> >> 20: emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
> >> 21: em = emf.createEntityManager();
> >> 22: ////////////////////////
> >> 23:
> >> 24: em.getTransaction().begin();
> >>
> >>
> >> Stacktrace:
> >>
> >>
> >>
> org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
> >>
> >>
> >>
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
> >>
> >>
> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
> >> org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
> >> javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
> >>
> >>
> >>
> org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
> >>
> >> root cause
> >>
> >> java.lang.NullPointerException
> >> org.apache.jsp.open_jsp._jspService(open_jsp.java:76)
> >>
> org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
> >> javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
> >>
> >>
> >>
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
> >>
> >>
> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
> >> org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
> >> javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
> >>
> >>
> >>
> org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
> >>
> >> note The full stack trace of the root cause is available in the Apache
> >> Tomcat/6.0.16 logs.
> >> --
> >> View this message in context:
> >> http://n2.nabble.com/using-jsp-and-openjpa....-tp664117p664117.html
> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/using-jsp-and-openjpa....-tp664117p667541.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>