0

I have a method that I use to return a dynamic query. This method is shown below


public Query getLastId(String sProvider)
{
       String serviceProvider = sProvider.toLowerCase();     
       String query2 = "SELECT MAX(:serviceProvider.id) " + 
                   " FROM :sProvider :serviceProvider ";

       return em.createQuery(query2)
              .setParameter("sProvider", sProvider)
              .setParameter("serviceProvider", serviceProvider);

}

I want this method to return this

SELECT MAX(multichoice.id) FROM Multichoice multichoice

when I call the method like this

getLastId("Multichoice");

Please how do I write the query variable to return the answer?

1 Answer 1

1

To do this task you can use Criteria object model and projections to run your query over different types: Take a look at this article (15.7. Projections, aggregation and grouping)

here is the code :

List results = session.createCriteria(class)
.setProjection( Projections.max("id"))
.list();

Then instead of a string you should send a class to your method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.