简体   繁体   English

Hibernate:如何将子类链接到其超类?

[英]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.

相关问题 在引用其超类时如何调用子类的方法? - How do i call a method of a subclass when referencing its superclass? 休眠-使用每个子类的表-如何将现有的超类对象链接到子类对象 - Hibernate -using Table per subclass - how to link an existing superclass object to a subclass object Spring Data JPA(Hibernate):如何仅使用抽象超类中的字段来检索具体实体? - Spring Data JPA(Hibernate): How do I retrieve a concrete entity using only a field in its abstract superclass? 如何将子类参数传递给超类私有变量? - how do I pass subclass parameters into the superclass private variables? 如何访问从超类传递下来的子类属性? - How do I access subclass attributes passed down from superclass? 通过子类实例调用时,如何在子类的方法中使用子类的静态字段? - How do I use a subclass' static fields in superclass' method when calling via instance of subclass? 在Java中,如何使子类的实例变量具有子类的类型,而不是父类的类型? - In Java, how do I make instance variables of a subclass have the type of the subclass, rather than of the superclass? 父类中的子类如何进行多态? - How can do a Polymorphism with subclass in a superclass? 如何从 Java 的超类中的 static 方法创建子类的新实例? - How can I create a new instance of a subclass from a static method in its superclass in Java? 如何通过将其超类作为方法的参数传递来访问子类属性 - How can I access subclass attribute through passing its superclass as parameter of a method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM