1

In many document about JPA I have read tell me that I need a Persistence file to work with JPA. That file is automatically created when I make a JPA project but now I want to use JPA in RAP project and also RCP project too. But I don't know how to make it.

Need help.

gamo
  • 1,549
  • 7
  • 24
  • 36

2 Answers2

1

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
}
Alex K.
  • 3,294
  • 4
  • 29
  • 41
1

I found out a way is use Project Facets.

Right click your project -> Properties -> Project Facets -> Check JPA box.

That will automatically create persistence.xml file in your META-INF folder. Then you just need to make a connection to work with JPA.

gamo
  • 1,549
  • 7
  • 24
  • 36