开发者

Hibernate: Mapping single column to different tables

I am having the following tables:

Template(
Template_Id,
Template_Name,
Component_Type,
Component_Id
)

Division(
Division_Id,
Division_Name
)

SubDivision(
SubDivision_Id,
SubDivision_Name
)

Department(
Department_Id,
Department_Name
)

In the Template table, th开发者_如何学运维e Component_Type field can contain the value Division or SubDivision or Department.

Component_Id will contain Division_Id or SubDivision_Id or Depatment_Id depending on Component_Type.

I am aware that the Template table is not completely normalized and it would be better to have different tables like Template_Division, Template_SubDivision etc... However, these are existing tables and we cannot modify them.

My question is, How should I configure the Component_Id column in Template class?

Template.java will look like the following:-

public class Template{

@Id
@Column(name="Template_Id)
private Long id;

@Column(name="Template_Name)
private String name;

@Column(name="Component_Type")
private String componentType; 

}

Can someone please explain how to configure the Component_Id column?

Thanks.


You can use @Any annotation. Note that since Hibernate 3.5 you have to access Hibernate via Session interface (i.e. not EntityManager) in order to use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜