播放框架2 +具有多个persistenceUnit的JPA [英] Play framework 2 + JPA with multiple persistenceUnit

查看:118
本文介绍了播放框架2 +具有多个persistenceUnit的JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Play和JPA方面苦苦挣扎,以便能够使用与两个不同的持久性单元相关联的两个不同的javax.persistence.Entity模型(需要能够连接到不同的数据库-例如Oracle和MySQL) db).

I'm struggling with Play and JPA in order to be able to use two different javax.persistence.Entity model associated to two different persistence units (needed to be able to connect to different DB - for example an Oracle and a MySQL db).

问题出在事务上,该事务始终绑定到默认的JPA persitenceUnit(请参阅jpa.default选项).

The problem come from the Transaction which is always bind to the default JPA persitenceUnit (see jpa.default option).

以下是两个控制器操作,这些操作显示了我发现的用于手动定义持久性的解决方案: 程序包控制器;

Here is two controller actions which show the solution I found to manually define the persistence : package controllers;

import models.Company;
import models.User;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;

public class Application extends Controller {
    //This method run with the otherPersistenceUnit
    @Transactional(value="other")
    public static Result test1() {
        JPA.em().persist(new Company("MyCompany"));

        //Transaction is run with the "defaultPersistenceUnit"
        JPA.withTransaction(new play.libs.F.Callback0() {
            @Override
            public void invoke() throws Throwable {
                JPA.em().persist(new User("Bobby"));
            }
        }); 
        return ok();
    }


    //This action run with the otherPersistenceUnit
    @Transactional
    public static Result test2() {
        JPA.em().persist(new User("Ryan"));

        try {
            JPA.withTransaction("other", false, new play.libs.F.Function0<Void>() {
                public Void apply() throws Throwable {
                    JPA.em().persist(new Company("YourCompany"));
                    return null;
                }
            });
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
        return ok();
    }
}

该解决方案似乎并不是真正的干净".我想知道您是否知道避免手动修改所使用交易的更好方法.

This solution doesn't seem to be really "clean". I'd like to know if you know a better way to avoid the need to manually modify the transaction used.

为此,我使用一个工作示例应用程序在git上创建了一个存储库,其中显示了我如何配置该项目.

For this purpose, I created a repo on git with a working sample application which shows how I configured the project.

https://github.com/cm0s/play2-jpa-multiple-persistenceunit

谢谢您的帮助

推荐答案

我也遇到了同样的问题.关于PersistenceUnit注释或getJPAConfig的建议太多.但它们似乎都不能在play框架中使用.
我发现了一种在我的项目中效果很好的方法.也许您可以尝试.
playframework2如何使用jpa打开多数据源配置
卢克!

i met the same problem, too. too many advices are about PersistenceUnit annotation or getJPAConfig. but both them seem not work in play framework.
i found out a method which works well in my projects. maybe you can try it.
playframework2 how to open multi-datasource configuration with jpa
gud luk!

这篇关于播放框架2 +具有多个persistenceUnit的JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆