Tagged: persistence.xml

Overriding JTA into RESOUCE-LOCAL while Unit Testing


references

JTA

Let’s say you have a very simple persistence.xml looks like this.

<persistence-unit name="somePU" transaction-type="JTA">
  <jta-data-source>jdbc/someDS</jta-data-source>
  <exclude-unlisted-classes>false</exclude-unlisted-classes>
  <properties>
    <property name="javax.persistence.validation.mode" value="CALLBACK" />
  </properties>
</persistence-unit>

RESOUCE-LOCAL

You can, maybe you should, override it for unit testing. Following codes uses EclipseLink dependent API.

final Map<String, String> properties = new HashMap<>();
properties.put(PersistenceUnitProperties.TRANSACTION_TYPE,
               PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
properties.put(PersistenceUnitProperties.JDBC_DRIVER,
               "org.apache.derby.jdbc.EmbeddedDriver");
properties.put(PersistenceUnitProperties.JDBC_URL,
               "jdbc:derby:memory:testDB;create=true");
properties.put(PersistenceUnitProperties.DDL_GENERATION,
               PersistenceUnitProperties.CREATE_ONLY);
properties.put(PersistenceUnitProperties.LOGGING_LEVEL,
               Level.FINE.getName());
properties.put(PersistenceUnitProperties.TARGET_SERVER,
               TargetServer.None);
ENTITY_MANAGER_FACTORY
    = createEntityManagerFactory("somePU", properties);