Skip to content

Standalone Application Setup

sclassen edited this page Jan 4, 2013 · 2 revisions

java code

public static void main(String[] args) {
    final PersistenceModule persistenceModule = new PersistenceModule();
    persistenceModule.addApplicationManagedPersistenceUnit("puName").useResourceLocalTransaction();
    final Injector injector = Guice.createInjector(persistenceModule, getApplicationSpecificModules());
    final PersistenceService persistenceService = injector.getInstance(injector.getInstance(PersistenceService.class).class)
    
    try {
        persistenceService.start();
    
        while(notTerminated()) {
            // run application
        }
    }
    finally {
        persistenceService.stop();
    }
}

persistence.xml

<persistence 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_2_0.xsd" version="2.0">
    <persistence-unit name="puName" transaction-type="RESOURCE_LOCAL">
        <!-- your configuration -->
    </peristence-unit>
</peristence>

Structure of the .jar

+ app.jar
|    + META-INF
|    |    + MANIFEST.MF
|    |    + persistence.xml
|    + foo
|    |    + bar
|    |    |    + Main.class
|    |    |    + App.class