0

我正在使用带有休眠实现的 JPA2.0 的 Play2.1.1 Java。

通过代码而不是像下面那样使用@transactional 来控制事务是正常的JPA 代码样式,有没有办法像下面一样在Play 中工作?或者如何使用 JPA.withtranaction() 来做?我试过了,不知道如何传入参数,我对功能代码不熟悉。多谢。请根据以下内容给我一些示例代码。

public  void createActorB(final String email, final String psw) throws Throwable {
    EntityManager manager = JPA.em();
    try {
        EntityTransaction ex = manager.getTransaction();
        this.dbActor.setEmail(email);
        this.dbActor.setCredential(psw);
        manager.persist(this.dbActor);
        ex.commit();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new ActorException(CODE.UNKNOWN, e);
    } finally {
        manager.close();
    }
}

现在我将下面的代码更改为从服务层开始事务,它看起来效率不高,还有其他写法吗?谢谢

private void internalCreateActor(String email, String psw) throws ActorException {
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(psw))
            throw new ActorException(CODE.INVALIDE_PARAMETER);
        try {
            this.dbActor.setEmail(email);
            this.dbActor.setCredential(psw);
            this.dbActor.setCreateD(new Date());
            JPA.em().persist(this.dbActor);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new ActorException(CODE.UNKNOWN, e);
        }
    }

 public void createActor(final String email, final String psw, final String cellPhone, final Actor.TYPE type)
            throws Throwable {

        JPA.withTransaction(new Callback0() {
            @Override
            public void invoke() throws Throwable {
                internalCreateActor(email, psw, cellPhone, type);
            }
        });
    }
4

2 回答 2

1

像这样的东西:

public static User getUserByIdentity(final AuthUserIdentity identity) {
    try {
        return JPA.withTransaction(new play.libs.F.Function0<User>() {
            public User apply() {       
                return User.findByAuthUserIdentity(identity);
            }
        });
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }       
}
于 2014-03-05T16:46:13.237 回答
0

经过一段时间的研究,我写了一个参考Play提供的JPA的方法JPAUtil,它可以正常使用,从服务层手动控制事务实际上无处不在。

public class JPAUtil {

    static ThreadLocal<EntityManager> currentEntityManager = new ThreadLocal<EntityManager>();

    /**
     * Get the EntityManager for specified persistence unit for this thread.
     */
    public static EntityManager em(String key) {
        Application app = Play.application();
        if (app == null) {
            throw new RuntimeException("No application running");
        }

        JPAPlugin jpaPlugin = app.plugin(JPAPlugin.class);
        if (jpaPlugin == null) {
            throw new RuntimeException("No JPA EntityManagerFactory configured for name [" + key + "]");
        }

        EntityManager em = jpaPlugin.em(key);
        if (em == null) {
            throw new RuntimeException("No JPA EntityManagerFactory configured for name [" + key + "]");
        }

        bindForCurrentThread(em);

        return em;
    }

    /**
     * Get the default EntityManager for this thread.
     */
    public static EntityManager em() {
        EntityManager em = currentEntityManager.get();
        if (em == null) {
            return em(Constants.DATASOURCEKEY);
        }
        return em;
    }

    /**
     * Bind an EntityManager to the current thread.
     */
    public static void bindForCurrentThread(EntityManager em) {
        currentEntityManager.set(em);
    }

    public static void closeEM() {
        EntityManager em = currentEntityManager.get();
        if (em != null) {
            em.close();
        }
        bindForCurrentThread(null);
    }

    public static void beginTransaction() {
        em().getTransaction().begin();
    }

    public static void commitTransaction() {
        em().getTransaction().commit();
    }

}
于 2013-09-04T10:11:24.170 回答