673 words

Missing Data on Retrieval (0, null values)

#1
2016-04-13 07:07

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

harald77
harald77's picture
Joined on 2016-04-13
User Post #1
#2
2016-04-13 12:59

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

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,483
#3
2016-04-13 13:23

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.  

 

 

harald77
harald77's picture
Joined on 2016-04-13
User Post #2
#4
2016-04-13 14:02

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,484
#5
2016-04-13 14:34

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.

 

 

 

 

 

 

 

harald77
harald77's picture
Joined on 2016-04-13
User Post #3
#6
2016-04-13 15:01

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,485
#7
2016-04-14 06:54

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?

harald77
harald77's picture
Joined on 2016-04-13
User Post #4
#8
2016-04-14 12:52

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,487
#9
2016-04-18 06:26

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.

harald77
harald77's picture
Joined on 2016-04-13
User Post #5
#10
2016-04-18 11:38

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,493
#11
2016-04-19 06:34

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

Best regards,
Harald

 

 

 

 

harald77
harald77's picture
Joined on 2016-04-13
User Post #6
#12
2016-04-19 09:23

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

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,495

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel