Missing Data on Retrieval (0, null values)

#1

I try  to export all Objects in an Flatfile-Hiearchy (.csv).
But only the first exported Table contains all necessary Data.
Most of the others  contains all  Object-Rows', but most Values of the Columns are missing.

With a  em.clear()
inside the Loop, all tables are exported correctly but the Enity-Keys do not match.

I can't get the point - what am i doing wrong?

 

emf = Persistence.createEntityManagerFactory(dbFile);
em = emf.createEntityManager();

for(String tableName: tables)
{
  List<Object> resultList = em.createQuery("SELECT q  FROM "+tableName+" "+n).getResultList();
  exportToCSV(resultList);
}

 

Best regards,
Harald

#2

More information is needed (e.g. about your method exportToCSV).

ObjectDB Support
#3

The Data is even missing in the  resultlist without any Export-Function.

A little Example:

First try:
List<Object> resultList1 = em.createQuery("SELECT p  FROM Person p").getResultList();    
-> resultlist1   OK
List<Object> resultList2 = em.createQuery("SELECT d  FROM Document d" ).getResultList();
-> resultlist2  Bad - many null and 0 Values.

Secound try:
List<Object> resultList1 = em.createQuery("SELECT d  FROM Document d" ).getResultList();   
-> resultList1 (Document)  OK
List<Object> resultList2 = em.createQuery("SELECT p  FROM Person p" ).getResultList();    
-> resultlist2 (Person) Bad

Third try:
List<Object> resultList1 = em.createQuery("SELECT d  FROM Document d" ).getResultList();    (OK)
em = emf.createEntityManager();
List<Object> resultList2 = em.createQuery("SELECT p  FROM Person p" ).getResultList();  
>  OK, but persistence.Person@... -Key do not match between the two Resultlists

Tested with  Version 2.6.7 and 2.4.5  - same behaviour.  

 

 

#4

The null and 0 values may be the result of lazy loading.

If you see them in the debugger before accessing the result object then this is normal.

If you access objects by reflection (and not by invoking property methods or accessing the fields directly) then you must make sure that data is fetched from the database before you your first reflection operation. This could be done, for example, by accessing a persistent field directly (with no reflection) from an enhanced class (e.g. by invoking a property method in the entity class if the class is enhanced).

ObjectDB Support
#5

Many thanks - that's the point.

That are my first steps in Java - so all is a little bit confusing ;-)
Do I have to create a class for each Object, or is there a generic Function/Class i can use?
There are about 100 Entitys - I was happy not to create a Class for each one.
Therfore i use reflection.

Or is there an easier way to do the whole operation?  
The result should be the same as the tables in (ObjectDB) Explorer.

 

 

 

 

 

 

 

#6

The usual way of using ObjectDB (and JPA) is to define entity classes as ordinary classes.

Large projects may need 100 or more entity classes, but you may be able to reduce the number of entity classes in your specific project (if it is not very large) by changing your design. For example, using one entity class with various parameters (including map of attributes) to represent similar objects, when applicable.

Having ordinary Java classes to represent your persistent data (including support of polymorphism, type checking, etc.) is one of the main benefits of using JPA and by generating classes with reflection you lose that benefit.

ObjectDB Support
#7

That's clear - and  I have no doubt it does a great job.
But is there a way to  generate entities directly from the database tables?

#8

There are no database tables in ObjectDB.

ObjectDB generate classes automatically when they are missing, so if you retrieve an object from a database and its class is not available you will still get an object that you can explore using reflection.

The main idea of an object database and of JPA is using standard (POJO) classes to persist data.

ObjectDB Support
#9

That is the Point:
"you will still get an object that you can explore using reflection."
That i thought,  but the  object values are not consistend. I've no idea how to go on.

I want to export the data from an third party App to make them useable (viewable) on my Android Smartphone.
So i don't have to take my Laptop with me every time.

#10

You can use the following code to activate an entity object (obj) before accessing its fields with reflection:

if (!Persistence.getPersistenceUtil().isLoaded(m_object))
    em.unwrap(PersistenceManager.class).retrieve(obj);

Note that ObjectDB is not supported on Android.

ObjectDB Support
#11

Now it works perfect.  Big Thanks!
I am very impressed from your great help for such an (un)devoloper  like me!

Best regards,
Harald

 

 

 

 

#12

You are welcome. Your question is important and this thread may also help other users in the future.

ObjectDB Support

Reply