Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Java Persistence API and persistence.xml

Thread view: 
Kenneth P. Turvey - 15 Mar 2008 23:43 GMT
I'm trying to work through the book "Pro EJB 3 - Java Persistence API"
(don't buy it), but I'm having some difficulty getting things to work
here.  I'm just trying to use the derby embedded database and the
glassfish implementation of the JPA, as I think is being done in the book.
Unfortunately, I'm getting a stack trace when I try to run the code.  

I have no idea what I'm doing with all this, so if you have any pointers,
I would greatly appreciate it.  I don't know how this book is going to
help if I can't get the first example running.  

Here's my persistence.xml file:

---
<?xml version="1.0" encoding="UTF-8"?>

<!--
   Document   : persistence.xml.xml
   Created on : March 15, 2008, 4:27 PM
   Author     : kt
   Description:
       Purpose of the document follows.
-->

<persistence>
   <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">
       <class>examples.model.Employee</class>
       <properties>
           <property name="toplink.jdbc.driver"
                     value="org.apache.derby.jdbc.EmbeddedDriver"/>
           <property name="toplink.jdbc.url"
                     value="jdbc:derby:MyTestDb;create=true"/>
           <property name="toplink.jdbc.user" value="APP" />
           <property name="toplink.jdbc.password" value="APP" />
       </properties>
   </persistence-unit>
</persistence>
----

Here's the one Entity

----
package javapersistence;

import javax.persistence.*;

@Entity
public class Employee {
   @Id private int id;
   private String name;
   private long salary;
   
   public Employee() {}
   public Employee(int id) { this.id = id; }
   
   public int getId() { return id; }
   public void setId(int id) { this.id = id; }
   public String getName() { return name; }
   public void setName(String name) { this.name = name; }
   public long getSalary() { return salary; }
   public void setSalary(long salary) { this.salary = salary; }
   
}
-----

And this is the stack trace I'm getting:

----
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named EmployeeService: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory:
oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Local Exception Stack:
Exception [TOPLINK-30005] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@17182c1
Internal Exception: Exception [TOPLINK-30004] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: file:/home/kt/git/Learning/JavaPersistence/build/classes/
Internal Exception:
(1. cvc-elt.1: Cannot find the declaration of element 'persistence'.)
       at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:143)
       at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
       at javapersistence.EmployeeTest.main(EmployeeTest.java:18)
Caused by: Exception [TOPLINK-30004] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: file:/home/kt/git/Learning/JavaPersistence/build/classes/
Internal Exception:
(1. cvc-elt.1: Cannot find the declaration of element 'persistence'.)
       at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionProcessingPersistenceXML(PersistenceUnitLoadingException.java:134)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:161)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.processPersistenceArchive(PersistenceUnitProcessor.java:99)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.getPersistenceUnits(PersistenceUnitProcessor.java:89)
       at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initPersistenceUnits(JavaSECMPInitializer.java:236)
       at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initialize(JavaSECMPInitializer.java:255)
       at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:155)
       ... 3 more
Caused by:
(1. cvc-elt.1: Cannot find the declaration of element 'persistence'.)
       at oracle.toplink.essentials.internal.ejb.cmp3.xml.parser.XMLExceptionHandler.error(XMLExceptionHandler.java:52)
       at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
       at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
       at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
       at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1887)
       at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)
       at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
       at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3088)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:914)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
       at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:151)
       ... 8 more

Provider named oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider threw unexpected exception at create EntityManagerFactory:
oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Local Exception Stack:
Exception [TOPLINK-30005] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@17182c1
Internal Exception: Exception [TOPLINK-30004] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: file:/home/kt/git/Learning/JavaPersistence/build/classes/
Internal Exception:
(1. cvc-elt.1: Cannot find the declaration of element 'persistence'.)
       at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:143)
       at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
       at javapersistence.EmployeeTest.main(EmployeeTest.java:18)
Caused by: Exception [TOPLINK-30004] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: file:/home/kt/git/Learning/JavaPersistence/build/classes/
Internal Exception:
(1. cvc-elt.1: Cannot find the declaration of element 'persistence'.)
       at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionProcessingPersistenceXML(PersistenceUnitLoadingException.java:134)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:161)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.processPersistenceArchive(PersistenceUnitProcessor.java:99)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.getPersistenceUnits(PersistenceUnitProcessor.java:89)
       at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initPersistenceUnits(JavaSECMPInitializer.java:236)
       at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initialize(JavaSECMPInitializer.java:255)
       at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:155)
       ... 3 more
Caused by:
(1. cvc-elt.1: Cannot find the declaration of element 'persistence'.)
       at oracle.toplink.essentials.internal.ejb.cmp3.xml.parser.XMLExceptionHandler.error(XMLExceptionHandler.java:52)
       at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
       at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
       at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
       at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1887)
       at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)
       at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
       at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3088)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:914)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
       at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:151)
       ... 8 more

       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
       at javapersistence.EmployeeTest.main(EmployeeTest.java:18)
----

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>

CK - 16 Mar 2008 00:53 GMT
Words to the wise, "Kenneth P. Turvey" <kt-usenet@squeakydolphin.com>
wrote:

>I'm trying to work through the book "Pro EJB 3 - Java Persistence API"
>(don't buy it), but I'm having some difficulty getting things to work
>here.  I'm just trying to use the derby embedded database and the
>glassfish implementation of the JPA, as I think is being done in the book.

>Unfortunately, I'm getting a stack trace when I try to run the code.  
>
[quoted text clipped - 9 lines]
><!--
>    Document   : persistence.xml.xml

persistence.xml.xml? Is the name of the file really that?

>    Created on : March 15, 2008, 4:27 PM
>    Author     : kt
[quoted text clipped - 3 lines]
>
><persistence>

I think you need to add something there:
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"

At least that is how I read the stacktrace. What I mentioned below are
is plain guess work.

>    <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">
>        <class>examples.model.Employee</class>
[quoted text clipped - 3 lines]
>            <property name="toplink.jdbc.url"
>                      value="jdbc:derby:MyTestDb;create=true"/>

Did you specify myTestDb as Datasource in glassfish?

>            <property name="toplink.jdbc.user" value="APP" />
>            <property name="toplink.jdbc.password" value="APP" />
>        </properties>
>    </persistence-unit>
></persistence>

Also, there is no datasource here, although I do not know whether you
actually need one when using the embedded derby db.

Oh, and does the database scheme exist already? I think you need to
create the scheme the tables and so on are created for you, but I
cannot say for sure.

Hope this helps, please report back if it does and also if it does
not.
--
Claus Dragon <clauskick@mpsahotmail.com>
=(UDIC)=
d++ e++ T--
K1!2!3!456!7!S a29
"Coffee is a mocker. So, I am going to mock."

- Me, lately.
Kenneth P. Turvey - 16 Mar 2008 01:08 GMT
> Also, there is no datasource here, although I do not know whether you
> actually need one when using the embedded derby db.
[quoted text clipped - 5 lines]
> Hope this helps, please report back if it does and also if it does
> not.

I got it working.  I actually tried to cancel this post, but it was
already out the door.  

Anyway, your post was helpful.  I got the system to create the schema for
me.  The file is actually quite close to what was necessary.  The problems
were mainly in the XML file above.  I'll post the result here so that it
goes to the search engines.  

This gets things working with Glassfish and Derby.  

Thanks!

-----
 <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">
       <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
       <class>javapersistence.Employee</class>
       <properties>
           <property name="toplink.jdbc.driver"
                     value="org.apache.derby.jdbc.EmbeddedDriver"/>
           <property name="toplink.jdbc.url"
                     value="jdbc:derby:MyTestDb;create=true"/>
           <property name="toplink.jdbc.user" value="APP" />
           <property name="toplink.jdbc.password" value="APP" />
         
           <!-- Java2DB properties, creates the tables -->
           <property name="toplink.ddl-generation" value="create-tables" />
       </properties>
   </persistence-unit>
</persistence>
-----

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.