Skip to main content

step by step directions Persistence API J2SE

5 replies [Last post]
lucasjordan
Offline
Joined: 2003-06-18

I have tried a bunch of blog/tutorials trying to get the new Persistence API working from j2se. I have been unable find a basic working example.

What I want to know is:
1) what jars do i need?
2) what does a persistence.xml file really look like?
3) where exactly must a persistence.xml file go?
4) how can I automatically generate the tables for my POJOs to be stored in?
5) Is there official/current documentation?
6) can I use Derby running as a separate server as a store? if so, how?

I would really appreciate any one helping me, I'm really tired of looking at the same outdated examples :)

-Lucas

P.S. what is wrong when I get a stack trace that looks like:
Exception in thread "main" java.lang.NullPointerException
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.callPredeploy(JavaSECMPInitializer.java:120)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initPersistenceUnits(JavaSECMPInitializer.java:279)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initialize(JavaSECMPInitializer.java:298)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initializeFromMain(JavaSECMPInitializer.java:335)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.getJavaSECMPInitializer(JavaSECMPInitializer.java:78)
at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.getServerSession(EntityManagerFactoryProvider.java:235)
at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:94)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:50)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
frdbr
Offline
Joined: 2004-08-03

Hi Lucas,

I had the same problem.

Turned out to be a problem with 'spaces' in the filename.

I had my project running under C:\Document and Settings\\...

If I moved it under a directory without spaces, it worked fine.

mperezma
Offline
Joined: 2005-03-22

Hi Lucas,

I got this persistence example working in NetBeans 5.0 RC2 J2SE project using MySQL as persistent storage. Some comments to make it work this way:

Add toplink-essentials-agent.jar in your NB5 project library. You will find this jar in the glassfish\lib folder. You don't need glassfish up and running, you only need some jars included in this lib folder. So download glassfish and simply extract it somewhere in yor filesystem.

Specify the agent to be loaded: open your project properties, go to Run node in the tree and include this param in the VM Options field:

-javaagent:c:\glassfish\lib\toplink-essentials-agent.jar

Replace the path for glassfish\lib folder in your filesystem. If you don't specify this agent to be loaded, you will get the NullPointerException with stacktrace you pointed before.

persistence.xml file must be placed in a folder named "META-INF". You can create this folder as a package in your source tree with this name and place descriptor in it. This is the persistence.xml that worked for me:

oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider entity.Customer
entity.Order

value="oracle.toplink.essentials.platform.database.MySQL4Platform"/>

Note the property "ddl-generation" wich make tables to be created by the provider at runtime. Replace the connection string (db url), username and password with your own.

Remember to include the jdbc driver for your db in the project library. In this case I used mysql-connector-java-3.1.10-bin.jar for MySQL 4.1 server. Keep de db server running and execute the example. It should work!!!

mariO

mvatkina
Offline
Joined: 2005-04-04

Please check persistence example page: https://glassfish.dev.java.net/javaee5/persistence/persistence-example.html

Automatic table generation steps are described here: http://blogs.sun.com/roller/page/java2dbInGlassFish

Regards,
-marina

lucasjordan
Offline
Joined: 2003-06-18

the example at:
https://glassfish.dev.java.net/javaee5/persistence/persistence-example.html

does not have a working download, do any one else know of another working example?

-Lucas

mvatkina
Offline
Joined: 2005-04-04

It works for me. Can you explain in more details what is
not working for you?

Regards,
-marina