JPA:如何在运行时指定类对应的表名? [英] JPA: How do I specify the table name corresponding to a class at runtime?

查看:34
本文介绍了JPA:如何在运行时指定类对应的表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:我非常熟悉 Java,但不熟悉 Hibernate 或 JPA - 还没有 :) )

(note: I'm quite familiar with Java, but not with Hibernate or JPA - yet :) )

我想编写一个通过 JPA 与 DB2/400 数据库对话的应用程序,现在我可以获取表中的所有条目并将它们列出到 System.out(使用 MyEclipse 进行逆向工程).我知道 @Table 注释会导致名称与类一起静态编译,但我需要能够使用在运行时提供名称和模式的表(它们的定义是相同的,但我们有很多他们).

I want to write an application which talks to a DB2/400 database through JPA and I have now that I can get all entries in the table and list them to System.out (used MyEclipse to reverse engineer). I understand that the @Table annotation results in the name being statically compiled with the class, but I need to be able to work with a table where the name and schema are provided at runtime (their defintion are the same, but we have many of them).

显然这不是那么容易做到,我希望得到提示.

Apparently this is not SO easy to do, and I'd appreciate a hint.

我目前选择 Hibernate 作为 JPA 提供程序,因为它可以处理这些数据库表没有被记录的问题.

I have currently chosen Hibernate as the JPA provider, as it can handle that these database tables are not journalled.

那么,问题是,我如何在运行时告诉 JPA 的 Hibernate 实现类 A 对应于数据库表 B?

So, the question is, how can I at runtime tell the Hibernate implementation of JPA that class A corresponds to database table B?

(Hibernate NamingStrategy 中被覆盖的 tableName() 可能允许我解决这个内在限制,但我仍然更喜欢与供应商无关的 JPA 解决方案)

(edit: an overridden tableName() in the Hibernate NamingStrategy may allow me to work around this intrinsic limitation, but I still would prefer a vendor agnostic JPA solution)

推荐答案

你需要使用XML 版本 配置而不是注释.这样您就可以在运行时动态生成 XML.

You need to use the XML version of the configuration rather than the annotations. That way you can dynamically generate the XML at runtime.

或者像 Dynamic JPA 之类的东西你会感兴趣吗?

Or maybe something like Dynamic JPA would interest you?

我认为有必要进一步澄清这个问题的问题.

I think it's necessary to further clarify the issues with this problem.

第一个问题是:可以存储实体的表集是否已知?我的意思是你不是在运行时动态创建表并希望将实体与它们相关联.例如,这种情况要求在编译时知道三个表.如果是这种情况,您可以使用 JPA 继承.OpenJPA 文档详细介绍了 每个类的表继承策略.

The first question is: are the set of tables where an entity can be stored known? By this I mean you aren't dynamically creating tables at runtime and wanting to associate entities with them. This scenario calls for, say, three tables to be known at compile-time. If that is the case you can possibly use JPA inheritance. The OpenJPA documentation details the table per class inheritance strategy.

这种方法的优点是纯JPA.但是它有局限性,因为必须知道表,并且您不能轻易更改给定对象存储在哪个表中(如果这是您的要求),就像面向对象系统中的对象通常不会更改类一样或输入.

The advantage of this method is that it is pure JPA. It comes with limitations however, being that the tables have to be known and you can't easily change which table a given object is stored in (if that's a requirement for you), just like objects in OO systems don't generally change class or type.

如果您希望这是真正动态的并在表之间移动实体(基本上),那么我不确定 JPA 是否适合您.可怕的魔法用于制作 JPA工作包括加载时编织(检测)和通常一级或多级缓存.更重要的是,实体管理器需要记录更改并处理托管对象的更新.据我所知,没有什么简单的工具可以指示实体管理器将给定的实体存储在一个或另一个表中.

If you want this to be truly dynamic and to move entities between tables (essentially) then I'm not sure JPA is the right tool for you. An awful lot of magic goes into making JPA work including load-time weaving (instrumentation) and usually one or more levels of caching. What's more the entity manager needs to record changes and handle updates of managed objects. There is no easy facility that I know of to instruct the entity manager that a given entity should be stored in one table or another.

这样的移动操作将隐含地要求从一个表中删除并插入到另一个表中.如果有子实体,这会变得更加困难.请注意,并非不可能,但这是一个不寻常的角落案例,我不确定有人会打扰.

Such a move operation would implicitly require a delete from one table and insertion into another. If there are child entities this gets more difficult. Not impossible mind you but it's such an unusual corner case I'm not sure anyone would ever bother.

一个较低级别的 SQL/JDBC 框架,例如 Ibatis 可能是一个更好的选择,因为它会给你你想要的控制.

A lower-level SQL/JDBC framework such as Ibatis may be a better bet as it will give you the control that you want.

我还考虑过在运行时动态更改或分配注释.虽然我还不确定这是否可能,但即使是我也不确定它是否一定会有所帮助.我无法想象实体管理器或缓存不会因为发生这种事情而绝望地混淆.

I've also given thought to dynamically changing or assigning at annotations at runtime. While I'm not yet sure if that's even possible, even if it is I'm not sure it'd necessarily help. I can't imagine an entity manager or the caching not getting hopelessly confused by that kind of thing happening.

我想到的另一种可能性是在运行时动态创建子类(作为匿名子类),但这仍然存在注释问题,而且我不确定您如何将其添加到现有的持久性单元中.

The other possibility I thought of was dynamically creating subclasses at runtime (as anonymous subclasses) but that still has the annotation problem and again I'm not sure how you add that to an existing persistence unit.

如果您提供有关您正在做什么以及为什么这样做的更多详细信息,这可能会有所帮助.不管它是什么,我倾向于认为你需要重新考虑你在做什么或你是如何做的,或者你需要选择不同的持久性技术.

It might help if you provided some more detail on what you're doing and why. Whatever it is though, I'm leaning towards thinking you need to rethink what you're doing or how you're doing it or you need to pick a different persistence technology.

这篇关于JPA:如何在运行时指定类对应的表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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