views:

65

answers:

3

I've used eclipselink in a web project in netbeans. Works nice and easy. How can I do the same in a project unrelated to web(console app)?

In my web app I have:

@PersistenceUnit
EntityManagerFactory enf;

Which instantiates enf. This does not work in A console app.

+1  A: 

Ok, I just need to call

enf = Persistence.createEntityManagerFactory("JavaApplication39PU");

Where JavaApplication39PU is the name found in META-INF/persistence.xml

Esben Skov Pedersen
+1  A: 

In case of an Web or an Application container, the container injects the EntityManagerFactory when the @PersistenceUnit annotation is called. This will not hold good for a console app. The possible ways for achieving this is

  1. To include Spring (which does the same job as your web container).Please refer the link below for the tutorial.

http://blog.interface21.com/main/2006/05/30/getting-started-with-jpa-in-spring-20

Or 2. To write the initiation code yourself as follows

    EntityManagerFactory emf = Persistence.
createEntityManagerFactory("PersistenceUnitName");
Manoj
+1  A: 

When running within an EJB3 container (or Spring) you get the benefits of dynamic weaving with EclipseLink. If you are running in a JavaSE scenario I would recommend using the -javaagent to enable dynamic weaving so your application behaves the same.

Docs: http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#How_to_Configure_Dynamic_Weaving_for_JPA_Entities_Using_the_EclipseLink_Agent

Doug

Doug Clarke