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
...
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:
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.
You should try Hibernate Envers for historic data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With