Create a JPQL and define the entity at runtime

Asked

Viewed 104 times

0

Good night.

I wonder if there is a way to make a JPQL query and define the entity at runtime, without having to concatenate or interpolate String.

Example using interpolation:

public long getRecords(String entity){
    String q = String.format("COUNT(e) FROM %s e", entity);
    Query query = em.createQuery(q);
    return (long) query.getSingleResult();
}
  • You can give a better example? String has not the method getSingleResult(). In addition, I would like to know in what context this method is used and where you get the parameter passed to the method, because this is important to be able to elaborate a response. In particular I wonder if you have how to know which is the object Class<?> of its entity.

  • I added the rest of the code. This method will be called during a user request. It will fill in the fields that will be generated in the database. I have no way of knowing Class<? > why the entity will be generated from a file. properties.

  • @helciodasilva has classes involved, that is, in addition to this Count in the table, there is the class that returns the content of a row or rows of a table?

  • Virgil, there isn’t. I just need to check if a table is empty.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.