Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a JPA Query in play Framework

I'm new to Play and also to hibernate and JPA. I'm using MySql DB and JPA I have included

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import play.db.jpa.JPA;
import play.mvc.Controller;
import play.db.jpa.*;

I have this Query

List languages = FormLanguages.findAll();
render(languages);

Which runs correctly, but i want to select based on id, something like this

"select * from FormLanguages where id>10"

When i use like this

Query query = JPA.em().createQuery("select * from FormLanguages");
List<FormLanguages> articles = query.getResultList();
render(articles);

Which gives me IllegalArgumentException error

When use like this

List queryList = FormLanguages.em().createQuery("select * from FormLanguages").getResultList();
render(queryList);

which gives the same error please help me how to write Query

Also suggest me some websites

like image 308
Arasu Avatar asked Jul 27 '11 09:07

Arasu


1 Answers

In your scenario:

List languages = FormLanguages.find("id > ?",10).fetch();

Should work.

This one and also this may help you learn the JPA query language. Once you are familiar with them, using find you can launch those queries. Or use named queries.

like image 155
Pere Villega Avatar answered Nov 25 '22 00:11

Pere Villega