Polymorphism ‘get’ uses hibernate, multi-one, inheritancetype.joined

I have something like this..

A car class, one seat with many seats. Seat has a subclass LeatherSeat.

public class Car {
private Seat seat;
...
@ManyToOne(fetch = FetchType.LAZY)
public Seat getSeat( ) {
return seat;
}
...
}

@Entity
@Inheritance(strategy=InheritanceType.JOINED)< br />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 seat a LeatherSeat, it is correctly saved on the database. When I want to get my Car (using Criteria or Query list) and when I read getSeat(), Seat is always just a seat, not a LeatherSeat. I can’t cast (exception), it seems that I have to get LeatherSeat manually by id.

Is this a limitation of using JOINED inheritance types or am I missing something. How to use Seat as LeatherSeat?

It seems that if you do a lazy extraction, like you are using getSeat, you Will only get the parent class, not the subclass. I tried the same example with FetchType.EAGER, and getSeat returns LeatherSeat correctly.

I’m not sure why hibernate can get the belt when getting EAGER There is a Car of LeatherSeat, but when performing LAZY acquisition, hibernate seems to be unable to acquire it. It seems that something is broken there.

There are tickets for the discriminator column on InheritanceType.JOINED, among them there are A point about this scenario. https://hibernate.onjira.com/browse/ANN-140 but the ticket was rejected, indicating that hibernate is too elegant and does not need to set a discriminator for InheritanceType.JOINED. However, it cannot be extracted in delay Return the subcategory correctly.

Then this ticket https://hibernate.onjira.com/browse/HHH-271?focusedCommentId=44089#comment-44089 is more specific to this question, there is the answer It’s “How do we want to know what subclass can get lazy extraction?”

These two tickets are old and rejected. It seems to be a problem to me. But now you have to change to Different inheritance types or use eager fetch type, because this is based on hibernate design.

I have something like this..

A car class has a seat with many seats. Seat has a subclass LeatherSeat.

public class Car {
private Seat seat;
.. .
@ManyToOne(fetch = FetchType.LAZY)
public Seat getSeat() {
return seat;
}
...
}

@Entity
@Inheritance(strategy=Inheritance Type.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 seat a LeatherSeat, it is correctly saved on the database. When I want to get my Car (using Criteria or Query list) and when I read getSeat(), Seat is always just a seat, not a LeatherSeat. I can’t cast (exception), it seems that I have to manually get LeatherSeat by id. < /p>

Is this a limitation of using JOINED inheritance types or am I missing something. How to use Seat as LeatherSeat?

It seems that if you do a lazy extraction, like you are using getSeat, you will only get the parent class, not the child class. I Tried the same example as FetchType.EAGER, and getSeat returns LeatherSeat correctly.

I’m not sure why hibernate can get Car with LeatherSeat when getting EAGER, but when performing LAZY get, hibernate doesn’t seem to be able to get it. It seems that something is broken there.

There is a ticket about the discriminator column on InheritanceType.JOINED, which has a point about this scenario. https://hibernate. onjira.com/browse/ANN-140 but the ticket was rejected, indicating that hibernate is too elegant and does not need to set a discriminator for InheritanceType.JOINED. However, it cannot return subclasses correctly on delayed extraction.

Then this ticket https://hibernate.onjira.com/browse/HHH-271?focusedCommentId=44089#comment-44089 is more specific to this question, where the answer is “How do we want to know what subcategory can get lazy Extraction?”

These two tickets are very old and rejected. It seems to be a problem to me. But now you have to change to a different inheritance type or use eager fetch type, because of this It is designed according to hibernate.

Leave a Comment

Your email address will not be published.