Just create new XML file, called persistence.xml
in your META-INF
folder. The contents of this file should be something like this (please refer to JPA documentation in case of specific questions):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
<class>DummyEntity</class>
<properties>
<property name="javax.persistence.jdbc.url" value="database-url" />
<property name="javax.persistence.jdbc.user" value="database-user" />
<property name="javax.persistence.jdbc.password" value="database-password" />
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
</properties>
</persistence-unit>
</persistence>
Afterwards you can create an EntityManagerFactory
, using following sample code:
import javax.persistence.EntityManagerFactory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.jpa.osgi.PersistenceProvider;
Map<String, Object> connectionProperties = new HashMap<String, Object>();
connectionProperties.put(PersistenceUnitProperties.CLASSLOADER, this
.getClass().getClassLoader());
try {
EntityManagerFactory emf = new PersistenceProvider()
.createEntityManagerFactory("pu", connectionProperties);
} catch (Exception e) {
// todo
}