[2.0] Hibernate DAO problem

370 views
Skip to first unread message

Bence

unread,
Aug 21, 2012, 8:13:13 AM8/21/12
to play-fr...@googlegroups.com
I would like to use Hibernate from outside the controller classes. I get the following exception:


[RuntimeException: No EntityManager bound to this thread. Try to annotate your action method with @play.db.jpa.Transactional]

@play.db.jpa.Transactional
private static AuthUser getUserByNameAndPass(String userName, String password){
    return (AuthUser) JPA.em().createNamedQuery(AuthUser.getByCredentials).setParameter("userName", userName).setParameter("password", password).getSingleResult();
}

Bence

unread,
Aug 21, 2012, 8:46:43 AM8/21/12
to play-fr...@googlegroups.com
Now I modified:

return (AuthUser) JPA.em("pu").createNamedQuery(AuthUser.getByCredentials).setParameter("userName", userName).setParameter("password", password).getSingleResult();

...and now I'm getting a new error:

Execution exception [[RuntimeException: No JPA EntityManagerFactory configured for name [auth]]]

my conf/META-INF/persistence.xml is:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>MyDS</non-jta-data-source>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

Bence

unread,
Aug 21, 2012, 9:39:59 AM8/21/12
to play-fr...@googlegroups.com
Is there a way to get more detailed exceptions? It does not help too much:

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[RuntimeException: No JPA EntityManagerFactory
configured for name [auth]]]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:82) [play_2.9.1.jar:2.0]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:63) [play_2.9.1.jar:2.0]
        at akka.actor.Actor$class.apply(Actor.scala:290) [akka-actor.jar:2.0]
        at play.core.ActionInvoker.apply(Invoker.scala:61) [play_2.9.1.jar:2.0]
        at akka.actor.ActorCell.invoke(ActorCell.scala:617) [akka-actor.jar:2.0]
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:179) [akka-actor.jar:2.0]
Caused by: java.lang.RuntimeException: No JPA EntityManagerFactory configured for name [auth]
        at play.db.jpa.JPA.em(JPA.java:33) ~[play_2.9.1.jar:2.0]
        at com.stepman.ag.auth.AuthenticationService.getByCredentials(AuthenticationService.java:80) ~[classes/:na]
        at com.stepman.ag.auth.AuthenticationService.handleAuthentication(AuthenticationService.java:29) ~[classes/:na]
        at com.stepman.ag.controllers.Login.authenticate(Login.java:20) ~[classes/:na]
        at Routes$$anonfun$routes$1$$anonfun$apply$3$$anonfun$apply$4.apply(routes_routing.scala:52) ~[classes/:na]
        at Routes$$anonfun$routes$1$$anonfun$apply$3$$anonfun$apply$4.apply(routes_routing.scala:52) ~[classes/:na]

Bence

unread,
Aug 22, 2012, 5:21:41 AM8/22/12
to play-fr...@googlegroups.com
Solved.

I needed to add the following line to application.conf:
jpa.default=pu

brainwatch

unread,
Aug 22, 2012, 6:23:51 AM8/22/12
to play-fr...@googlegroups.com
Hi Bence

As per the documentation @Transactional can be applied only for the Controller actions. I wonder how did you manage to get this stuff working. After stumbling on this post I tried the same and it does not work.

My use case is :
I have to catch the transactional exception and throw a readable error json message to the user which I'm not able to achieve 'cos of @transactional annotation handles the rollback automatically which makes the controller not go through the catch block at all.

Any suggestion ?

-bw

Bence

unread,
Aug 22, 2012, 6:47:22 AM8/22/12
to play-fr...@googlegroups.com
Did you use @play.db.jpa.Transactional and JPA.em() ?
It is working for me without anything else (extending/implmenting anything, or having any other kind of annotation)

brainwatch

unread,
Aug 22, 2012, 7:07:31 AM8/22/12
to play-fr...@googlegroups.com
Yes. I tried the same but not working.

- bm

brainwatch

unread,
Aug 22, 2012, 7:09:13 AM8/22/12
to play-fr...@googlegroups.com
Were you able to catch the transactional exception ( Runtime ) and handle it ( In you logic you should be able to ). Let me know is there any hack you have made to make it work.

- bm

Bence Takács

unread,
Aug 22, 2012, 7:54:42 AM8/22/12
to play-fr...@googlegroups.com
Good question ;-)
No, I have only a POC, not a production app. You should ask more experienced players.

Bence

unread,
Aug 29, 2012, 4:19:57 PM8/29/12
to play-fr...@googlegroups.com
So, let me see:
There is no answer, because this is a very conscious concept not to create DAOs in Play.

Since Anorm and Ebean so conscise and practical that there is no need to that concept.
 
...and this is why there is no answer to this kind of questions:
https://groups.google.com/forum/#!searchin/play-framework/DAO/play-framework/I1hwzBXVvxU/4rP5JPmzAhsJ
https://groups.google.com/forum/#!searchin/play-framework/DAO/play-framework/ZNNafEyUIn4/b2h2RKctgRcJ

Bence

unread,
Aug 29, 2012, 4:24:53 PM8/29/12
to play-fr...@googlegroups.com
Well, maybe I know the reason. There are three conditions needed:
- use @play.db.jpa.Transactional
- on a static method
- use JPA.em() to achieve the entityManager

I used a static method - which is not conventional for DAOs, but conventional in Play ;-)
Reply all
Reply to author
Forward
0 new messages