[hibernate-issues] [JIRA] (HHH-13982) Incorrect FK column generated/used when using @OneToMany with @Inheritance(strategy = InheritanceType.JOINED)

Василий Музыченко (JIRA) jira at hibernate.atlassian.net
Mon Apr 27 03:30:28 EDT 2020


Василий Музыченко ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5a72fc7a2d61371e861f231e ) *updated* an issue

Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjcyMTMwODZjODljNDA4MDhiNjM3YjlkYTgxZGY3YzMiLCJwIjoiaiJ9 ) / Bug ( https://hibernate.atlassian.net/browse/HHH-13982?atlOrigin=eyJpIjoiNjcyMTMwODZjODljNDA4MDhiNjM3YjlkYTgxZGY3YzMiLCJwIjoiaiJ9 ) HHH-13982 ( https://hibernate.atlassian.net/browse/HHH-13982?atlOrigin=eyJpIjoiNjcyMTMwODZjODljNDA4MDhiNjM3YjlkYTgxZGY3YzMiLCJwIjoiaiJ9 ) Incorrect FK column generated/used when using @OneToMany with @Inheritance(strategy = InheritanceType.JOINED) ( https://hibernate.atlassian.net/browse/HHH-13982?atlOrigin=eyJpIjoiNjcyMTMwODZjODljNDA4MDhiNjM3YjlkYTgxZGY3YzMiLCJwIjoiaiJ9 )

Change By: Василий Музыченко ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5a72fc7a2d61371e861f231e )

[^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=eyJpIjoiNjcyMTMwODZjODljNDA4MDhiNjM3YjlkYTgxZGY3YzMiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-13982#add-comment?atlOrigin=eyJpIjoiNjcyMTMwODZjODljNDA4MDhiNjM3YjlkYTgxZGY3YzMiLCJwIjoiaiJ9 )

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.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100125- sha1:c966059 )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200427/3b9245d6/attachment.html 


More information about the hibernate-issues mailing list