How to use ObjectDB properly in spring boot application?

#1

I have a spring boot application with objectdb embedded database.

I am manually handling connection and transaction operations as described at http://www.objectdb.com/java/jpa/persistence/overview

Below is a sample code that I am using: (taken from objecdb documentation):

EntityManagerFactory emf =
Persistence.createEntityManagerFactory("myDbFile.odb");
EntityManager em = emf.createEntityManager();
try {
  em.getTransaction().begin();
  // Operations that modify the database should come here.
  em.getTransaction().commit();
}
finally {
  if (em.getTransaction().isActive())
      em.getTransaction().rollback();
}

It works but the code has become uggly since I had to use try catch finally blocks in order to properly close connections.

I want to refactore my application so that database operations are done in JpaRepositories or Dao classes with @Transactional methods (as described in http://spring.io/guides/gs/accessing-data-jpa/)

I had a research on the web but could not find any solution that works. Existing spring-objectdb questions are usually related to regular spring or previous objectdb versions (before to 2.6.3). I could not make them work.

What I am looking for is a very simple spring boot sample application with:

  • spring-boot-starter-data-jpa
  • Objectdb (embedded)
  • Maven
  • Uses annotation based configuration (no xml file)
  • A dummy entity class (e.g: Customer(id,firstname) )
  • A JpaRepository class or dao class with list() and @Transactional persist(Customer) methods

 

Thanks in advance..

#2

Please check this stackoverflow thread.

If you cannot make it work please post your simple example with exact description of the issues that you have and we may be able to help.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Thank you for your quick reply.

Actually, I have already tried the suggestion in that thread, but could not make it work. Here is the error message when I run after mentioned modifications:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

 

To be sure that I am not doing anything wrong, here are steps I followed:

 

#4

Additional note: the suggestion in mentioned stackoverflow thread is based on objectdb 2.5.6. I can not simply use latest objectdb version since ObjectDB does not include JPA and JTA after 2.6.3 version.

Some other forum threads suggests excluding jdbc library that comes within spring-boot-starter-data-jpa. 

#5

No solution? :((

#6

Spring Data JPA expects a JDBC driver in the classpath.

Therefore to solve this error:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

you must keep H2 dependency from the original example (or another JAVA database):

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

This is strange, as H2 will not be used, but it is an easy workaround.

> Additional note: the suggestion in mentioned stackoverflow thread is based on objectdb 2.5.6. I can not simply use latest objectdb version since ObjectDB does not include JPA and JTA after 2.6.3 version.

This is now divided into 3 dependencies:

    <dependency>
        <groupId>com.objectdb</groupId>
        <artifactId>objectdb</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

The attached file contains the original example adjusted to work with up to date Spring JPA Data and ObjectDB 2.7.5.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#7

It works. Thank you very much :)))

 


Post Reply

To post a reply and/or subscribe to update notifications - please login