re-enable tests

re-organize some tests
continuing with o.h.test.hql
fixed bug in JPA model building with dynamic entities
added NavigablePath#getUnaliasedLocalName to aid in resolving ModelParts as part of SQM->SQL
This commit is contained in:
Steve Ebersole 2021-04-01 11:07:28 -05:00
parent f1ede3df4f
commit 51074eb9a0
14 changed files with 239 additions and 35 deletions

View File

@ -386,7 +386,9 @@ public class MetadataContext {
final Component cidValue = (Component) persistentClass.getIdentifier();
final Iterator<Property> cidPropertyItr = cidValue.getPropertyIterator();
AbstractIdentifiableType idType = (AbstractIdentifiableType) entityTypes.get( cidValue.getOwner().getMappedClass() );
assert cidValue.isEmbedded();
AbstractIdentifiableType idType = (AbstractIdentifiableType) entityTypesByEntityName.get( cidValue.getOwner().getEntityName() );
Set idAttributes = idType.getIdClassAttributesSafely();
if ( idAttributes == null ) {
idAttributes = new HashSet<>( cidValue.getPropertySpan() );

View File

@ -157,8 +157,7 @@ public abstract class AbstractManagedType<J>
}
if ( getSuperType() != null ) {
attribute = getSuperType().findAttribute( name );
//noinspection RedundantIfStatement
attribute = getSuperType().findAttributeInSuperTypes( name );
if ( attribute != null ) {
return attribute;
}
@ -174,6 +173,19 @@ public abstract class AbstractManagedType<J>
return null;
}
@Override
public PersistentAttribute<? super J, ?> findAttributeInSuperTypes(String name) {
final PersistentAttribute<J, ?> local = findDeclaredAttribute( name );
if ( local != null ) {
return local;
}
if ( superType != null ) {
return superType.findAttributeInSuperTypes( name );
}
return null;
}
@Override
public PersistentAttribute<? super J, ?> findSubTypesAttribute(String name) {

View File

@ -53,6 +53,7 @@ public interface ManagedDomainType<J> extends SimpleDomainType<J>, ManagedType<J
PersistentAttribute<? super J,?> findAttribute(String name);
PersistentAttribute<? super J, ?> findSubTypesAttribute(String name);
PersistentAttribute<? super J, ?> findAttributeInSuperTypes(String name);
SingularPersistentAttribute<? super J,?> findSingularAttribute(String name);
PluralPersistentAttribute<? super J, ?,?> findPluralAttribute(String name);

View File

@ -19,6 +19,7 @@ public class NavigablePath implements DotIdentifierSequence {
private final NavigablePath parent;
private final String fullPath;
private final String unaliasedLocalName;
private final String identifierForTableGroup;
public NavigablePath(NavigablePath parent, String navigableName) {
@ -29,9 +30,11 @@ public class NavigablePath implements DotIdentifierSequence {
// various things such as criteria paths and fetch profile association paths
if ( IDENTIFIER_MAPPER_PROPERTY.equals( navigableName ) ) {
this.fullPath = parent != null ? parent.getFullPath() : "";
this.unaliasedLocalName = "";
this.identifierForTableGroup = parent != null ? parent.getIdentifierForTableGroup() : "";
}
else {
this.unaliasedLocalName = navigableName;
if ( parent != null ) {
final String parentFullPath = parent.getFullPath();
this.fullPath = StringHelper.isEmpty( parentFullPath )
@ -55,6 +58,7 @@ public class NavigablePath implements DotIdentifierSequence {
public NavigablePath(String rootName, String alias) {
this.parent = null;
this.fullPath = alias == null ? rootName : rootName + "(" + alias + ")";
this.unaliasedLocalName = StringHelper.unqualify( rootName );
identifierForTableGroup = rootName;
}
@ -70,9 +74,11 @@ public class NavigablePath implements DotIdentifierSequence {
// various things such as criteria paths and fetch profile association paths
if ( IDENTIFIER_MAPPER_PROPERTY.equals( navigableName ) ) {
this.fullPath = parent != null ? parent.getFullPath() : "";
this.unaliasedLocalName = "";
identifierForTableGroup = parent != null ? parent.getFullPath() : "";
}
else {
this.unaliasedLocalName = property;
if ( parent != null ) {
final String parentFullPath = parent.getFullPath();
this.fullPath = StringHelper.isEmpty( parentFullPath )
@ -109,6 +115,10 @@ public class NavigablePath implements DotIdentifierSequence {
return parent == null ? fullPath : StringHelper.unqualify( fullPath );
}
public String getUnaliasedLocalName() {
return unaliasedLocalName;
}
public String getFullPath() {
return fullPath;
}

View File

@ -78,7 +78,6 @@ import org.hibernate.query.QueryLogging;
import org.hibernate.query.SemanticException;
import org.hibernate.query.TemporalUnit;
import org.hibernate.query.UnaryArithmeticOperator;
import org.hibernate.query.internal.QueryHelper;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.spi.QueryParameterBinding;
import org.hibernate.query.spi.QueryParameterBindings;
@ -3628,9 +3627,16 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
@Override
public Expression visitPluralAttributeSizeFunction(SqmCollectionSize function) {
final SqmPath<?> pluralPath = function.getPluralPath();
final PluralAttributeMapping mappingModelExpressable = (PluralAttributeMapping) determineValueMapping(
pluralPath );
final FromClauseAccess parentFromClauseAccess = getFromClauseAccess();
final TableGroup parentTableGroup = getFromClauseAccess().getTableGroup( pluralPath.getNavigablePath().getParent() );
assert parentTableGroup != null;
final PluralAttributeMapping collectionPart = (PluralAttributeMapping) parentTableGroup.getModelPart().findSubPart(
pluralPath.getNavigablePath().getUnaliasedLocalName(),
null
);
assert collectionPart != null;
final QuerySpec subQuerySpec = new QuerySpec( false );
pushProcessingState(
new SqlAstQueryPartProcessingStateImpl(
@ -3641,7 +3647,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
)
);
try {
final TableGroup tableGroup = mappingModelExpressable.createRootTableGroup(
final TableGroup tableGroup = collectionPart.createRootTableGroup(
pluralPath.getNavigablePath(),
null,
true,
@ -3672,8 +3678,8 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
subQuerySpec.getSelectClause().addSqlSelection( new SqlSelectionImpl( 1, 0, expression ) );
subQuerySpec.applyPredicate(
mappingModelExpressable.getKeyDescriptor().generateJoinPredicate(
parentFromClauseAccess.findTableGroup( pluralPath.getNavigablePath().getParent() ),
collectionPart.getKeyDescriptor().generateJoinPredicate(
parentTableGroup,
tableGroup,
SqlAstJoinType.INNER,
getSqlExpressionResolver(),

View File

@ -163,6 +163,12 @@ public class SqmPolymorphicRootDescriptor<T> implements EntityDomainType<T> {
return commonAttributes.get( name );
}
@Override
public PersistentAttribute<? super T, ?> findAttributeInSuperTypes(String name) {
// there are effectively no super-types
return null;
}
@Override
public void visitAttributes(Consumer<PersistentAttribute<T, ?>> action) {
commonAttributes.values().forEach( (Consumer) action );

View File

@ -4,7 +4,7 @@
* 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.test.hql;
package org.hibernate.orm.test.any.hbm;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

View File

@ -4,7 +4,7 @@
* 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.test.hql;
package org.hibernate.orm.test.any.hbm;
/**

View File

@ -0,0 +1,62 @@
<?xml version="1.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 http://www.gnu.org/licenses/lgpl-2.1.html
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.orm.test.any.hbm">
<class name="PropertySet" table="T_PROP_SET">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="string"/>
<any name="someSpecificProperty" id-type="long" meta-type="string" cascade="all">
<meta-value value="I" class="IntegerPropertyValue"/>
<meta-value value="S" class="StringPropertyValue"/>
<meta-value value="C" class="ComplexPropertyValue" />
<column name="S_S_PROP_TYPE"/>
<column name="S_S_PROP_ID"/>
</any>
<map name="generalProperties" table="T_GEN_PROPS" lazy="true" cascade="all">
<key column="PROP_SET_ID"/>
<map-key type="string" column="GEN_PROP_NAME"/>
<many-to-any id-type="long" meta-type="string">
<meta-value value="I" class="IntegerPropertyValue"/>
<meta-value value="S" class="StringPropertyValue"/>
<column name="PROP_TYPE"/>
<column name="PROP_ID"/>
</many-to-any>
</map>
</class>
<class name="StringPropertyValue" table="T_CHAR_PROP">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="value" column="VAL" not-null="true" type="string"/>
</class>
<class name="IntegerPropertyValue" table="T_NUM_PROP">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="value" column="VAL" not-null="true" type="integer"/>
</class>
<class name="ComplexPropertyValue" table="T_COMPLEX_PROP">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<map name="subProperties" table="T_COMPLEX_SUB_PROPS" lazy="true">
<key column="PROP_ID" />
<map-key type="string" column="SUB_PROP_NAME" />
<element type="string" column="SUB_PROP_VAL" />
</map>
</class>
</hibernate-mapping>

View File

@ -4,7 +4,7 @@
* 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.test.hql;
package org.hibernate.orm.test.any.hbm;
import java.util.HashMap;
import java.util.Map;

View File

@ -4,7 +4,7 @@
* 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.test.hql;
package org.hibernate.orm.test.any.hbm;
/**

View File

@ -4,7 +4,7 @@
* 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.test.hql;
package org.hibernate.orm.test.any.hbm;
/**

View File

@ -53,12 +53,17 @@ import org.hibernate.loader.MultipleBagFetchException;
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
import org.hibernate.orm.test.any.hbm.IntegerPropertyValue;
import org.hibernate.orm.test.any.hbm.PropertySet;
import org.hibernate.orm.test.any.hbm.PropertyValue;
import org.hibernate.orm.test.any.hbm.StringPropertyValue;
import org.hibernate.query.Query;
import org.hibernate.query.SemanticException;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.query.sqm.SqmExpressable;
import org.hibernate.query.sqm.internal.QuerySqmImpl;
import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.expression.SqmFunction;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.hibernate.stat.QueryStatistics;
@ -77,6 +82,8 @@ import org.hibernate.orm.test.cid.LineItem;
import org.hibernate.orm.test.cid.LineItem.Id;
import org.hibernate.orm.test.cid.Order;
import org.hibernate.orm.test.cid.Product;
import org.junit.After;
import org.junit.Test;
import org.hamcrest.CoreMatchers;
@ -85,6 +92,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.notNullValue;
import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
@ -116,27 +124,109 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
private List<Long> createdAnimalIds = new ArrayList<>();
@After
public void cleanUpTestData() {
inTransaction(
(session) -> {
session.createQuery( "from Animal" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from User" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Zoo" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from StateProvince" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Joiner" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Foo" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from One" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Many" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from SimpleAssociatedEntity" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from SimpleEntityWithAssociation" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from HeresAnotherCrazyIdFieldName" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from MoreCrazyIdFieldNameStuffEntity" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Image" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from ComponentContainer" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from VariousKeywordPropertyEntity" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Constructor" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from ProductLine" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Model" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from LineItem" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Product" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Order" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Customer" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from PropertySet" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Commento" ).list().forEach(
(animal) -> session.delete( animal )
);
session.createQuery( "from Marelo" ).list().forEach(
(animal) -> session.delete( animal )
);
}
);
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
return false;
}
@Override
public String[] getMappings() {
return new String[] {
"hql/Animal.hbm.xml",
"hql/FooBarCopy.hbm.xml",
"hql/SimpleEntityWithAssociation.hbm.xml",
"hql/CrazyIdFieldNames.hbm.xml",
"hql/Image.hbm.xml",
"hql/ComponentContainer.hbm.xml",
"hql/VariousKeywordPropertyEntity.hbm.xml",
"hql/Constructor.hbm.xml",
"/org/hibernate/test/hql/Animal.hbm.xml",
"/org/hibernate/test/hql/FooBarCopy.hbm.xml",
"/org/hibernate/test/hql/SimpleEntityWithAssociation.hbm.xml",
"/org/hibernate/test/hql/CrazyIdFieldNames.hbm.xml",
"/org/hibernate/test/hql/Image.hbm.xml",
"/org/hibernate/test/hql/ComponentContainer.hbm.xml",
"/org/hibernate/test/hql/VariousKeywordPropertyEntity.hbm.xml",
"/org/hibernate/test/hql/Constructor.hbm.xml",
"batchfetch/ProductLine.hbm.xml",
"cid/Customer.hbm.xml",
"cid/Order.hbm.xml",
"cid/LineItem.hbm.xml",
"cid/Product.hbm.xml",
"any/Properties.hbm.xml",
"/org/hibernate/orm/test/cid/Customer.hbm.xml",
"/org/hibernate/orm/test/cid/Order.hbm.xml",
"/org/hibernate/orm/test/cid/LineItem.hbm.xml",
"/org/hibernate/orm/test/cid/Product.hbm.xml",
"/org/hibernate/orm/test/any/hbm/Properties.hbm.xml",
"legacy/Commento.hbm.xml",
"legacy/Marelo.hbm.xml"
};
@ -2607,8 +2697,18 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
sqmStatement = (SqmSelectStatement<?>) q.unwrap( QuerySqmImpl.class ).getSqmStatement();
selections = sqmStatement.getQuerySpec().getSelectClause().getSelections();
assertThat( selections.size(), is( 2 ) );
assertThat( selections.get( 0 ), nullValue() );
assertThat( selections.get( 1 ), is( "avg" ) );
assertThat( selections.get( 0 ), notNullValue() );
assertThat( selections.get( 0 ).getAlias(), nullValue() );
assertThat( selections.get( 0 ).getSelectableNode(), instanceOf( SqmFunction.class ) );
assertThat( ( (SqmFunction) selections.get( 0 ).getSelectableNode() ).getFunctionName(), is( "count" ) );
assertThat( selections.get( 1 ), notNullValue());
assertThat( selections.get( 1 ).getAlias(), notNullValue() );
assertThat( selections.get( 1 ).getAlias(), is( "avg" ) );
assertThat( selections.get( 1 ).getSelectableNode(), instanceOf( SqmFunction.class ) );
assertThat( ( (SqmFunction) selections.get( 1 ).getSelectableNode() ).getFunctionName(), is( "avg" ) );
s.delete(a);
t.commit();
s.close();

View File

@ -198,10 +198,15 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
String[] mappings = getMappings();
if ( mappings != null ) {
for ( String mapping : mappings ) {
configuration.addResource(
getBaseForMappings() + mapping,
getClass().getClassLoader()
);
if ( mapping.startsWith( "/" ) ) {
configuration.addResource( mapping, getClass().getClassLoader() );
}
else {
configuration.addResource(
getBaseForMappings() + mapping,
getClass().getClassLoader()
);
}
}
}
Class<?>[] annotatedClasses = getAnnotatedClasses();