[英]Hibernate: How do I link a subclass to its superclass?
I'm having a little problem setting up my webshop project. 我在设置网上商店项目时遇到了一些问题。 Thing is, I have a User() superclass and two subclasses, PrivateUser and BusinessUser. 问题是,我有一个User()超类和两个子类PrivateUser和BusinessUser。
Now, I'm not quite sure how to get my head around storing this relationship via hibernate. 现在,我不确定如何通过休眠存储这种关系。
For the purpose of this question, the User() class contains only one field: 出于这个问题的目的,User()类仅包含一个字段:
String address; 字符串地址;
the PrivateUser contains: PrivateUser包含:
String firstName; 字符串firstName;
and the BusinessUser contains: 并且BusinessUser包含:
String CompanyName; 字符串CompanyName;
Each field has its getter and setter. 每个字段都有其getter和setter。 As is right now, I would only store and be able to get firstName and companyName. 就像现在一样,我只会存储并能够获取firstName和companyName。 When I fetch a user from my DB using Hibernate I would get a PrivateUser/BusinessUser with a null address. 当我使用Hibernate从数据库中获取用户时,我将获得一个带空地址的PrivateUser / BusinessUser。
Bottom line is, could someone point me towards a useful tutorial or better yet show a similar example code? 最重要的是,有人可以向我介绍有用的教程,或者更好地显示类似的示例代码吗?
Thanks! 谢谢!
The Hibernate online documentation has some very good information on mapping your classes, in particular this section on setting up subclasses: Hibernate在线文档提供了有关映射类的一些非常好的信息,特别是关于设置子类的这一部分:
http://docs.jboss.org/hibernate/stable/core/manual/en/html/mapping.html#mapping-declaration-subclass http://docs.jboss.org/hibernate/stable/core/manual/en/html/mapping.html#mapping-declaration-subclass
You want to pay special attention to using discriminator values and such if you are storing your sub-classed objects in a common table. 您要特别注意使用鉴别符值,例如,如果要将子类对象存储在公用表中,则应特别注意。
You might also consider picking up a copy of Java Persistence with Hibernate, which is considered the "Hibernate Bible" amongst my co-workers. 您可能还考虑使用Hibernate来获取Java Persistence的副本,这在我的同事中被视为“ Hibernate Bible”。
http://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/1932394885/ref=sr_1_1?ie=UTF8&s=books&qid=1272501916&sr=8-1 http://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/1932394885/ref=sr_1_1?ie=UTF8&s=books&qid=1272501916&sr=8-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.