HHH-14949 - Support @Access on @ElementCollection (for embeddable elements)
Test
This commit is contained in:
parent
2d064974fd
commit
86799d6c28
|
@ -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" );
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<Name> 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<Name> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public void setAliases(Set<Name> aliases) {
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
public void addAlias(Name alias) {
|
||||
if ( aliases == null ) {
|
||||
aliases = new HashSet<>();
|
||||
}
|
||||
aliases.add( alias );
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
Loading…
Reference in New Issue