Empty query results after JAR creation

#1

Hi,

I struggle with a strange issue. I've embedded ObjectDB in a small application

to query cases. Within Netbeans everything works fine but when I create

a  JAR File and executed via "Java -Jar xyz.jar", I get either empty results or on

some cases a "NoResultException" which appears to report the same issue.

I've created a small test case (attached zip file) with a sample DB to reproduce the issue. 

In essence, I've just annotated my beans, created a file as storage and

imported some data. The main class of the test case looks like this:

    EntityManagerFactory emf =
        Persistence.createEntityManagerFactory(
            "src/main/resources/TravelDB.odb");
    EntityManager em = emf.createEntityManager();
    String s = "SELECT c FROM Case c";

    TypedQuery<Case> q = em.createQuery(s, Case.class);

    List<Case> allCases = q.getResultList();

    for (Case c1 : allCases) {
        System.out.println(c1.getCaseId());

In Netbeans, the code is working but once the Jar File is created and moved to a different folder,

the query does not return any results any more.

What do I wrong?

Do I need any additional configuration to deploy ObjectDB correctly to a Jar file that can be distributed?

Thanks for any help.

 

#2

Empty results or NoResultException may be caused by accessing an empty database.

In your code you use a relative path to access the database:

    EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("src/main/resources/TravelDB.odb");

When you run the jar the current directory is probably different so maybe you are trying to access the database in a wrong location? In that case, a new empty database will be created automatically in that path.

ObjectDB Support
#3

Well,

thanks for your advice. I have changed the location to:

emf = Persistence.createEntityManagerFactory("main/resources/TravelDB.odb");

and imported my data again. The result was initially exactly the same. I've observed, when I run the Jar for

the first time, it creates the folder structure "main/resources/" containting the DB file.

However, when I copy my original DB over the one created by the JAR file my application is working.

The Size of both DB files is exactly the same, so I have no idea what the difference is or

why I need to copy the DB into the dist folder.

 

Is this the way it should be?

 

I mean, it's nothing wrong with copying the DB file manually, it was just not mentioned in the manual

so I was assuming that the DB would be copyied correctly during JAR creation.

Thanks anyway, it seems to work now.

 

#4

> when I run the Jar for the first time, it creates the folder structure "main/resources/" containting the DB file.

That file is probably empty you can check it in the Explorer.

> The Size of both DB files is exactly the same

There is a default initial database size so you may have the same size for an empty database and a database with some content.

> Is this the way it should be?

Probably this can be done automatically. Check that the initial database in the resources is not empty.

ObjectDB Support

Reply