休眠:数据对象与注解动态表名 [英] Hibernate: Data Object with a dynamic table name by Annotations

查看:125
本文介绍了休眠:数据对象与注解动态表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据类为休眠相关联的表;想象实体的的是这样的:

I have a Data Class for Hibernate associated to a table; imagine the Entity Person like this:

 @Entity
 @org.hibernate.annotations.Proxy(lazy=false)
 @Table(name="Person", schema="MySchema")
 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
 public class ProfileData implements Serializable {

    private static final long serialVersionUID = -844564646821609090L;

    public PersonData() {
    }

    @Column(name="idPerson", nullable=false, unique=true)   
    @Id 
    ...

我需要通过多年的该表的创建历史表:Person2010,Person2011,Person2012 ...是否有可能无需创建新的数据对象?也许一个参数...?我不知道。

I need to create historic tables by years of this table: Person2010, Person2011, Person2012... Is it possible without creating new Data Objects? Maybe by a parameter...? I don´t know.

实体类是相同的,不断变化的表名和构造函数。

The Entity class is the same, changing the table name and the constructor.

推荐答案

另外一个建筑,更complez而优雅的:

Another one Architecture, more complez but elegant:

,您可以用更改表名的 NamingStrategies 的:

YES, You can change the table names using NamingStrategies:

public class MyNamingStrategy extends DefaultNamingStrategy {
   ...
   @Override
   public  String tableName(String tableName) {
      return tableName+yearSuffixTable;
   }
   ...
}

而且,当你想使用_year表,则必须建立与Hibernate会话重写RHE表名称:

And, when you wanna to use the _year tables, you must to create a session with Hibernate that override rhe table names:

  SessionFactory sessionFactory;
  Configuration config = new AnnotationConfiguration()
                         .configure("hibernate.cfg.xml")
                         .setNamingStrategy( new MyNamingStrategy () );
  sessionFactory = config.buildSessionFactory();
  session = sessionFactory.openSession();

有关我的建筑我在今年创建一个会话,并将其存储到应用的地图进行访问时,我需要它。

For my architecture I create a session by year and store it into Application map for access when I need it.

感谢。

这篇关于休眠:数据对象与注解动态表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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