Василий Музыченко (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5a72fc7...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjcyMTMwODZj...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-13982?atlOrigin=eyJpIjoiNjcyMT...
) HHH-13982 (
https://hibernate.atlassian.net/browse/HHH-13982?atlOrigin=eyJpIjoiNjcyMT...
) Incorrect FK column generated/used when using @OneToMany with @Inheritance(strategy =
InheritanceType.JOINED) (
https://hibernate.atlassian.net/browse/HHH-13982?atlOrigin=eyJpIjoiNjcyMT...
)
Change By: Василий Музыченко (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5a72fc7...
)
[^hibernate-one-to-many-bug.zip]
Hi everyone. Here are some entities to demonstrate this (getters/setters omitted for
brevity):
Parent entity (container):
{code:java}
@Entity
public class Parent {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String uuid;
@OneToMany(mappedBy = "parent")
private Set<FirstChild> firstChildren;
}
{code}
Child (element) base class:
{code:java}
@Entity
@DiscriminatorColumn(name = "discriminator")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class ChildBase {
public static final String DISCRIMINATOR_FIRST = "FIRST";
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String uuid;
@ManyToOne
private Parent parent;
}
{code}
And element itself:
{code:java}
@Entity
@DiscriminatorValue(ChildBase.DISCRIMINATOR_FIRST)
public class FirstChild extends ChildBase {
private String firstChildField;
}
{code}
This results in the folowing statements being generated:
{code:java}
create table ChildBase (
discriminator varchar(31) not null,
uuid varchar(255) not null,
parent_uuid varchar(255),
primary key (uuid)
)
create table FirstChild (
firstChildField varchar(255),
uuid varchar(255) not null,
parent_uuid varchar(255),
primary key (uuid)
)
{code}
Note that ` * parent_uuid ` * column is present in both ` * ChildBase ` * and ` *
FirstChild ` *. Which to my understanding is incorrect, since ` * FirstChild ` * should
only contain columns, defined in ` * FirstChild ` * itself. Also, removing ` * @OneToMany
` * makes this additional column disappear.
Furthermore Hibernate will actually use correct column when persisting data, but incorrect
one when loading. Thus failing to find any rows. This behaviour can be seen it tests that
provided.
As a workaround you can use ` * targetEntity = ChildBase.class ` * on ` * @OneToMany ` *
but this results in ugly things and suboptimal SQL when working with this collection, so
it is far from ideal.
(
https://hibernate.atlassian.net/browse/HHH-13982#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-13982#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100125- sha1:c966059 )