Hibernate: Data Object with a dynamic table name by Annotations

后端 未结 6 861
抹茶落季
抹茶落季 2020-12-09 13:06

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

 @Entity
 @org.hibernate.annotations.Proxy(lazy=false)         


        
6条回答
  •  孤城傲影
    2020-12-09 13:11

    Another one Architecture, more complez but elegant:

    YES, You can change the table names using NamingStrategies:

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

    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.

    Thanks.

提交回复
热议问题