From 86799d6c28d56bdfdf6a9c629ea281302ff817d3 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 3 Dec 2021 15:22:15 -0600 Subject: [PATCH] HHH-14949 - Support @Access on @ElementCollection (for embeddable elements) Test --- .../mapping/access/EmbeddableAccessTests.java | 80 +++++++++++++++++++ .../orm/test/mapping/access/Name.java | 42 ++++++++++ .../orm/test/mapping/access/Person.java | 79 ++++++++++++++++++ .../orm/test/mapping/access/package-info.java | 11 +++ 4 files changed, 212 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/EmbeddableAccessTests.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Name.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Person.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/package-info.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/EmbeddableAccessTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/EmbeddableAccessTests.java new file mode 100644 index 0000000000..34c6c8ed88 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/EmbeddableAccessTests.java @@ -0,0 +1,80 @@ +/* + * 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.mapping.access; + +import org.hibernate.mapping.Collection; +import org.hibernate.mapping.Component; +import org.hibernate.mapping.Property; +import org.hibernate.metamodel.RuntimeMetamodels; +import org.hibernate.metamodel.mapping.AttributeMapping; +import org.hibernate.metamodel.mapping.EmbeddableMappingType; +import org.hibernate.metamodel.mapping.EntityMappingType; +import org.hibernate.metamodel.mapping.PluralAttributeMapping; +import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping; +import org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.DomainModelScope; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Steve Ebersole + */ +@DomainModel( annotatedClasses = { Person.class, Name.class } ) +@SessionFactory +public class EmbeddableAccessTests { + @Test + public void verifyBootModel(DomainModelScope scope) { + scope.withHierarchy( Person.class, (personDescriptor) -> { + final Property nameProperty = personDescriptor.getProperty( "name" ); + + final Component nameMapping = (Component) nameProperty.getValue(); + assertThat( nameMapping.getPropertySpan() ).isEqualTo( 2 ); + final Property nameFirst = nameMapping.getProperty( 0 ); + final Property nameLast = nameMapping.getProperty( 1 ); + assertThat( nameFirst.getName() ).isEqualTo( "firstName" ); + assertThat( nameLast.getName() ).isEqualTo( "lastName" ); + + final Property aliasesProperty = personDescriptor.getProperty( "aliases" ); + final Component aliasMapping = (Component) ( (Collection) aliasesProperty.getValue() ).getElement(); + assertThat( aliasMapping.getPropertySpan() ).isEqualTo( 2 ); + final Property aliasFirst = aliasMapping.getProperty( 0 ); + final Property aliasLast = aliasMapping.getProperty( 1 ); + assertThat( aliasFirst.getName() ).isEqualTo( "firstName" ); + assertThat( aliasLast.getName() ).isEqualTo( "lastName" ); + } ); + } + + @Test + public void verifyRuntimeModel(SessionFactoryScope scope) { + final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels(); + final EntityMappingType personDescriptor = runtimeMetamodels.getEntityMappingType( Person.class ); + + // Person defines FIELD access, while Name uses PROPERTY + // - if we find the property annotations, the attribute names will be + // `firstName` and `lastName`, and the columns `first_name` and `last_name` + // - otherwise, we have property and column names being `first` and `last` + final EmbeddableMappingType nameEmbeddable = ( (EmbeddedAttributeMapping) personDescriptor.findAttributeMapping( "name" ) ).getEmbeddableTypeDescriptor(); + assertThat( nameEmbeddable.getNumberOfAttributeMappings() ).isEqualTo( 2 ); + final AttributeMapping nameFirst = nameEmbeddable.getAttributeMapping( 0 ); + final AttributeMapping nameLast = nameEmbeddable.getAttributeMapping( 1 ); + assertThat( nameFirst.getAttributeName() ).isEqualTo( "firstName" ); + assertThat( nameLast.getAttributeName() ).isEqualTo( "lastName" ); + + final PluralAttributeMapping aliasesAttribute = (PluralAttributeMapping) personDescriptor.findAttributeMapping( "aliases" ); + final EmbeddableMappingType aliasEmbeddable = ( (EmbeddedCollectionPart) aliasesAttribute.getElementDescriptor() ).getEmbeddableTypeDescriptor(); + assertThat( aliasEmbeddable.getNumberOfAttributeMappings() ).isEqualTo( 2 ); + final AttributeMapping aliasFirst = nameEmbeddable.getAttributeMapping( 0 ); + final AttributeMapping aliasLast = nameEmbeddable.getAttributeMapping( 1 ); + assertThat( aliasFirst.getAttributeName() ).isEqualTo( "firstName" ); + assertThat( aliasLast.getAttributeName() ).isEqualTo( "lastName" ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Name.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Name.java new file mode 100644 index 0000000000..1faf2fe9ef --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Name.java @@ -0,0 +1,42 @@ +/* + * 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.mapping.access; + +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; + +@Embeddable +public class Name { + private String first; + private String last; + + private Name() { + } + + public Name(String first, String last) { + this.first = first; + this.last = last; + } + + @Column(name = "first_name") + public String getFirstName() { + return first; + } + + public void setFirstName(String first) { + this.first = first; + } + + @Column(name = "last_name") + public String getLastName() { + return last; + } + + public void setLastName(String last) { + this.last = last; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Person.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Person.java new file mode 100644 index 0000000000..0aa3cd4dce --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/Person.java @@ -0,0 +1,79 @@ +/* + * 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.mapping.access; /** + * @author Steve Ebersole + */ + +import java.util.HashSet; +import java.util.Set; + +import jakarta.persistence.Access; +import jakarta.persistence.AccessType; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +import static jakarta.persistence.AccessType.*; + +@Entity +@Table(name = "persons") +public class Person { + @Id + public Integer id; + + @Embedded + @Access( PROPERTY ) + public Name name; + + @ElementCollection + @Embedded + @Access( PROPERTY ) + public Set aliases; + + private Person() { + // for Hibernate use + } + + public Person(Integer id, Name name) { + this.id = id; + this.name = name; + } + + public Integer getId() { + return id; + } + + public Name getName() { + return name; + } + + public void setName(Name name) { + this.name = name; + } + + public void setId(Integer id) { + this.id = id; + } + + public Set getAliases() { + return aliases; + } + + public void setAliases(Set aliases) { + this.aliases = aliases; + } + + public void addAlias(Name alias) { + if ( aliases == null ) { + aliases = new HashSet<>(); + } + aliases.add( alias ); + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/package-info.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/package-info.java new file mode 100644 index 0000000000..9cf7154e74 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/access/package-info.java @@ -0,0 +1,11 @@ +/* + * 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 + */ + +/** + * Test support for {@link jakarta.persistence.Access} / {@link jakarta.persistence.AccessType} + */ +package org.hibernate.orm.test.mapping.access; \ No newline at end of file