JPA und Tomcat

2AndAHalfBit

Aktives Mitglied
Hallo,

erstmal entschuldige ich mich. Ich weiss nicht wirklich, ob der Thread hier richtig ist. Falls nicht, ist ein Moderator so nett und verschiebt ihn?

Ich habe folgendes Requirement:

- Webanwendung mit JSF
- Persistenzschicht mit JPA
- kein Applikationsserver

Daraus resultierend setze ich folgendes ein:

- Eclipse als IDE
- Eclipse dynmaic web project
- Tomcat 7 als Servletcontainer
- mySQL Datenbank
- myFaces von Apache
- OpenJPA von Apache

Ich kenne JPA nur von Applikationsservern. JPA benötigt bekanntlich eine persistence.xml die beim Erzeugen des EntityManagers ausgelesen wird. Meine Frage ist nun: wo muss ich diese hinstellen?

Ins META-INF?
Ins WEB-INF?

Ich habe sie einfach mal ins META-INF Gestellt - und es funktioniert nicht.

Meldung ist:

A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.

viewId=/index.xhtml
location=C:\Users\sebastian\workspace_web\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\xxx\index.xhtml
phaseId=INVOKE_APPLICATION(5)

Caused by:
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:76)

dazu folgender Output auf der Konsole:
*******************************************************************
*** WARNING: Apache MyFaces-2 is running in DEVELOPMENT mode. ***
*** ^^^^^^^^^^^ ***
*** Do NOT deploy to your live server(s) without changing this. ***
*** See Application#getProjectStage() for more information. ***
*******************************************************************

Jan 02, 2012 12:39:58 PM org.apache.coyote.AbstractProtocol start
Information: Starting ProtocolHandler ["http-bio-8080"]
Jan 02, 2012 12:39:58 PM org.apache.coyote.AbstractProtocol start
Information: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 02, 2012 12:39:58 PM org.apache.catalina.startup.Catalina start
Information: Server startup in 8264 ms
Jan 02, 2012 12:40:02 PM org.apache.catalina.core.ApplicationContext log
Information: No state saving method defined, assuming default server state saving
Jan 02, 2012 12:40:03 PM org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider newInstance
Information: Creating instance of JDOSessionBean
12 WARN [http-bio-8080-exec-4] openjpa.Runtime - The configuration property named "openjpa.Id" was not recognized and will be ignored, although the name closely matches a valid property called "openjpa.Id".
32 INFO [http-bio-8080-exec-4] openjpa.Runtime - Starting OpenJPA 2.1.1
Jan 02, 2012 12:50:51 PM org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider newInstance
Information: Creating instance of JDOSessionBean
2 WARN [http-bio-8080-exec-5] openjpa.Runtime - The configuration property named "openjpa.Id" was not recognized and will be ignored, although the name closely matches a valid property called "openjpa.Id".
2 INFO [http-bio-8080-exec-5] openjpa.Runtime - Starting OpenJPA 2.1.1

anbei noch di persistence.xml

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<persistence-unit name="JDOIT" transaction-type="RESOURCE_LOCAL">
<class>Task</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost/JDOIT-DB" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="xxx" />
<property name="openjpa.ConnectionPassword" value="xxxxx" />
<property name="openjpa.Log" value="SQL=TRACE" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
</properties>
</persistence-unit>
</persistence>
[/xml]

  • Wird die XML-Datei gezogen, oder resultiert der Fehler daraus das er sie nicht findet? Wie kann ich dies evtl. überprüfen?
  • Wie oben bereits gefragt: Wo muss die XML-Datei stehen


Vielen Dank und Grüße,
Sebastian.
 
Kann ich mir auch nie merken. Darum schaue ich immer hier nach:
Java How To ...: Where to put persistence.xml in web app?

Ich vergas: Das hatte ich schon gefunden.

In all cases, persistence.xml always resides in {root-of-persistence-unit}/META-INF/ directory. For example,

foo.war:
WEB-INF/classes/META-INF/persistence.xml //good
WEB-INF/classes/com/foo123/jpa/Project.class
WEB-INF/web.xml
index.jsp

In Eclipse gibt es kein WEB-INF/classes, es gibt aber WEB-INF/lib
Heisst das die persistence.xml muss stehen unter:

Projekt/WebContent/WEB-INF/lib/META-INF?
Wenn ja, dann funktioniert das (bei mir) nicht....
Bzw. dann ist etwas anderes falsch???

Grüße.
 
Der Link gibt die möglichen Speicherorte bei laufender Webanwendung an. WEB-INF/classes ist ein Ordner, in dem Klassendateien liegen, die nicht in jars verpackt sind. Der "Partner"-ordner WEB-INF/lib nimmt die jars auf. Diese Ordner gibt es je nach Entwicklungsumgebung/Plugin/Buildsystem in der Entwicklersicht oder eben nicht (bei Dir offensichtlich nicht). Ich selbst sorge mit Maven dafür, dass die Datei im richtigen Ordner des jars landet, das meine Persistenzklassen enthält. Du müsstes halt bei DEINEM Buildsystem schauen, wie es geht. Mit Eclipse Dynamic Webproject kenne ich mich leider nicht aus. Zum Ausprobieren kannst Du ja die persistence.xml händisch auf den Webcontainer kopieren und den Context neu laden.
 
Ja, okay.
Andere Frage: sieht die xml Datei denn korrekt aus?
Ich weiss ja nicht, ob sie geladen wird...



solved!!!

@ Für die Nachwelt:
Für die Persistence muss im "src-Ordner" ein neuer Ordner "META-INF" angelegt werden.
Grund ist, dass die Klassen in WEB-INF/classes exportiert werden, wenn man ein war file erstellt.

Für einen Screenshot siehe man in den Anhang...


Ich bekomme jetzt nur noch einen anderen Fehler:

There were errors initializing your configuration: <openjpa-2.1.1-r422266:1148538 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "com.mysql.jdbc.Driver" and URL "jdbc:mysql://localhost/JDOIT-DB". You may have specified an invalid URL. at org.apache.openjpa.jdbc.schema.DataSourceFactory.newConnectException(DataSourceFactory.java:261)

Da ist wohl etwas mit der Config falsch.
Kann da wer helfen?!

Grüße
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben