Hibernate:如何将子类链接到其超类?
我在设置网上商店项目时遇到一些问题。事实是,我有一个 User() 超类和两个子类:PrivateUser 和 BusinessUser。
现在,我不太确定如何通过休眠来存储这种关系。
就本问题而言,User() 类仅包含一个字段:
字符串地址;
PrivateUser 包含:
字符串名字;
BusinessUser 包含:
字符串公司名称;
每个字段都有其 getter 和 setter。现在,我只会存储并能够获取名字和公司名称。当我使用 Hibernate 从数据库中获取用户时,我会得到一个带有空地址的 PrivateUser/BusinessUser 。
最重要的是,有人可以向我指出一个有用的教程或者更好的展示类似的示例代码吗?
谢谢!
I'm having a little problem setting up my webshop project. Thing is, I have a User() superclass and two subclasses, PrivateUser and 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:
String address;
the PrivateUser contains:
String firstName;
and the BusinessUser contains:
String CompanyName;
Each field has its getter and setter. As is right now, I would only store and be able to get firstName and companyName. When I fetch a user from my DB using Hibernate I would get a PrivateUser/BusinessUser with a null address.
Bottom line is, could someone point me towards a useful tutorial or better yet show a similar example code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hibernate 在线文档有一些关于映射类的非常好的信息,特别是关于设置子类的部分:
http://docs.jboss.org/hibernate/stable/core/manual/en/html/mapping.html#mapping-declaration-subclass
如果您将子类对象存储在公用表中,您需要特别注意使用鉴别器值等。
您还可以考虑购买一本 Java Persistence with Hibernate,它被我的同事视为“Hibernate 圣经”。
http://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/ dp/1932394885/ref=sr_1_1?ie=UTF8&s=books&qid=1272501916&sr=8-1
The Hibernate online documentation has some very good information on mapping your classes, in particular this section on setting up subclasses:
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.
http://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/1932394885/ref=sr_1_1?ie=UTF8&s=books&qid=1272501916&sr=8-1