Polymorphic'get'使用hibernate,多对一,InheritanceType.JOINED [英] Polymorphic 'get' using hibernate, many to one, InheritanceType.JOINED
问题描述
我有这样的事情。
一个汽车级别从多个座位有一个座位。座椅有一个子类LeatherSeat。
公共班车{
私人座椅;
...
@ManyToOne(fetch = FetchType.LAZY)
public Seat getSeat(){
return seat;
}
...
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Seat {
私人字符串ID;
私有字符串颜色;
}
@实体
公共类LeatherSeat扩展Seat {
private String leatherType;
}
当我创建我的汽车并将我的汽车座椅设置为LeatherSeat时,在分贝。当我想要获得我的汽车(使用标准或查询列表)并阅读getSeat()时,座椅总是座椅,从不是皮革座椅。我不能施放(例外)并且看起来必须通过id手动获取LeatherSeat。
这是使用JOINED继承类型的一个限制,还是我缺少一些东西。如何获得Seat作为LeatherSeat?
看起来,如果您进行懒取,就像您在使用getSeat ,你只会得到父母,而不会是子类。我用FetchType.EAGER和getSeat正确地返回了一个LeatherSeat。
我不确定为什么hibernate可以在EAGER获取时获得带有LeatherSeat的汽车,但是当进行LAZY获取时hibernate似乎无法得到它。好像有些东西在那里被破坏了。
在InheritanceType.JOINED中有一个关于鉴别器列的票据,其中关于这个场景提出了一个观点。 https://hibernate.onjira.com/browse/ANN-140 ,但票是拒绝,表明hibernate过于优雅,不需要为InheritanceType.JOINED鉴别器。然而,它无法正确地返回延迟提取的子类。
然后这张票 https://hibernate.onjira.com/browse/HHH-271?focusedCommentId=44089#comment-44089 更具体到这个问题,答案是'我们怎么想知道什么是子以获得懒惰的获取?'
这两张票都是旧的并被拒绝。对我来说似乎是一个问题。但是现在你不得不改变为不同的继承类型或使用渴望的获取类型,因为这是根据hibernate的设计。
I have something like this..
A Car class that has one Seat from many seats. Seat has a sub class LeatherSeat.
public class Car {
private Seat seat;
...
@ManyToOne(fetch = FetchType.LAZY)
public Seat getSeat() {
return seat;
}
...
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Seat {
private String id;
private String color;
}
@Entity
public class LeatherSeat extends Seat {
private String leatherType;
}
When I create my Car and make my Car's seat a LeatherSeat it saves all correctly on the db. When I want to then get my Car (using a Criteria or Query list) and I read getSeat() the Seat is always just a Seat, never a LeatherSeat. I cannot cast (exception) and seemingly must manually get the LeatherSeat by id.
Is this a limitation of using the JOINED inheritance type or am I missing something. How do I get Seat as a LeatherSeat?
It appears that if you do a lazy fetch, like you are doing with getSeat, you will only ever get the parent, never the sub-class. I tried this same example with FetchType.EAGER and getSeat correctly returns a LeatherSeat.
I am unsure why hibernate can get a Car with a LeatherSeat when for EAGER fetch but hibernate cannot seem to get it when do a LAZY fetch. Seems like something is broken there.
There is a ticket regarding discriminator columns on an InheritanceType.JOINED where a point is made regarding this scenario. https://hibernate.onjira.com/browse/ANN-140 but the ticket was rejected indicating that hibernate was too elegant for needing a discriminator for InheritanceType.JOINED. Yet it fails to correctly return sub-classes on lazy fetches.
Then this ticket https://hibernate.onjira.com/browse/HHH-271?focusedCommentId=44089#comment-44089 is more specific to this issue and the answer there was 'how are we suppose to know what sub-class to get on a lazy fetch?'
Both tickets are old and were rejected. Seems like a problem to me. But for now you would have to change to a different inheritance type or use eager fetch type as this is according to design by hibernate.
这篇关于Polymorphic'get'使用hibernate,多对一,InheritanceType.JOINED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!