From 821d85b9a3bdd1bbbbe0f1da0cecd8d404fd181a Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 9 Jul 2021 11:34:35 +0200 Subject: [PATCH] Implemented EmbeddableMappingType and EmbeddedIdentifierMappingImpl applySqlSelections methods --- .../mapping/EmbeddableMappingType.java | 23 ++++++- .../EmbeddedIdentifierMappingImpl.java | 21 +++++++ .../{ => orm}/test/refresh/Customer.hbm.xml | 0 .../{ => orm}/test/refresh/Customer.java | 0 ...reshDetachedInstanceWhenIsAllowedTest.java | 54 ++++++++++++++++ ...hDetachedInstanceWhenIsNotAllowedTest.java | 62 +++++++++++++++++++ .../refresh/RefreshUsingEntityNameTest.java | 52 ++++++++++++++++ .../{ => orm}/test/refresh/TestEntity.java | 0 ...reshDetachedInstanceWhenIsAllowedTest.java | 44 ------------- ...hDetachedInstanceWhenIsNotAllowedTest.java | 49 --------------- .../refresh/RefreshUsingEntityNameTest.java | 46 -------------- .../internal/AbstractSchemaMigratorTest.java | 5 +- 12 files changed, 214 insertions(+), 142 deletions(-) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/refresh/Customer.hbm.xml (100%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/refresh/Customer.java (100%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshUsingEntityNameTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/refresh/TestEntity.java (100%) delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshUsingEntityNameTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java index 2f35f76e43..1f74976b92 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -48,6 +49,7 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.property.access.spi.PropertyAccess; import org.hibernate.query.NavigablePath; import org.hibernate.sql.ast.Clause; +import org.hibernate.sql.ast.spi.SqlSelection; import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.results.graph.DomainResult; import org.hibernate.sql.results.graph.DomainResultCreationState; @@ -555,7 +557,26 @@ public class EmbeddableMappingType implements ManagedMappingType, SelectableMapp NavigablePath navigablePath, TableGroup tableGroup, DomainResultCreationState creationState) { - throw new NotYetImplementedFor6Exception( getClass() ); + visitAttributeMappings( + attributeMapping -> attributeMapping.applySqlSelections( navigablePath, tableGroup, creationState ) + ); + } + + @Override + public void applySqlSelections( + NavigablePath navigablePath, + TableGroup tableGroup, + DomainResultCreationState creationState, + BiConsumer selectionConsumer) { + visitAttributeMappings( + attributeMapping -> + ManagedMappingType.super.applySqlSelections( + navigablePath, + tableGroup, + creationState, + selectionConsumer + ) + ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedIdentifierMappingImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedIdentifierMappingImpl.java index d9eb977742..4dd51cfaac 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedIdentifierMappingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedIdentifierMappingImpl.java @@ -7,17 +7,23 @@ package org.hibernate.metamodel.mapping.internal; import java.util.List; +import java.util.function.BiConsumer; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.metamodel.internal.AbstractCompositeIdentifierMapping; import org.hibernate.metamodel.mapping.EmbeddableMappingType; import org.hibernate.metamodel.mapping.EntityMappingType; +import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.SingularAttributeMapping; import org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess; import org.hibernate.property.access.spi.PropertyAccess; import org.hibernate.proxy.HibernateProxy; +import org.hibernate.query.NavigablePath; import org.hibernate.sql.ast.Clause; +import org.hibernate.sql.ast.spi.SqlSelection; +import org.hibernate.sql.ast.tree.from.TableGroup; +import org.hibernate.sql.results.graph.DomainResultCreationState; /** * Support for {@link javax.persistence.EmbeddedId} @@ -56,6 +62,21 @@ public class EmbeddedIdentifierMappingImpl return name; } + @Override + public void applySqlSelections( + NavigablePath navigablePath, TableGroup tableGroup, DomainResultCreationState creationState) { + getEmbeddableTypeDescriptor().applySqlSelections( navigablePath, tableGroup, creationState ); + } + + @Override + public void applySqlSelections( + NavigablePath navigablePath, + TableGroup tableGroup, + DomainResultCreationState creationState, + BiConsumer selectionConsumer) { + getEmbeddableTypeDescriptor().applySqlSelections( navigablePath, tableGroup, creationState, selectionConsumer ); + } + @Override public Object getIdentifier(Object entity, SharedSessionContractImplementor session) { if ( entity instanceof HibernateProxy ) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/refresh/Customer.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/Customer.hbm.xml similarity index 100% rename from hibernate-core/src/test/java/org/hibernate/test/refresh/Customer.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/refresh/Customer.hbm.xml diff --git a/hibernate-core/src/test/java/org/hibernate/test/refresh/Customer.java b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/Customer.java similarity index 100% rename from hibernate-core/src/test/java/org/hibernate/test/refresh/Customer.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/refresh/Customer.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java new file mode 100644 index 0000000000..929db9f4ec --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java @@ -0,0 +1,54 @@ +/* + * 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 . + */ +package org.hibernate.test.refresh; + +import org.hibernate.orm.test.jpa.refresh.TestEntity; + +import org.hibernate.testing.TestForIssue; +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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * @author Andrea Boriero + */ +@TestForIssue(jiraKey = "HHH-11188") +@DomainModel( + annotatedClasses = TestEntity.class +) +@SessionFactory +public class RefreshDetachedInstanceWhenIsAllowedTest { + private TestEntity testEntity; + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + testEntity = new TestEntity(); + scope.inTransaction( + session -> + session.save( testEntity ) + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from TestEntity" ).executeUpdate() + ); + } + + @Test + public void testRefreshDetachedInstance(SessionFactoryScope scope) { + scope.inSession( + session -> + session.refresh( testEntity ) + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java new file mode 100644 index 0000000000..5a64e8ae31 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java @@ -0,0 +1,62 @@ +/* + * 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 . + */ +package org.hibernate.test.refresh; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.orm.test.jpa.refresh.TestEntity; + +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.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * @author Andrea Boriero + */ +@DomainModel( + annotatedClasses = TestEntity.class +) +@SessionFactory +@ServiceRegistry( + settings = @Setting(name = AvailableSettings.ALLOW_REFRESH_DETACHED_ENTITY, value = "false") +) +public class RefreshDetachedInstanceWhenIsNotAllowedTest { + private TestEntity testEntity; + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + testEntity = new TestEntity(); + scope.inTransaction( + session -> + session.save( testEntity ) + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from TestEntity" ).executeUpdate() + ); + } + + @Test + public void testRefreshDetachedInstance(SessionFactoryScope scope) { + scope.inSession( + session -> + Assertions.assertThrows( + IllegalArgumentException.class, () -> + session.refresh( testEntity ) + ) + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshUsingEntityNameTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshUsingEntityNameTest.java new file mode 100644 index 0000000000..00f89ce79a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshUsingEntityNameTest.java @@ -0,0 +1,52 @@ +/* + * 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 . + */ +package org.hibernate.test.refresh; + +import org.hibernate.testing.TestForIssue; +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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * @author Andrea Boriero + */ +@TestForIssue(jiraKey = "HHH-11217") +@DomainModel( + xmlMappings = "org/hibernate/test/refresh/Customer.hbm.xml" +) +@SessionFactory +public class RefreshUsingEntityNameTest { + private Customer customer; + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + customer = new Customer(); + scope.inTransaction( + session -> + session.save( "CustomName", customer ) + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from CustomName" ).executeUpdate() + ); + } + + @Test + public void testRefreshUsingEntityName(SessionFactoryScope scope) { + scope.inSession( + session -> + session.refresh( "CustomName", customer ) + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/refresh/TestEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/TestEntity.java similarity index 100% rename from hibernate-core/src/test/java/org/hibernate/test/refresh/TestEntity.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/refresh/TestEntity.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java b/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java deleted file mode 100644 index 4d04202a1d..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsAllowedTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.refresh; - -import org.hibernate.Session; -import org.hibernate.orm.test.jpa.refresh.TestEntity; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; - -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; - -/** - * @author Andrea Boriero - */ -@TestForIssue(jiraKey = "HHH-11188") -public class RefreshDetachedInstanceWhenIsAllowedTest extends BaseCoreFunctionalTestCase { - private TestEntity testEntity; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] {TestEntity.class}; - } - - @Before - public void setUp() { - testEntity = new TestEntity(); - doInHibernate( this::sessionFactory, session -> { - session.save( testEntity ); - } ); - } - - @Test - public void testRefreshDetachedInstance() { - final Session session = openSession(); - session.refresh( testEntity ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java b/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java deleted file mode 100644 index 6694f0bedb..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshDetachedInstanceWhenIsNotAllowedTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.refresh; - -import org.hibernate.Session; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.orm.test.jpa.refresh.TestEntity; - -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; - -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; - -/** - * @author Andrea Boriero - */ -public class RefreshDetachedInstanceWhenIsNotAllowedTest extends BaseCoreFunctionalTestCase { - private TestEntity testEntity; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] {TestEntity.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.ALLOW_REFRESH_DETACHED_ENTITY, "false" ); - } - - @Before - public void setUp() { - testEntity = new TestEntity(); - doInHibernate( this::sessionFactory, session -> { - session.save( testEntity ); - } ); - } - - @Test(expected = IllegalArgumentException.class) - public void testRefreshDetachedInstance() { - final Session session = openSession(); - session.refresh( testEntity ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshUsingEntityNameTest.java b/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshUsingEntityNameTest.java deleted file mode 100644 index 33351268dd..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/refresh/RefreshUsingEntityNameTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.refresh; - -import org.hibernate.Session; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; - -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; - -/** - * @author Andrea Boriero - */ -@TestForIssue(jiraKey = "HHH-11217") -public class RefreshUsingEntityNameTest extends BaseCoreFunctionalTestCase { - private Customer customer; - - @Override - public String[] getMappings() { - return new String[] {"refresh/Customer.hbm.xml"}; - } - - - @Before - public void setUp() { - customer = new Customer(); - doInHibernate( this::sessionFactory, session -> { - session.save( "CustomName", customer ); - } ); - } - - - @Test - public void testRefreshUsingEntityName() { - try (final Session session = openSession();) { - session.refresh( "CustomName", customer ); - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/tool/schema/internal/AbstractSchemaMigratorTest.java b/hibernate-core/src/test/java/org/hibernate/tool/schema/internal/AbstractSchemaMigratorTest.java index a817a40526..129b8b16f0 100644 --- a/hibernate-core/src/test/java/org/hibernate/tool/schema/internal/AbstractSchemaMigratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/tool/schema/internal/AbstractSchemaMigratorTest.java @@ -26,12 +26,13 @@ import org.hibernate.tool.schema.extract.spi.TableInformation; import org.hibernate.tool.schema.internal.exec.GenerationTarget; import org.hibernate.tool.schema.spi.ContributableMatcher; import org.hibernate.tool.schema.spi.ExecutionOptions; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import static java.util.Collections.singletonList; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.hibernate.boot.model.naming.Identifier.toIdentifier; -import static org.junit.Assert.assertThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock;