From 998f2ef21f0510cc0e2c50132aed6804e0c2c96a Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 23 Jan 2023 16:59:35 +0100 Subject: [PATCH] HHH-15933 Better property owner check + new test case for JoinedSubclass --- .../boot/model/internal/BinderHelper.java | 7 +-- .../refcolnames/subclass/Animal.java | 46 +++++++++++++++++++ .../annotations/refcolnames/subclass/Cat.java | 36 +++++++++++++++ .../subclass/RefToJoinedSuperclassTest.java | 39 ++++++++++++++++ .../annotations/refcolnames/subclass/Toy.java | 29 ++++++++++++ 5 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Animal.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Cat.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/RefToJoinedSuperclassTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Toy.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java index 8fc1f7018e..a872856065 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java @@ -336,11 +336,12 @@ public class BinderHelper { result.setInsertable( false ); result.setValue( embeddedComponent ); result.setPropertyAccessorName( "embedded" ); - if ( ownerEntity instanceof JoinedSubclass ) { - ownerEntity.addProperty( result ); + if ( persistentClassOrJoin instanceof Join ) { + // the referenced column is in the joined table, add the synthetic property there + persistentClassOrJoin.addProperty( result ); } else { - persistentClassOrJoin.addProperty( result ); + ownerEntity.addProperty( result ); } embeddedComponent.createUniqueKey(); //make it unique return result; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Animal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Animal.java new file mode 100644 index 0000000000..bc728f0cda --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Animal.java @@ -0,0 +1,46 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.annotations.refcolnames.subclass; + +import org.hibernate.annotations.NaturalId; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; + +/** + * @author Marco Belladelli + */ +@Entity(name = "Animal") +@Inheritance(strategy = InheritanceType.JOINED) +public class Animal { + @Id + @GeneratedValue + private Long id; + + @Column(name = "name") + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Cat.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Cat.java new file mode 100644 index 0000000000..f3b101d5b8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Cat.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.annotations.refcolnames.subclass; + +import java.util.ArrayList; +import java.util.List; + +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; + +/** + * @author Marco Belladelli + */ +@Entity(name = "Cat") +public class Cat extends Animal { + @ManyToMany + @JoinTable( + name = "cat_toys", + joinColumns = @JoinColumn(name = "cat_name", referencedColumnName = "name") + ) + private List toys = new ArrayList<>(); + + public List getToys() { + return toys; + } + + public void setToys(List toys) { + this.toys = toys; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/RefToJoinedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/RefToJoinedSuperclassTest.java new file mode 100644 index 0000000000..5de3a52d78 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/RefToJoinedSuperclassTest.java @@ -0,0 +1,39 @@ + +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.annotations.refcolnames.subclass; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Marco Belladelli + */ +@SessionFactory +@DomainModel(annotatedClasses = { Animal.class, Cat.class, Toy.class }) +public class RefToJoinedSuperclassTest { + @Test + public void test(SessionFactoryScope scope) { + final Cat cat = new Cat(); + cat.setName( "cat" ); + final Toy toy = new Toy(); + cat.getToys().add( toy ); + scope.inTransaction( session -> { + session.persist( cat ); + session.persist( toy ); + } ); + scope.inSession( session -> { + Cat result = session.createQuery( "from Cat", Cat.class ).getSingleResult(); + assertEquals( 1, result.getToys().size() ); + assertEquals( toy.getId(), result.getToys().get( 0 ).getId() ); + } ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Toy.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Toy.java new file mode 100644 index 0000000000..c7bd7f789a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/refcolnames/subclass/Toy.java @@ -0,0 +1,29 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.annotations.refcolnames.subclass; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; + +/** + * @author Marco Belladelli + */ +@Entity(name = "Toy") +public class Toy { + @Id + @GeneratedValue + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +}