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

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

问题描述

这个表格是按月生成的。基本上所有的每月表的表结构都是相同的。



因为将同一个实体映射为不同的表名,



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

  @Entity 
@Table(name =FOO_JAN2010)//任何其他方式来动态生成它?
public class FooJan2010Table {//如果我们可以动态设置表名,可以简单地命名为FooTable
...
}

如果不是,您可以提出什么方法?

解决方案


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

这是不可能的,至少不是像标准JPA那样(这并不意味着我使用非标准JPA)如问题中提到的那样:



总而言之,JPA不提供修改一个已经初始化的持久化单元的一个给定实体(以及相关的预编译的CRUD查询,预编译的命名查询等等)。

你正在使用Hibernate,也许看看 http://www.hibernate.org/171.html ,以了解使用Hibernate Core API的可能性。



另一个选项I可以想到会使用一个数据库别名 / 别名 FOO 会是<$ c $的别名直到...您将别名更改为指向 FOO_FEB2010 。我从来没有测试过,我不知道它是否会满足您的需求。但这是另一个想法。


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?

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:

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

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.

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天全站免登陆