Hibernate关联中对于mappedby 继承属性的描述和解决方法

本文探讨了使用Hibernate框架时遇到的关联映射问题,特别是关于继承结构中的一对多关联映射,涉及到MappedSuperclass的使用及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    近期使用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;
	}
}

    如上文所说,父类(图像)表示一个抽象的图像概念,其下分为具体的照片图像和附件图像,而人员信息同时具体照片图像列表和附件资料列表。一般来说,这个继承结构是正确的,但是这种引用关系并不被Hibernate所接受,在程序运行中,Yx类会提示一个找不到所mappBy的属性(yx)的错误,即Hibernate不会找到父类上对于Yx的引用设置。

    这个问题,在Hibernate的官方论坛上(07年回复)中找到这样一个回复,随附如下:

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了,虽然属性写得有点重复,但在这种情况下,好像还没办法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值