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 に答える