近期使用Hibernate进行开发,在开发的过程中碰到了几个问题,其中一些是由于不了解Hibernate,另外一些则属于设计或者Hibernate自身不支持一些操作。主要还是对Hibernate自身的一些东西都没有很好地了解,导致出了问题都往google上找,也不太知道其中的道理。现在把这些问题都列下来,以便以后容易查找。
1,Hibernate ANN-588 在关联引用中不能引用继承的属性。
这个问题举例如下:
父类(图像) public class Xa implements Serializable{ //其他属性 private Yx yx;//引用yx @ManyToOne public Yx getYx() { return yx; } } 子类A(照片) public class XsubA extends Xa{ } 子类B(附件资料) public class XsubB extends Xa{ } Yx类(人员信息),分别引用子类A和子类B public class Yx{ //其他属性 private List<XsubA> xsubAList; private List<XsubB> xsubBList; @OneToMany(mappedBy = "yx") public List<XsubA> getXsubAList() { return xsubAList; } @OneToMany(mappedBy = "yx") public List<XsubB> getXsubBList() { return xsubBList; } }
https://forums.hibernate.org/viewtopic.php?f=9&t=968594 写道
Emmanuel communicated the following to me, and I figured it made sense to post it here since it is directly relevant (hopefully he won't mind):
Quote:
I did not properly understood the forum case hence the "unless you're not explaining all the details" 🙂
What you can do could be possible (need to be tested) if the superclass was mapped with @MappedSuperclass, because in this case the FK is held by the entity class. But in your case, the FK is held by the superclass table, so from the relational POV, you have an association between the associated entity and the superclass, which is not what is described in your one to many association. I thought a lot about supporting this kind of case, but right now, I think it would be a bad thing to support: I like the consistency between the object model and the relational one.
Personally, I naturally assumed that you could map a relationship to a subclass using mappedBy on a superclass, even if the superclass is an entity. At least that's more like the way OO inheritance behaves. I recall changing to MappedSuperclass has other effects (at least w/ pure JPA), such loss of ability to do polymorphic queries. IMHO, seems it would be better to make the decision to go with MappedSuperclass versus Entity based on that kind of concern, not how you've set up the inheritance hierarchy. I can relate to wanting to keep things consistent, it's just that it seems like it should work as a few of us expected–so it takes time to figure out why it doesn't and how to work around it.
Instead of using MappedSuperclass, I chose to leave the superclass as an entity, make the accessors in the superclass abstract, and implement them in the subclass. Seems a bit duplicative, but it works and retains the consistency Emmanuel likes.
没仔细明白所说的意思,我想估计是认为,关系在一对多时引用的子类,但是在多这一方的关系是由父类来进行维护的,双方的关系没有一致性,虽然在oo设计中,这是一种常用的方式。
文件提到了用MappedSuperclass来支持这种定义,但MappedSuperclass并不满足我们现有的需要,因为它仅是一个属性的复制,其实的内容会分别存储到两个表中(即subA和subB各一张表,父类不再是实体),在最后提到了一种解决方法,即是在父类上设置一个抽象的引用方法,然后在子类中实现这个方法,但是子类的两个方法在Hibernate配置上是相同的。我把它简单修改了一个,不把父类方法作为抽象的,而是用了Transient来表示一个父类方法,在子类上再复写。如下所示:
//Xa父类 public class Xa implements Serializable{ protected Yx yx; @Transient public Yx getYx() { return yx; } } //子类A public class XsubA extends Xa{ @ManyToOne @JoinColumn(name = "yx_id") public Yx getYx() { return yx; } } //子类B public class XsubB extends Xa{ @ManyToOne @JoinColumn(name = "yx_id") public Yx getYx() { return yx; } }
这样反而就OK了,虽然属性写得有点重复,但在这种情况下,好像还没办法。
转载请标明出处:i flym
本文地址:https://www.iflym.com/index.php/code/hibernate-mappedby-inheritance-problem-describe-and-solve.html