用注释扩展hibernate实体 [英] Extending hibernate entities with annotation

查看:126
本文介绍了用注释扩展hibernate实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要扩展一个实体,具有相同的特性,而不使用抽象类。



可以编码如下的代码吗?

  @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 ..
}



提前

解决方案

是的,这个工作没有任何问题。不过,您应该查看继承注释。



问题:你有一个表Abc,其中包含field1,filed2;然后你有ExAbc,其中包含Abc字段和adition字段3。
现在,如果您用数据库的方式来思考,那么这个系统应该对这两个类做什么?把它们放到一个表中让所有类型为Abc的行的field3 = null?或把它们放到两个不同的表中?或将公共领域放在一个表中,并为附加的领域创建第二个领域3?每个解决方案都有其优点和缺点,因为您可以在我发布的链接中阅读,那么它取决于你的情况来决定哪种方法是最好的。



(默认情况下,我认为是加入策略,但我不会指望每个数据库都这样)

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

解决方案

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)

这篇关于用注释扩展hibernate实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆