1

How to get JPA work in Play MVC. I tried a lot of variants. But all unsuccesfull.

public class Application extends Controller {

    public static void book() {
        EntityManager em = JPA.em();
        List<Book> bookList = em.createNativeQuery("select * from book").getResultList();

        render( bookList );
    }


    public static void index() {
        render( );
    }
}

I got error like this:

A JPA error occurred (The JPA context is not initialized. JPA Entity Manager automatically start when one or more classes annotated with the @javax.persistence.Entity annotation are found in the application.):

Second variant doesnt' work:

@NamedQuery(name = "findFoo", query = "select f from Foo f where f.state in :stateInList")

final Query query = this.entityManager.createNamedQuery("findFoo");

How to get worked with NamedQuery ?? Where to place NamedQuery ??

4

1 回答 1

0

您的命名查询是正确的,您可以将其写在实体上方

@Entity
@Table(name = "Foo")
@NamedQuery(
{
    @NamedQuery(name = "findFoo", query = "select f from Foo f where f.state in :stateInList")
)}
public class Foo
{
 ....

}
于 2013-08-01T02:38:41.447 回答