From 70408a543d0443a1c2c2e978629b02626f683985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Wed, 1 Jan 2025 17:41:42 +0100 Subject: [PATCH 1/3] HHH-18988 Adapted test case from Jira issue https://hibernate.atlassian.net/browse/HHH-18988 --- .../orm/test/columndiscriminator/Author.java | 45 +++++++++++++++++++ .../orm/test/columndiscriminator/Book.java | 34 ++++++++++++++ .../test/columndiscriminator/BookDetails.java | 23 ++++++++++ .../BoringBookDetails.java | 24 ++++++++++ .../ColumnDiscrimnatorWithSchemaTest.java | 37 +++++++++++++++ .../SpecialBookDetails.java | 24 ++++++++++ .../orm/test/columndiscriminator/orm.xml | 32 +++++++++++++ 7 files changed, 219 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Author.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Book.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BookDetails.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BoringBookDetails.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/SpecialBookDetails.java create mode 100644 hibernate-core/src/test/resources/org/hibernate/orm/test/columndiscriminator/orm.xml diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Author.java b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Author.java new file mode 100644 index 000000000000..a42591cad3e7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Author.java @@ -0,0 +1,45 @@ +/* + * 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.columndiscriminator; + +import java.util.*; + +public class Author { + private Long id; + private String name; + private String email; + private List books = new ArrayList<>(); + + public Author(String name, String email) { + this.name = name; + this.email = email; + } + + protected Author() { + // default + } + + public Long id() { + return id; + } + + public String name() { + return name; + } + + public String email() { + return email; + } + + public List books() { + return books; + } + + public void addBook(Book book) { + books.add(book); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Book.java b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Book.java new file mode 100644 index 000000000000..ca16510368e3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/Book.java @@ -0,0 +1,34 @@ +/* + * 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.columndiscriminator; + +public class Book { + private Long id; + private String title; + private BookDetails details; + + public Book(String title, BookDetails details) { + this.title = title; + this.details = details; + } + + protected Book() { + // default + } + + public Long id() { + return id; + } + + public String title() { + return title; + } + + public BookDetails details() { + return details; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BookDetails.java b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BookDetails.java new file mode 100644 index 000000000000..77f8b389c71f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BookDetails.java @@ -0,0 +1,23 @@ +/* + * 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.columndiscriminator; + +public abstract class BookDetails { + private String information; + + protected BookDetails(String information) { + this.information = information; + } + + protected BookDetails() { + // default + } + + public String information() { + return information; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BoringBookDetails.java b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BoringBookDetails.java new file mode 100644 index 000000000000..235dba94696b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/BoringBookDetails.java @@ -0,0 +1,24 @@ +/* + * 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.columndiscriminator; + +public class BoringBookDetails extends BookDetails { + private String boringInformation; + + public BoringBookDetails(String information, String boringInformation) { + super(information); + this.boringInformation = boringInformation; + } + + public BoringBookDetails() { + // default + } + + public String boringInformation() { + return boringInformation; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java new file mode 100644 index 000000000000..014d4d3d47a3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java @@ -0,0 +1,37 @@ +/* + * 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.columndiscriminator; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.SchemaToolingSettings; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +@DomainModel(xmlMappings = "org/hibernate/orm/test/columndiscriminator/orm.xml") +@ServiceRegistry(settings = { + @Setting(name = AvailableSettings.DEFAULT_SCHEMA, value = "GREET"), + @Setting(name = SchemaToolingSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS, value = "true") +}) +@SessionFactory +class ColumnDiscrimnatorWithSchemaTest { + + @Test + void testIt(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { + var book = new Book( "The Art of Computer Programming", + new SpecialBookDetails( "Hardcover", "Computer Science" ) ); + + var author = new Author( "Donald Knuth", "dn@cs.com" ); + author.addBook( book ); + entityManager.persist( author ); + } ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/SpecialBookDetails.java b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/SpecialBookDetails.java new file mode 100644 index 000000000000..5cc837e63ebf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/SpecialBookDetails.java @@ -0,0 +1,24 @@ +/* + * 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.columndiscriminator; + +public class SpecialBookDetails extends BookDetails { + private String specialInformation; + + public SpecialBookDetails(String information, String specialInformation) { + super(information); + this.specialInformation = specialInformation; + } + + protected SpecialBookDetails() { + // default + } + + public String specialInformation() { + return specialInformation; + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/columndiscriminator/orm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/columndiscriminator/orm.xml new file mode 100644 index 000000000000..02e59602ee07 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/columndiscriminator/orm.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 4b12e4eb0660f2103efd1d390f925d033bd0ba5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Thu, 2 Jan 2025 09:35:21 +0100 Subject: [PATCH 2/3] HHH-18988 Table name constructor parameter to ExplicitColumnDiscriminatorMappingImpl should be qualified --- .../metamodel/mapping/internal/EmbeddableMappingTypeImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddableMappingTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddableMappingTypeImpl.java index 3c0af66c2580..5e9b280e2ca6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddableMappingTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddableMappingTypeImpl.java @@ -764,7 +764,7 @@ private EmbeddableDiscriminatorMapping generateDiscriminatorMapping( return new ExplicitColumnDiscriminatorMappingImpl( this, name, - bootDescriptor.getTable().getName(), + bootDescriptor.getTable().getQualifiedName( creationContext.getSqlStringGenerationContext() ), discriminatorColumnExpression, isFormula, !isFormula, From 7961072682a21bf80e8fcd322777d5abfb121f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Thu, 2 Jan 2025 13:37:54 +0100 Subject: [PATCH 3/3] HHH-18988 Skip test if dialect does not supports schema creation --- .../ColumnDiscrimnatorWithSchemaTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java index 014d4d3d47a3..d8f2d3384c2b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/columndiscriminator/ColumnDiscrimnatorWithSchemaTest.java @@ -8,7 +8,9 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.SchemaToolingSettings; +import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportSchemaCreation; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; import org.hibernate.testing.orm.junit.ServiceRegistry; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; @@ -21,8 +23,16 @@ @Setting(name = SchemaToolingSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS, value = "true") }) @SessionFactory +@RequiresDialectFeature(feature = SupportSchemaCreation.class) class ColumnDiscrimnatorWithSchemaTest { + private ColumnDiscrimnatorWithSchemaTest() { + } + + static ColumnDiscrimnatorWithSchemaTest createColumnDiscrimnatorWithSchemaTest() { + return new ColumnDiscrimnatorWithSchemaTest(); + } + @Test void testIt(SessionFactoryScope scope) { scope.inTransaction( entityManager -> {