|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.formula; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.Table; |
| 10 | +import org.hibernate.annotations.Formula; |
| 11 | +import org.hibernate.cfg.MappingSettings; |
| 12 | +import org.hibernate.dialect.H2Dialect; |
| 13 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 14 | +import org.hibernate.metamodel.mapping.AttributeMapping; |
| 15 | +import org.hibernate.metamodel.mapping.SelectableMapping; |
| 16 | +import org.hibernate.persister.entity.EntityPersister; |
| 17 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModelScope; |
| 19 | +import org.hibernate.testing.orm.junit.Jira; |
| 20 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 21 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 22 | +import org.hibernate.testing.orm.junit.Setting; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Steve Ebersole |
| 29 | + */ |
| 30 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-12227" ) |
| 31 | +@RequiresDialect( value = H2Dialect.class, comment = "Not dialect specific" ) |
| 32 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 33 | +public class SchemaSubstitutionTests { |
| 34 | + @Test |
| 35 | + @ServiceRegistry( settings = @Setting( name = MappingSettings.DEFAULT_SCHEMA, value = "my_schema" ) ) |
| 36 | + @DomainModel( annotatedClasses = Thing.class ) |
| 37 | + void testWithSchema(DomainModelScope modelScope) { |
| 38 | + try (SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) modelScope.getDomainModel().buildSessionFactory()) { |
| 39 | + final EntityPersister persister = sessionFactory.getMappingMetamodel().getEntityDescriptor( Thing.class ); |
| 40 | + final AttributeMapping attributeMapping = persister.findAttributeMapping( "externalName" ); |
| 41 | + verifyFormula( attributeMapping, true ); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @ServiceRegistry |
| 47 | + @DomainModel( annotatedClasses = Thing.class ) |
| 48 | + void testWithoutSchema(DomainModelScope modelScope) { |
| 49 | + try (SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) modelScope.getDomainModel().buildSessionFactory()) { |
| 50 | + final EntityPersister persister = sessionFactory.getMappingMetamodel().getEntityDescriptor( Thing.class ); |
| 51 | + final AttributeMapping attributeMapping = persister.findAttributeMapping( "externalName" ); |
| 52 | + verifyFormula( attributeMapping, false ); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void verifyFormula(AttributeMapping attributeMapping, boolean expectSchema) { |
| 57 | + final SelectableMapping selectable = attributeMapping.getSelectable( 0 ); |
| 58 | + assertThat( selectable.isFormula() ).isTrue(); |
| 59 | + assertThat( selectable.getSelectionExpression() ).doesNotContain( "{h-schema}" ); |
| 60 | + assertThat( selectable.getSelectionExpression().contains( "my_schema" ) ).isEqualTo( expectSchema ); |
| 61 | + } |
| 62 | + |
| 63 | + @Entity(name="Thing") |
| 64 | + @Table(name="things") |
| 65 | + public static class Thing { |
| 66 | + @Id |
| 67 | + private Integer id; |
| 68 | + private String name; |
| 69 | + @Formula( "select name from {h-schema}externals" ) |
| 70 | + private String externalName; |
| 71 | + } |
| 72 | +} |
0 commit comments