Hibernate Annotation for Entity existing in more than 1 catalog

那年仲夏 提交于 2019-12-03 13:59:22

问题


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 {
}

回答1:


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.




回答2:


My thought would be to write a query to select both tables from db A and B. then create a query with hibernate and map it to your class.

example:

@Entity
@NamedNativeQuery(
name="GetAllPerson",
query="select * from A.Person inner join B.Person on A.Person.Id = B.Person.Id"
)
public class Person  {
...
}

Not sure if it could work, your question made me also curious about the best way to do it :). I'll test it tonight after work and see if its any good.




回答3:


I think there is a subproject of hibernate named shards. It is designed to work with multiple relational databases. If you want to use it, you may need big changes in your code.



来源:https://stackoverflow.com/questions/2380754/hibernate-annotation-for-entity-existing-in-more-than-1-catalog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!