-1

I have this code but when i;m running it I'm getting an error, I've also used native Query to do it but i vain

EntityManager entityManager =  Persistence.createEntityManagerFactory("AdvancedJavaProjectPU").createEntityManager();
entityManager.getTransaction().begin();

    Query query = entityManager.createQuery("Select e FROM Question e ORDER BY RAND()").setMaxResults(20);
List<Question> question =query.getResultList();
for(Question q:question){
   System.out.println(q.getName()); 
} 

I get this error:-

Exception Description: Syntax error parsing the query [Select e FROM Question e ORDER BY RAND()], line 1, column 38: unexpected token [(].

user2327579
  • 449
  • 9
  • 24

1 Answers1

0

If you use a native query, you should write a query as you were running it on a SQL editor ...

By the way, why didn't you continue with the stackoverflow topic you opened some minutes ago????

Community
  • 1
  • 1
Paolo
  • 1,641
  • 11
  • 15
  • yes but then i want it to be able to use getResultlist() and when i use this i can another error, primary key cannot be null, I've checked in the database there is not null primary key, I've also read some other comments on this error but it did not solve my problem.Thanks for the help btw. – user2327579 Nov 07 '13 at 10:15
  • 1
    If you want to use native query and then use the results as an object, you need to define a `SqlResultSetMapping` in order to map the sql result into your object. See [here](http://stackoverflow.com/questions/13012584/jpa-how-to-convert-a-native-query-result-set-to-pojo-class-collection) – Paolo Nov 07 '13 at 10:22
  • If this was the right solution, please accept the answer. Thank you! – Paolo Nov 07 '13 at 10:33