views:

267

answers:

1

I have a Person entity mapped by Hibernate to a database table in a database catalog "Active". After a period of time, records in this database table in the "Active" catalog are archived/moved to an exact copy of the table in a database Catalog "History". I have the need to retrieve from both the Active and History Catalogs. Is there a better way to model this with Hibernate annotations than making an abstract class that 2 classes extend from.

This is what I have now.

@MappedSuperclass
public abstract class Person  {

    @Id
    private Integer id;
    private String name;
}

@Entity
@Table(name="Person", catalog="Active")
public class PersonActive extends Person {
}

@Entity
@Table(name="Person", catalog="History")
public class PersonHistory extends Person {
}
A: 

To my knowledge, that would be the right way to do it with annotations (you kinda have two tables so you need two entities). Then run a polymorphic query on the Person entity. I find this pretty clean by the way.

PS: Can you add a pointer on how to do this with mapping files, I'm really curious.

Pascal Thivent
Thanks for the insight. In the past when I've had this business requirement with other projects that I was using Spring JDBC, I would just have one class and one row mapper for both of the tables.I haven't done it before with hibernate, but here's an examplehttp://old.nabble.com/Mapping-class-to-multiple-tables-with-entity-name-(hibernate-plugin)-to9547563.html<hibernate-mapping> <class table="CLASS" name="some.my.Class"> (...) </class> <class entity-name="archived" table="ARCHIVED_CLASS" name="some.my.Class"> (...) </class> </hibernate-mapping>
Daniel Spivey