-1

I implemented a method that allows me to generate a test ( entire of question ) automatically but the problem is : the method will obviously take the number of random questions but also the category of questions generated (I have a entié category and therefore a table too) I don't know where i will put the category in query. and secondly RANDOM() is not taked by JPQL what can i do ?

schema of the DataBase

public List<Question> prepareRandomTest(int number_of_questions, Categorie      categorie){ 
      String jpql = "SELECT q FROM Question q ORDER BY RANDOM() LIMIT "+number_of_questions  ;
     Query query = entityManager.createQuery(jpql);
      return query.getResultList();
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Daly
  • 19
  • 8

1 Answers1

1

You are trying to use Java Persistence Query Language here, Hence your solution is not taking RANDOM into account. Use Native query and build Query from native sql string, Native query is just a plain sql statement without Entity object reference (like Question). This way a normal sql keywords like RANDOM etc are easily read.

Native Query Tutorial

Instead of Using

String jpql = "SELECT q FROM Question q ORDER BY RANDOM() LIMIT "+number_of_questions  ;

use:

"SELECT * FROM question where category="+category+" ORDER BY RANDOM() LIMIT "+number_of_questions;

Additional Advice: Get the category string from an "Enum" in your code to match the string value in the database category column.

Maneshwar Singh
  • 426
  • 7
  • 12
  • in my program I use JPQL so it does not cause problem if I use simple SQL queries ? – Daly Apr 25 '16 at 12:16
  • Whether you are using JPQL or Sql Query String, hibernate will negotiate both on its own to create Query Object like this (Query query = entityManager.createQuery(jpql or sql);), so you dont have to worry about native query breaking your code, this is a mandatory abstraction hibernate provides. – Maneshwar Singh Apr 25 '16 at 12:19
  • is that correct? public List prepareRandomTest(int number_of_questions, Categorie categorie){ String sql = "SELECT * FROM question where category="+categorie+" ORDER BY RANDOM() LIMIT "+number_of_questions; SQLQuery query = session.createSQLQuery(sql); List results = (List)query.list(); return result; } – Daly Apr 25 '16 at 12:23
  • "return results" instead of returning result. – Maneshwar Singh Apr 25 '16 at 12:26
  • SQLQuery query = session.createSQLQuery(sql); sqlQuery can not be resolved as a type and session also can you help me – Daly Apr 25 '16 at 12:29
  • can you paste the whole stacktrace, what is sqlQuery object? – Maneshwar Singh Apr 25 '16 at 12:33
  • public List prepareRandomTest(int number_of_questions, Categorie categorie){ String sql = "SELECT * FROM question where category="+categorie+" ORDER BY RANDOM() LIMIT "+number_of_questions; SQLQuery query = session.createSQLQuery(sql); =>sqlquery and session can not be resolved as a type List results = (List)query.list(); return results; } – Daly Apr 25 '16 at 12:35
  • Oh yeah you dont have Session Object defined as you are using EntityManager, you need to pull out Session from EntityManager...Session session = entityManager.unwrap(Session.class); – Maneshwar Singh Apr 25 '16 at 12:37
  • If you dont want to use Session, and wish to use EntityManage itself, than use, this solution, http://stackoverflow.com/questions/2110809/use-of-entitymanager-createnativequeryquery-foo-class – Maneshwar Singh Apr 25 '16 at 12:38
  • the last solution is not compatible with my exemple sorry – Daly Apr 25 '16 at 12:58