我在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.
答案 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);
}
});
...