[^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. |
|