JPA

There are two ways to configure OrientDB JPA

Configuration

The first - do it through /META-INF/persistence.xml Folowing OrientDB properties are supported as for now:

javax.persistence.jdbc.url, javax.persistence.jdbc.user, javax.persistence.jdbc.password, com.orientdb.entityClasses

You can also use <class> tag

Example:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence version="2.0"
  3. xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  5. <persistence-unit name="appJpaUnit">
  6. <provider>com.orientechnologies.orient.object.jpa.OJPAPersistenceProvider</provider>
  7. <!-- JPA entities must be registered here -->
  8. <class>com.example.domain.MyPOJO</class>
  9. <properties>
  10. <property name="javax.persistence.jdbc.url" value="remote:localhost/test.odb" />
  11. <property name="javax.persistence.jdbc.user" value="admin" />
  12. <property name="javax.persistence.jdbc.password" value="admin" />
  13. <!-- Register whole package.
  14. See com.orientechnologies.orient.core.entity.OEntityManager.registerEntityClasses(String) for more details -->
  15. <property name="com.orientdb.entityClasses" value="com.example.domains" />
  16. </properties>
  17. </persistence-unit>
  18. </persistence>

Programmatic

The second one is programmatic:

Guice example

  1. com.google.inject.persist.jpa.JpaPersistModule.properties(Properties)
  1. /**
  2. * triggered as soon as a web application is deployed, and before any requests
  3. * begin to arrive
  4. */
  5. @WebListener
  6. public class GuiceServletConfig extends GuiceServletContextListener {
  7. @Override
  8. protected Injector getInjector() {
  9. return Guice.createInjector(
  10. new JpaPersistModule("appJpaUnit").properties(orientDBProp),
  11. new ConfigFactoryModule(),
  12. servletModule);
  13. }
  14. protected static final Properties orientDBProp = new Properties(){{
  15. setProperty("javax.persistence.jdbc.url", "remote:localhost/test.odb");
  16. setProperty("javax.persistence.jdbc.user", "admin");
  17. setProperty("javax.persistence.jdbc.password", "admin");
  18. setProperty("com.orientdb.entityClasses", "com.example.domains");
  19. }};
  20. protected static final ServletModule servletModule = new ServletModule() {
  21. @Override
  22. protected void configureServlets() {
  23. filter("/*").through(PersistFilter.class);
  24. // ...
  25. };
  26. }

Native example

  1. // OPEN THE DATABASE
  2. OObjectDatabaseTx db = new OObjectDatabaseTx ("remote:localhost/petshop").open("admin", "admin");
  3. // REGISTER THE CLASS ONLY ONCE AFTER THE DB IS OPEN/CREATED
  4. db.getEntityManager().registerEntityClasses("foo.domain");

DB properties, that were passed programmatically, will overwrite parsed from XML ones

Note

Config parser checks persistence.xml with validation schemes (XSD), so configuration file must be valid.

1.0, 2.0 and 2.1 XSD schemes are supported.