在运行时更改实体的表名称? [英] Change Table Name of an Entity on runtime?

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

问题描述

这个表格是每月生成的.基本上所有月度表的表结构都是一样的.

There is this table that is being generated on a monthly basis. Basically the table structure of all monthly tables 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?

推荐答案

实体的表名是否可以在运行时更改如下,因为它们毕竟具有相同的表结构?

Is it possible to change the table name of an entity as follows on runtime since they have the same table structure after all?

这实际上是不可能的,至少在以下问题中提到的标准 JPA 不可能(这并不意味着我是用非标准 JPA 做到的):

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 没有提供一种方法来改变"已经初始化的持久性单元的给定实体(以及相关的预编译 CRUD 查询、预编译命名查询等).

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).

不过,由于您使用的是 Hibernate,不妨看看 http://www.hibernate.org/171.html 以了解使用 Hibernate Core API 的可能性.

Still, since you're using Hibernate, maybe have a look at http://www.hibernate.org/171.html to get an idea of what would be possible using Hibernate Core API.

我能想到的另一个选择是使用数据库同义词/别名:FOO 将是 FOO_JAN2010 的别名 直到...您将别名更改为指向 FOO_FEB2010.我从来没有测试过这个,我不知道它是否适合你的需求.但这是另一个想法.

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.

这篇关于在运行时更改实体的表名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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