休止状態を実装した 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);
}
});
}