Extending hibernate entities with annotation

北城余情 提交于 2019-12-04 20:11:37

问题


i need to extend an entity, with the same characteristics without using abstract classes.

can i code something like below?

@Entity
@Table(name="ABC")
@SequenceGenerator(sequenceName="SEQ_ABC",name="idGenerator",allocationSize=1)
public class Abc {
.. // define members
}

@Entity
@Table(name="EX_ABC")
public class ExAbc extends Abs {
.. // define extras..
}

thx in advance


回答1:


Yes, this one works without any problems. However you should have a look at the inheritance annotation.

What's the problem: You have a table "Abc" which contains field1,filed2; Then you have ExAbc which contains the fields of "Abc" and in adition field3. Now, if you think in terms of Databases, what should that system do with these two classes? Put them into a single table letting field3=null for all rows of type "Abc"? or put them into two different tables? or put the common fields in one table and create a second one for the additional filed3?

Each solution has its advantages and disadvantages as you can read in the link i posted, then its up to your situation to decide which is the best way.

(Default I think is the joined strategy, however I would not count on that being so for every database)




回答2:


See

  • https://forum.hibernate.org/viewtopic.php?f=1&t=958277
  • HIbernate loads subclasses along with classes
  • https://forum.hibernate.org/viewtopic.php?f=1&t=971779
  • Retrieve only the superclass from a class hierarchy
  • http://www.yulebiao.com/questions/757812/hibernate-polymorphism-instantiating-the-right-class

It seems that you MUST have a separate abstract class. And there probably is no technical obstacle not to.



来源:https://stackoverflow.com/questions/4957228/extending-hibernate-entities-with-annotation

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