Play Framework 2.2。* SecureSocial Service没有绑定到此线程的EntityManager

时间:2014-03-28 10:03:22

标签: playframework entitymanager securesocial

我在Play Framework项目中使用SecureSocial。我希望在我的服务中使用JPA(创建,查找用户和令牌)。我有错误:

[RuntimeException: No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread.]

我的代码:

public Identity doFindByEmailAndProvider(String email, String providerId) {
    List<User> userList = User.findByEmailAndProvider(email, providerId);
    if(userList.size() != 1){
        logger.debug("found a null in doFindByEmailAndProvider");
        return null;
    }....

如果我使用JPA.withTransaction

执行此操作
public Identity doFindByEmailAndProvider(String email, String providerId) {
    List<User> userList;
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            userList = User.findByEmailAndProvider(email, providerId);
        }
    });

它告诉

Variable 'userList' is accessed from within inner class. Needs to be declared final.

1 个答案:

答案 0 :(得分:1)

如果您不在具有JPA.withTransaction()注释的控制器的上下文中,则必须在@Transactional内执行代码。

要使您的上一个代码段生效,您只需声明userList变量final即可。 :

public Identity doFindByEmailAndProvider(String email, String providerId) {
    final List<User> userList;
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            userList = User.findByEmailAndProvider(email, providerId);
        }
    });
    ...