Hibernate: Data Object with a dynamic table name by Annotations

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> @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
...
</code>
<code> @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 ... </code>
 @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 
    ...

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.

Another one Architecture, more complez but elegant:

YES, You can change the table names using NamingStrategies:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class MyNamingStrategy extends DefaultNamingStrategy {
...
@Override
public String tableName(String tableName) {
return tableName+yearSuffixTable;
}
...
}
</code>
<code>public class MyNamingStrategy extends DefaultNamingStrategy { ... @Override public String tableName(String tableName) { return tableName+yearSuffixTable; } ... } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> SessionFactory sessionFactory;
Configuration config = new AnnotationConfiguration()
.configure("hibernate.cfg.xml")
.setNamingStrategy( new MyNamingStrategy () );
sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
</code>
<code> SessionFactory sessionFactory; Configuration config = new AnnotationConfiguration() .configure("hibernate.cfg.xml") .setNamingStrategy( new MyNamingStrategy () ); sessionFactory = config.buildSessionFactory(); session = sessionFactory.openSession(); </code>
  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.

2

You should try Hibernate Envers for historic data.

6

In Hibernate you map 1 class to 1 table. You can not reuse the same Entity to map several tables dynamically.

Hibernate Envers is a quite good solution for Historic data, but you still will not be able to do what you try (dynamically grow the number of tables without touching mapper Entities).

4

Thanks to @CodeBrickie and @edutesoy I found Envers.

I configure the hibernate config file with AUD suffix and I create new hibernate config files per year (hibernate.cfg.2009.xml, hibernate.cfg.2010.xml, hibernate.cfg.2011.xml…) with the year-suffix.

When I save data, always is audited in AUD table. On January 1, automatically:

  • _AUD TABLE is renamed as _PAST_YEAR table.
  • A new _AUD table is created.
  • A new hibernate.cfg.past_year.xml is created with the new suffix.

When I need to get data, I load the corresponding hibernate configuration file.

Hope this helps to others 🙂

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Table(name="emd_employee_1001")
</code>
<code>@Table(name="emd_employee_1001") </code>
@Table(name="emd_employee_1001")

In the above annotation file name can pass as parameter, for example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>x=1001
@Table(name="emd_employee_+x+")
</code>
<code>x=1001 @Table(name="emd_employee_+x+") </code>
x=1001 
@Table(name="emd_employee_+x+")

1

Modified at runtime(I think it’s the best way):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Session session = super.getSession();
SQLQuery query = session.createSQLQuery("raw sql");
query.setParameter(":abc", "value");
query.addEntity(Some.class);
return query.list();
</code>
<code>Session session = super.getSession(); SQLQuery query = session.createSQLQuery("raw sql"); query.setParameter(":abc", "value"); query.addEntity(Some.class); return query.list(); </code>
Session session = super.getSession();
    SQLQuery query = session.createSQLQuery("raw sql");
    query.setParameter(":abc", "value");
    query.addEntity(Some.class);
    return query.list();

1

You can use Hibernate interceptors to change the table in the generated SQL statements.

For your case you can define your table class like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Entity
@org.hibernate.annotations.Proxy(lazy=false)
@Table(name=PersonTableNameReplacer.PLACEHOLDER, schema="MySchema")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class ProfileData implements Serializable {
</code>
<code>@Entity @org.hibernate.annotations.Proxy(lazy=false) @Table(name=PersonTableNameReplacer.PLACEHOLDER, schema="MySchema") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) public class ProfileData implements Serializable { </code>
@Entity
@org.hibernate.annotations.Proxy(lazy=false)
@Table(name=PersonTableNameReplacer.PLACEHOLDER, schema="MySchema")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class ProfileData implements Serializable {

and define your Hibernate interceptor in a following way:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class TableNameReplacer extends EmptyInterceptor {
public static final String TABLE_PLACEHOLDER = "{person_table_placeholder}";
@Override
public String onPrepareStatement(String sql) {
if (sql.contains(TABLE_PLACEHOLDER )) {
String replacement = "{your logic to fill proper table name}";
sql = sql.replace(TABLE_SUFFIX_PLACEHOLDER, replacement);
}
return super.onPrepareStatement(sql);
}
</code>
<code>public class TableNameReplacer extends EmptyInterceptor { public static final String TABLE_PLACEHOLDER = "{person_table_placeholder}"; @Override public String onPrepareStatement(String sql) { if (sql.contains(TABLE_PLACEHOLDER )) { String replacement = "{your logic to fill proper table name}"; sql = sql.replace(TABLE_SUFFIX_PLACEHOLDER, replacement); } return super.onPrepareStatement(sql); } </code>
public class TableNameReplacer extends EmptyInterceptor {

    public static final String TABLE_PLACEHOLDER = "{person_table_placeholder}";

    @Override
    public String onPrepareStatement(String sql) {
        if (sql.contains(TABLE_PLACEHOLDER )) {
            String replacement = "{your logic to fill proper table name}";


            sql = sql.replace(TABLE_SUFFIX_PLACEHOLDER, replacement);
        }

        return super.onPrepareStatement(sql);
    }

Using this approach you’re free to modify generated SQL and replace the table name there as you wish.

I recommend to use good placeholder value which you’re sure will not be a part of actual values being saved to the table (or you can only limit this to select statements if you only read the data).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật