prep work for to-one mapping support

This commit is contained in:
Steve Ebersole 2019-10-16 10:04:28 -05:00
parent 63b22c4c6b
commit 4d32d3d763
4 changed files with 144 additions and 1 deletions

View File

@ -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 http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.metamodel.mapping.internal;
import org.hibernate.LockMode;
import org.hibernate.engine.FetchStrategy;
import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.EntityValuedModelPart;
import org.hibernate.metamodel.mapping.ManagedMappingType;
import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.results.spi.DomainResultCreationState;
import org.hibernate.sql.results.spi.Fetch;
import org.hibernate.sql.results.spi.FetchParent;
/**
* @author Steve Ebersole
*/
public class SingularAssociationAttributeMapping extends AbstractSingularAttributeMapping implements EntityValuedModelPart {
public SingularAssociationAttributeMapping(
String name,
int stateArrayPosition,
StateArrayContributorMetadataAccess attributeMetadataAccess,
FetchStrategy mappedFetchStrategy,
MappingType type,
ManagedMappingType declaringType,
PropertyAccess propertyAccess) {
super(
name,
stateArrayPosition,
attributeMetadataAccess,
mappedFetchStrategy,
type,
declaringType,
propertyAccess
);
}
@Override
public EntityMappingType getEntityMappingType() {
return null;
}
@Override
public Fetch generateFetch(
FetchParent fetchParent,
NavigablePath fetchablePath,
FetchTiming fetchTiming,
boolean selected,
LockMode lockMode,
String resultVariable,
DomainResultCreationState creationState) {
return null;
}
}

View File

@ -21,18 +21,22 @@ import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.internal.BasicValuedSingularAttributeMapping;
import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;
import org.hibernate.metamodel.mapping.internal.SingularAssociationAttributeMapping;
import org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter;
import org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.hamcrest.CollectionMatchers;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
@ -93,11 +97,64 @@ public class SmokeTests {
}
}
@Test
@FailureExpected
public void testEntityBasedManyToOne(SessionFactoryScope scope) {
final EntityPersister entityDescriptor = scope.getSessionFactory()
.getDomainModel()
.getEntityDescriptor( OtherEntity.class );
final ModelPart part = entityDescriptor.findSubPart( "simpleEntity" );
assertThat( part, notNullValue() );
assertThat( part, instanceOf( SingularAssociationAttributeMapping.class ) );
final SingularAssociationAttributeMapping attrMapping = (SingularAssociationAttributeMapping) part;
// assertThat( attrMapping.getContainingTableExpression(), is( "mapping_simple_entity" ) );
// assertThat( attrMapping.getMappedColumnExpressions(), CollectionMatchers.hasSize( 4 ) );
// assertThat( attrMapping.getMappedColumnExpressions().get( 0 ), is( "attribute1" ) );
// assertThat( attrMapping.getMappedColumnExpressions().get( 1 ), is( "attribute2" ) );
}
public enum Gender {
MALE,
FEMALE
}
@Entity( name = "OtherEntity" )
@Table( name = "mapping_other_entity" )
@SuppressWarnings("unused")
public static class OtherEntity {
private Integer id;
private String name;
private SimpleEntity simpleEntity;
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
public SimpleEntity getSimpleEntity() {
return simpleEntity;
}
public void setSimpleEntity(SimpleEntity simpleEntity) {
this.simpleEntity = simpleEntity;
}
}
@Entity( name = "SimpleEntity" )
@Table( name = "mapping_simple_entity" )
@SuppressWarnings("unused")

View File

@ -18,6 +18,7 @@ import org.hibernate.query.sqm.tree.expression.SqmExpression;
import org.hibernate.query.sqm.tree.from.SqmRoot;
import org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelectableNode;
import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.junit.jupiter.api.Test;
@ -102,4 +103,15 @@ public class AttributePathTests extends BaseSqmUnitTest {
interpretSelect( "select s.mate from Person s where s.{id} = ?1" );
interpretSelect( "select s.mate from Person s where s.pk = ?1" );
}
@Test
public void testManyToOneReference() {
final SqmSelectStatement sqm = interpretSelect( "select s.mate from Person s" );
final List<SqmSelection> selections = sqm.getQuerySpec().getSelectClause().getSelections();
assertThat( selections.size(), is( 1 ) );
final SqmSelection selection = selections.get( 0 );
final SqmSelectableNode selectableNode = selection.getSelectableNode();
assert Person.class.equals( selectableNode.getJavaTypeDescriptor().getJavaType() );
}
}

View File

@ -13,10 +13,12 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.orm.test.metamodel.mapping.SmokeTests.Component;
import org.hibernate.orm.test.metamodel.mapping.SmokeTests.Gender;
import org.hibernate.orm.test.metamodel.mapping.SmokeTests.SimpleEntity;
import org.hibernate.orm.test.metamodel.mapping.SmokeTests.OtherEntity;
import org.hibernate.query.Query;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -37,7 +39,7 @@ import static org.hibernate.orm.test.metamodel.mapping.SmokeTests.Gender.MALE;
*/
@SuppressWarnings("WeakerAccess")
@DomainModel(
annotatedClasses = SimpleEntity.class,
annotatedClasses = {SimpleEntity.class, OtherEntity.class},
extraQueryImportClasses = {
SmokeTests.ListItemDto.class,
SmokeTests.CategorizedListItemDto.class,
@ -275,6 +277,16 @@ public class SmokeTests {
);
}
@Test
@FailureExpected
public void testSelectManyToOne(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "select e.simpleEntity from OtherEntity e" ).list();
}
);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Dynamic instantiations