[英]Hibernate/JPA Mapping on abstract class
What would be the best practice to implement the following situation with Hibernate. 使用Hibernate实现以下情况的最佳实践是什么?
We define an abstract class that will form the base of any object that we want to persist in our database. 我们定义一个抽象类,它将构成我们想要在数据库中持久化的任何对象的基础。 It contains the id etc etc...
它包含id等等......
public abstract class ModelObject {
protected int id;
...
}
Now we subclass our base clase for special cases where more than one entity will have a similar fields. 现在我们为特殊情况子类化我们的基础分支,其中多个实体将具有相似的字段。
public abstract class ModelObjectWithNose extends ModelObject {
...
protected Nose nose;
}
Now, for all classes that we want to have a Nose
: 现在,对于我们想要拥有
Nose
所有课程:
public class Person extends ModelObjectWithNose { ... }
public class Animal extends ModelObjectWithNose { ... }
The real problem we have at the moment now is that this relationship needs to be bi-directional. 我们现在面临的真正问题是这种关系需要是双向的。 Each concrete class needs to know which
Nose
is theirs, but each Nose
also needs to know to what object it belongs. 每个具体的类都需要知道哪个是他们的
Nose
,但每个Nose
也需要知道它属于哪个对象。
public class Nose {
...
private ModelObjectWithNose owner;
}
For our example we need a @OneToOne
relationship, as each Person
can only have one Nose
and each Nose
can only belong to one Person
. 对于我们的示例,我们需要
@OneToOne
关系,因为每个Person
只能有一个Nose
,每个Nose
只能属于一个Person
。
What we have tried is doing the following: 我们尝试过的是做以下事情:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class ModelObjectWithNose extend ModelObject { ... }
and then: 接着:
@Entity
public class Person extends ModelObjectWithNose { ... }
...etc etc etc. 等等等
1) How do I define the relationship between the Nose
and the ModelObjectWithNose
? 1)如何定义
Nose
和ModelObjectWithNose
之间的关系? Is it as simple as: 它是如此简单:
@Entity
public class Nose {
...
// this causes: 'Basic' attribute should not be 'Persistence Entity'
ModelObjectWithNose owner;
}
2) Is there a better / more preferred way? 2)有更好/更优选的方式吗? Would the inheritance strategy work properly?
继承策略是否正常运作?
Thanks! 谢谢!
EDIT 编辑
So I have tried a few ways which all seem to potentially work. 所以我尝试了一些似乎都可行的方法。 Using any of the three Inheritance schemes works fine if you set up the annotations as in the question.
如果您在问题中设置注释,则使用三种继承方案中的任何一种都可以正常工作。 Unfortunately this has a huge impact on our existing database and code, so we opted to (for now) simply index the
Nose
object's id
field that is kept on our current objects that would have extended ModelObjectWithNose
and simply write helper methods to do point queries to find the entries. 不幸的是,这对我们现有的数据库和代码产生了巨大的影响,所以我们选择(现在)简单地索引
Nose
对象的id
字段,该字段保存在我们当前的对象上,这些对象将扩展ModelObjectWithNose
并简单地编写辅助方法来进行点查询找到条目。
Annotate your Nose
property in ModelObjectWithNose
as @OneToOne
and in the Nose
entity you do the same with ModelObjectWithNose owner
. 在
ModelObjectWithNose
您的Nose
属性注释为@OneToOne
并在Nose
实体中对ModelObjectWithNose owner
执行相同ModelObjectWithNose owner
。 Set the mappedBy
attribute of the annotation of the Nose
entity, so JPA knows on which table the FK should be inserted (In your model the ModelObjectWithNose
should have the FK to Nose
, not other way around. You set it on the inverse end of the one-to-one relationship, ie. the one that has the PK to use.) 设置
Nose
实体的注释的mappedBy
属性,因此JPA知道应该在哪个表上插入FK(在模型中, ModelObjectWithNose
应该具有FK到Nose
,而不是其他方式。你将它设置在反向末端一对一关系,即具有PK使用的关系。)
BTW your Entities must implement Serializable
(you can do so on the ModelObject
class to enforce it on inheriting classes). BTW您的实体必须实现
Serializable
(您可以在ModelObject
类上执行此操作以在继承类时强制执行)。 The id
property of ModelObject
should be annotated as @Id
. ModelObject
的id
属性应注释为@Id
。
Cheers, Kai 干杯,凯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.