Retrieve data of all entity classes in a single query

#1

I have in an objectdb file 10 Entity Classes with some data in each (Athlete, Trainer, Stadium etc)

This is the code I use to retrieve all Managed Types and data for the Athlete class

Metamodel metamodel = em.getMetamodel();
Set<ManagedType<?>> allManagedTypes = metamodel.getManagedTypes();
     
for(ManagedType o : allManagedTypes)
{
   String strTmpClass = o.toString().replace("Type ", ""); // Getting the object name for the SELECT

   String tmpQuery = "SELECT s FROM " + strTmpClass + " s"; // This works for all classes
   TypedQuery<Athlete> query = em.createQuery(tmpQuery, Athlete.class); // I want to avoid this
   List<Athlete> results = query.getResultList();
   for(Athlete result : results)
   {
         System.out.println(result.Code + ", " + result.Name + ", " + result.Surname + ", " + result.CountryOfOrigin + ", " +  result.Genre);
   }
}

Is there a way to get data of all classes without writing down 10 different queries (i.e. using the for loop)?

I tried to use TypedQuery<Object> instead of TypedQuery<Athlete> but had casting issues
 

#2

You can retrieve all the entity objects using a query on Object:

    SELECT o FROM Object o

Of course, since the result list will include objects of different types, before using casting you will have to check object types using instanceof. Alternatively, you may avoid casting and retrieve fields in a generic way using Java reflection.

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

Thanks for your answers

So in case I don't have the java classes declarations and I only have an .odb file, how can I parse the data?

#4

Using Java Reflection API. The JPA Metamodel API may also help.

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

Post Reply

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