使用在JBoss上运行的EclipseLink在运行时修改JPA模式 [英] Modify JPA schema at runtime with EclipseLink running on JBoss

查看:127
本文介绍了使用在JBoss上运行的EclipseLink在运行时修改JPA模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JPA和JBoss的新手,所以如果这是一个愚蠢的问题,请提前抱歉.

I am new to JPA and JBoss, so sorry in advance if this is a stupid question.

我有一个使用EclipseLink在JBoss上运行的应用程序,该应用程序基于JPQL字符串从各种数据库中检索实体.除一个数据库包含多个架构的数据库外,这非常有效.我希望能够在运行时指定要使用的架构,并选择属于该架构的所有实体,而无需修改JPQL字符串.

I have application running on JBoss using EclipseLink that retrieves entities from various databases based on a JPQL string. This is working great, except one database contains multiple schema's. I would like to be able to specify which schema to use, at runtime, and select all entities that belong to that schema, without modifying the JPQL string.

为了进一步使事情复杂化,我还想在同一查询中跨模式进行联接.在这种情况下,可以更改JPQL查询,或者可以设置指定每个实体模式的参数.

To further complicate things, I would also like to join across schema's, in the same query. In this case, the JPQL query could change, or parameters could be set which specifies each entities schema.

经过一番搜索,我发现了这一点:

After some searching I came across this:

JPA :如何在运行时指定与类相对应的表名?

哪个有使用ASM的解决方案来动态修改Table批注.我试图用它来修改表注解的schema属性,但是我一生都无法使用它.看来Eclipse Link使用了不同的类加载器来初始化我不知道如何访问的实体类,因此更改注释无效.

Which has a solution using ASM that dynamically modifies the Table annotation. I have tried to use this to modify the Table annotation's schema property but for the life of me I cannot get it to work; it seems that Eclipse Link uses a different class loader to init the entity classes that I don't know how to access and thus changing the annotation has no effect.

另一个解决方案是每个模式都有多个xml配置文件.看起来不雅;当我添加一个新实体时,我必须记住将其添加到每个模式的xml文件中.我也不知道如何跨多个架构进行这种连接.

Another solution is have multiple xml configuration files for each schema. It seems inelegant; when I add a new entity I have to remember to add it to every schema's xml file. I also do not know how I can make this join across multiple schema's.

有什么办法解决这个问题?我是否缺少某些东西?或者这会使JPA超出了预期的范围?

Is there any way of solving this problem? Am I missing something or is this pushing JPA beyond what it was intended for?

推荐答案

每个架构应具有不同的持久性单元.您可以使用相同的类/映射,只需要一个定义不同默认架构的orm.xml文件即可.该文件不需要任何其他内容,您仍然可以在注释中或在单独的orm.xml文件中映射对象.

You should have a different persistence unit per schema. You can use the same classes/mappings, you would just need an orm.xml file that defines the different default schema. The file doesn't need anything else in it, you can still map your objects in annotations, or in a separate orm.xml file.

要拥有一个持久单元,在一个模式中包含一些对象,而在另一个模式中包含一些对象,则可以再次在orm.xml文件中执行此操作.您只需要在orm.xml文件中包括使用非默认模式的实体. EclipseLink总是将注释和XML合并,因此您可以同时使用它们和xml中仅包含所需的附加内容.

To have a persistence unit with some objects in one schema, and some in another, you again could do this in an orm.xml file. You will just need to include the entities that use the non-default schema in the orm.xml file. EclipseLink always merges annotations and XML, so you can use both and only have the additions you need in xml.

EclipseLink还允许您设置一个tableQualifier,它定义了默认模式.可以在会话的登录名上使用SessionCutsomizer进行设置.

EclipseLink also allows you to set a tableQualifier which defines the default schema. This can be set using a SessionCutsomizer on the Session's login.

这篇关于使用在JBoss上运行的EclipseLink在运行时修改JPA模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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