Java 在运行时更改实体的表名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3879607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Table Name of an Entity on runtime?
提问by Joopiter
There is this table that is being generated on a monthly basis. Basically the table structure of all monthlytables is the same.
有这个表是每月生成的。基本上所有月表的表结构都是一样的。
Since it would be a lot of work to map the same entity just with a different table name,
由于仅使用不同的表名映射相同的实体将需要大量工作,
Is it possible to change the table name of an entity as follows on runtime since they have the same table structure after all?
是否可以在运行时更改实体的表名,因为它们毕竟具有相同的表结构?
@Entity
@Table(name="FOO_JAN2010") // any other ways to generate this dynamically?
public class FooJan2010Table { // if we can dynamically set the table name this can be simply named FooTable
...
}
If not, what approach can you suggest?
如果没有,您可以建议什么方法?
采纳答案by Pascal Thivent
Is it possible to change the table name of an entity as follows on runtime since they have the same table structure after all?
是否可以在运行时更改实体的表名,因为它们毕竟具有相同的表结构?
This is not really possible, at least not with standard JPA (which doesn't mean I did it with non standard JPA) as mentioned in questions such as:
这实际上是不可能的,至少不是使用标准 JPA(这并不意味着我使用非标准 JPA 做到的),如以下问题中所述:
- In @Table(name = “tableName”) - make “tableName” a variable in JPA
- JPA: How do I specify the table name corresponding to a class at runtime?
- Hibernate or iBatis or something else?
To summarize, JPA doesn't offer a way to "alter" a given entity of an already initialized persistence unit (and the related pre-compiled CRUD queries, the pre-compiled named queries, etc).
总而言之,JPA 不提供“更改”已初始化持久性单元(以及相关的预编译 CRUD 查询、预编译命名查询等)的给定实体的方法。
Still, since you're using Hibernate, maybe have a look at http://www.hibernate.org/171.htmlto get an idea of what would be possible using Hibernate Core API.
不过,由于您使用的是 Hibernate,也许可以查看http://www.hibernate.org/171.html以了解使用 Hibernate Core API 的可能性。
Another option I can think of would be to use a database synonym/ alias: FOO
would be an alias for FOO_JAN2010
until... you change the alias to point on FOO_FEB2010
. I've never tested this, I don't know if it will suit your needs. But it's another idea.
我能想到的另一个选择是使用数据库同义词/别名:FOO
将是一个别名,FOO_JAN2010
直到...您将别名更改为指向 on FOO_FEB2010
。我从来没有测试过这个,我不知道它是否适合你的需求。但这是另一个想法。
回答by Henning
I cannot imagine a good way to map this. If this is a legacy database, you'll have a hard time using JPA to access it.
我无法想象一个很好的方法来映射这个。如果这是一个遗留数据库,您将很难使用 JPA 访问它。
If, however, the database layout is under your control, then I'd change it. Having multiple tables with the exact same layout is usually bad design. You could achieve the same result by having only one table with extra columns for year and month. Don't forget to put an index on the two columns.
但是,如果数据库布局在您的控制之下,那么我会更改它。拥有多个布局完全相同的表通常是糟糕的设计。您可以通过只有一个带有年份和月份额外列的表来获得相同的结果。不要忘记在两列上放置索引。
回答by Sean Patrick Floyd
You canprobably do it using Naming Strategiesif you use Hibernate as JPA provider. See my answer to this previous questionfor reference.
如果您使用 Hibernate 作为 JPA 提供程序,您可能可以使用命名策略来做到这一点。请参阅我对上一个问题的回答以供参考。
You should be able to design your naming strategy to return table names in a dynamic way.
您应该能够设计命名策略以动态方式返回表名。
The question whether you shoulddo it that way is a completely different one, though.
不过,你是否应该这样做是一个完全不同的问题。
Also, thanks Pascal for reminding me, this will only work if the EntityManagerFactory is recreated once per month (there are many ways to do that, restarting the webapp being the simplest one)
另外,感谢 Pascal 提醒我,这仅在 EntityManagerFactory 每月重新创建一次时才有效(有很多方法可以做到,重启 webapp 是最简单的方法)
回答by Denilson
I could not figure this one either, had a similar requirement.
我也想不出这个,有类似的要求。
Since my table name changed relatively infrequently (daily) , I ended up defining a DB alias to the "active" table at the RDBMS (I am using DB2) and referenced the table alias name in the @Table annotation.
由于我的表名相对很少(每天)更改,因此我最终在 RDBMS(我使用的是 DB2)中为“活动”表定义了一个数据库别名,并在 @Table 注释中引用了表别名。
I am fully aware this is not strictly what the OP asked, but thought I would share the experience.
我完全知道这不是 OP 严格要求的,但我想我会分享经验。