下面列出了怎么用org.hibernate.persister.entity.PropertyMapping的API类实例代码及写法,或者点击链接到github查看源代码。
protected QueryableCollection getQueryableCollection(
String entityName,
String propertyName,
SessionFactoryImplementor factory) throws HibernateException {
final PropertyMapping ownerMapping = (PropertyMapping) factory.getEntityPersister( entityName );
final Type type = ownerMapping.toType( propertyName );
if ( !type.isCollectionType() ) {
throw new MappingException(
"Property path [" + entityName + "." + propertyName + "] does not reference a collection"
);
}
final String role = ( (CollectionType) type ).getRole();
try {
return (QueryableCollection) factory.getCollectionPersister( role );
}
catch ( ClassCastException cce ) {
throw new QueryException( "collection role is not queryable: " + role );
}
catch ( Exception e ) {
throw new QueryException( "collection role not found: " + role );
}
}
@Override
public ExpandingCompositeQuerySpace makeCompositeIdentifierQuerySpace() {
final String compositeQuerySpaceUid = getUid() + "-id";
final ExpandingCompositeQuerySpace rhs = getExpandingQuerySpaces().makeCompositeQuerySpace(
compositeQuerySpaceUid,
new CompositePropertyMapping(
(CompositeType) getEntityPersister().getIdentifierType(),
(PropertyMapping) getEntityPersister(),
getEntityPersister().getIdentifierPropertyName()
),
canJoinsBeRequired()
);
final JoinDefinedByMetadata join = JoinHelper.INSTANCE.createCompositeJoin(
this,
EntityPersister.ENTITY_ID,
rhs,
canJoinsBeRequired(),
(CompositeType) persister.getIdentifierType()
);
internalGetJoins().add( join );
return rhs;
}
/**
* Get the qualified (prefixed by alias) names of the columns of the owning entity which are to be used in the join
*
* @param associationType The association type for the association that represents the join
* @param columnQualifier The left-hand side table alias
* @param propertyIndex The index of the property that represents the association/join
* @param begin The index for any nested (composites) attributes
* @param lhsPersister The persister for the left-hand side of the association/join
* @param mapping The mapping (typically the SessionFactory).
*
* @return The qualified column names.
*/
public static String[] getAliasedLHSColumnNames(
AssociationType associationType,
String columnQualifier,
int propertyIndex,
int begin,
OuterJoinLoadable lhsPersister,
Mapping mapping) {
if ( associationType.useLHSPrimaryKey() ) {
return StringHelper.qualify( columnQualifier, lhsPersister.getIdentifierColumnNames() );
}
else {
final String propertyName = associationType.getLHSPropertyName();
if ( propertyName == null ) {
return ArrayHelper.slice(
toColumns( lhsPersister, columnQualifier, propertyIndex ),
begin,
associationType.getColumnSpan( mapping )
);
}
else {
//bad cast
return ( (PropertyMapping) lhsPersister ).toColumns( columnQualifier, propertyName );
}
}
}
PropertyMapping getPropertyMapping(String name) throws QueryException {
PropertyMapping decorator = getDecoratedPropertyMapping( name );
if ( decorator != null ) {
return decorator;
}
String type = getType( name );
if ( type == null ) {
String role = getRole( name );
if ( role == null ) {
throw new QueryException( "alias not found: " + name );
}
return getCollectionPersister( role ); //.getElementPropertyMapping();
}
else {
Queryable persister = getEntityPersister( type );
if ( persister == null ) {
throw new QueryException( "persistent class not found: " + type );
}
return persister;
}
}
protected QueryableCollection getQueryableCollection(String entityName, String propertyName, SessionFactoryImplementor factory)
throws HibernateException {
PropertyMapping ownerMapping = ( PropertyMapping ) factory.getEntityPersister( entityName );
Type type = ownerMapping.toType( propertyName );
if ( !type.isCollectionType() ) {
throw new MappingException(
"Property path [" + entityName + "." + propertyName + "] does not reference a collection"
);
}
String role = ( ( CollectionType ) type ).getRole();
try {
return ( QueryableCollection ) factory.getCollectionPersister( role );
}
catch ( ClassCastException cce ) {
throw new QueryException( "collection role is not queryable: " + role );
}
catch ( Exception e ) {
throw new QueryException( "collection role not found: " + role );
}
}
/**
* Get the aliased columns of the owning entity which are to
* be used in the join
*/
public static String[] getAliasedLHSColumnNames(
AssociationType type,
String alias,
int property,
int begin,
OuterJoinLoadable lhsPersister,
Mapping mapping
) {
if ( type.useLHSPrimaryKey() ) {
return StringHelper.qualify( alias, lhsPersister.getIdentifierColumnNames() );
}
else {
String propertyName = type.getLHSPropertyName();
if (propertyName==null) {
return ArrayHelper.slice(
lhsPersister.toColumns(alias, property),
begin,
type.getColumnSpan(mapping)
);
}
else {
return ( (PropertyMapping) lhsPersister ).toColumns(alias, propertyName); //bad cast
}
}
}
/**
* Returns the type of a property, given it's name (the last part) and the full path.
*
* @param propertyName The last part of the full path to the property.
* @return The type.
* @0param propertyPath The full property path.
*/
public Type getPropertyType(String propertyName, String propertyPath) {
checkInitialized();
Type type = null;
// If this is an entity and the property is the identifier property, then use getIdentifierType().
// Note that the propertyName.equals( propertyPath ) checks whether we have a component
// key reference, where the component class property name is the same as the
// entity id property name; if the two are not equal, this is the case and
// we'd need to "fall through" to using the property mapping.
if ( persister != null && propertyName.equals( propertyPath ) && propertyName.equals( persister.getIdentifierPropertyName() ) ) {
type = persister.getIdentifierType();
}
else { // Otherwise, use the property mapping.
PropertyMapping mapping = getPropertyMapping( propertyName );
type = mapping.toType( propertyPath );
}
if ( type == null ) {
throw new MappingException( "Property " + propertyName + " does not exist in " +
( ( queryableCollection == null ) ? "class" : "collection" ) + " "
+ ( ( queryableCollection == null ) ? fromElement.getClassName() : queryableCollection.getRole() ) );
}
return type;
}
PropertyMapping getPropertyMapping(String propertyName) {
checkInitialized();
if ( queryableCollection == null ) { // Not a collection?
return ( PropertyMapping ) persister; // Return the entity property mapping.
}
// If the property is a special collection property name, return a CollectionPropertyMapping.
if ( CollectionProperties.isCollectionProperty( propertyName ) ) {
if ( collectionPropertyMapping == null ) {
collectionPropertyMapping = new CollectionPropertyMapping( queryableCollection );
}
return collectionPropertyMapping;
}
if ( queryableCollection.getElementType().isAnyType() ) {
// collection of <many-to-any/> mappings...
// used to circumvent the component-collection check below...
return queryableCollection;
}
if ( queryableCollection.getElementType().isComponentType() ) {
// Collection of components.
if ( propertyName.equals( EntityPersister.ENTITY_ID ) ) {
return ( PropertyMapping ) queryableCollection.getOwnerEntityPersister();
}
}
return queryableCollection;
}
PropertyMapping getPropertyMapping(String name) throws QueryException {
PropertyMapping decorator = getDecoratedPropertyMapping( name );
if ( decorator != null ) return decorator;
String type = getType( name );
if ( type == null ) {
String role = getRole( name );
if ( role == null ) {
throw new QueryException( "alias not found: " + name );
}
return getCollectionPersister( role ); //.getElementPropertyMapping();
}
else {
Queryable persister = getEntityPersister( type );
if ( persister == null ) throw new QueryException( "persistent class not found: " + type );
return persister;
}
}
private PropertyMapping getPropertyMapping(String entityName) throws MappingException {
final CriteriaInfoProvider info = nameCriteriaInfoMap.get( entityName );
if ( info == null ) {
throw new HibernateException( "Unknown entity: " + entityName );
}
return info.getPropertyMapping();
}
/**
* Builds a CompositePropertyMapping
*
* @param compositeType The composite being described by this PropertyMapping
* @param parentPropertyMapping The PropertyMapping of our parent (composites have to have a parent/owner)
* @param parentPropertyName The name of this composite within the parentPropertyMapping
*/
public CompositePropertyMapping(
CompositeType compositeType,
PropertyMapping parentPropertyMapping,
String parentPropertyName) {
this.compositeType = compositeType;
this.parentPropertyMapping = parentPropertyMapping;
this.parentPropertyName = parentPropertyName;
}
@Override
public PropertyMapping getPropertyMapping() {
// entity persisters are typically PropertyMapping implementors, but this is part of the funky
// "optional interface hierarchy" for entity persisters. The internal ones all implement
// PropertyMapping...
return (PropertyMapping) persister;
}
public String toSubselectString(String ukname) {
String[] joinColumns = ukname == null
? StringHelper.qualify( alias, loadable.getIdentifierColumnNames() )
: ( (PropertyMapping) loadable ).toColumns( alias, ukname );
return "select " + String.join( ", ", joinColumns ) + queryString;
}
/**
* Returns the type of a property, given it's name (the last part) and the full path.
*
* @param propertyName The last part of the full path to the property.
*
* @return The type.
*
* @0param getPropertyPath The full property path.
*/
public Type getPropertyType(String propertyName, String propertyPath) {
checkInitialized();
Type type = null;
// If this is an entity and the property is the identifier property, then use getIdentifierType().
// Note that the propertyName.equals( getPropertyPath ) checks whether we have a component
// key reference, where the component class property name is the same as the
// entity id property name; if the two are not equal, this is the case and
// we'd need to "fall through" to using the property mapping.
if ( persister != null && propertyName.equals( propertyPath ) && propertyName.equals( persister.getIdentifierPropertyName() ) ) {
type = persister.getIdentifierType();
}
else { // Otherwise, use the property mapping.
PropertyMapping mapping = getPropertyMapping( propertyName );
type = mapping.toType( propertyPath );
}
if ( type == null ) {
throw new MappingException(
"Property " + propertyName + " does not exist in " +
( ( queryableCollection == null ) ? "class" : "collection" ) + " "
+ ( ( queryableCollection == null ) ?
fromElement.getClassName() :
queryableCollection.getRole() )
);
}
return type;
}
public String toSubselectString(String ukname) {
String[] joinColumns = ukname==null ?
StringHelper.qualify( alias, loadable.getIdentifierColumnNames() ) :
( (PropertyMapping) loadable ).toColumns(alias, ukname);
return new StringBuffer()
.append("select ")
.append( StringHelper.join(", ", joinColumns) )
.append(queryString)
.toString();
}
@Override
public PropertyMapping getPropertyMapping() {
return helper.getCollectionPropertyMapping( role );
}
@Override
public PropertyMapping getPropertyMapping() {
return persister;
}
@Override
public PropertyMapping getPropertyMapping() {
return persister;
}
private static String[] determineRhsColumnNames(EntityType entityType, SessionFactoryImplementor sessionFactory) {
final Joinable persister = entityType.getAssociatedJoinable( sessionFactory );
return entityType.getRHSUniqueKeyPropertyName() == null ?
persister.getKeyColumnNames() :
( (PropertyMapping) persister ).toColumns( entityType.getRHSUniqueKeyPropertyName() );
}
@Override
public PropertyMapping getPropertyMapping() {
return compositeSubPropertyMapping;
}
@Override
public PropertyMapping getPropertyMapping() {
return (PropertyMapping) persister;
}
private CollectionFetchableIndex buildIndexGraph() {
final CollectionPersister persister = collectionQuerySpace.getCollectionPersister();
if ( persister.hasIndex() ) {
final Type type = persister.getIndexType();
if ( type.isAssociationType() ) {
if ( type.isEntityType() ) {
final EntityPersister indexPersister = persister.getFactory().getEntityPersister(
( (EntityType) type ).getAssociatedEntityName()
);
final ExpandingEntityQuerySpace entityQuerySpace = QuerySpaceHelper.INSTANCE.makeEntityQuerySpace(
collectionQuerySpace,
indexPersister,
CollectionPropertyNames.COLLECTION_INDICES,
(EntityType) persister.getIndexType(),
collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(),
collectionQuerySpace.canJoinsBeRequired(),
allowIndexJoin
);
return new CollectionFetchableIndexEntityGraph( this, entityQuerySpace );
}
else if ( type.isAnyType() ) {
return new CollectionFetchableIndexAnyGraph( this );
}
}
else if ( type.isComponentType() ) {
final ExpandingCompositeQuerySpace compositeQuerySpace = QuerySpaceHelper.INSTANCE.makeCompositeQuerySpace(
collectionQuerySpace,
new CompositePropertyMapping(
(CompositeType) persister.getIndexType(),
(PropertyMapping) persister,
""
),
CollectionPropertyNames.COLLECTION_INDICES,
(CompositeType) persister.getIndexType(),
collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(),
collectionQuerySpace.canJoinsBeRequired(),
allowIndexJoin
);
return new CollectionFetchableIndexCompositeGraph( this, compositeQuerySpace );
}
}
return null;
}
private CollectionFetchableElement buildElementGraph() {
final CollectionPersister persister = collectionQuerySpace.getCollectionPersister();
final Type type = persister.getElementType();
if ( type.isAssociationType() ) {
if ( type.isEntityType() ) {
final EntityPersister elementPersister = persister.getFactory().getEntityPersister(
( (EntityType) type ).getAssociatedEntityName()
);
final ExpandingEntityQuerySpace entityQuerySpace = QuerySpaceHelper.INSTANCE.makeEntityQuerySpace(
collectionQuerySpace,
elementPersister,
CollectionPropertyNames.COLLECTION_ELEMENTS,
(EntityType) persister.getElementType(),
collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(),
collectionQuerySpace.canJoinsBeRequired(),
allowElementJoin
);
return new CollectionFetchableElementEntityGraph( this, entityQuerySpace );
}
else if ( type.isAnyType() ) {
return new CollectionFetchableElementAnyGraph( this );
}
}
else if ( type.isComponentType() ) {
final ExpandingCompositeQuerySpace compositeQuerySpace = QuerySpaceHelper.INSTANCE.makeCompositeQuerySpace(
collectionQuerySpace,
new CompositePropertyMapping(
(CompositeType) persister.getElementType(),
(PropertyMapping) persister,
""
),
CollectionPropertyNames.COLLECTION_ELEMENTS,
(CompositeType) persister.getElementType(),
collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(),
collectionQuerySpace.canJoinsBeRequired(),
allowElementJoin
);
return new CollectionFetchableElementCompositeGraph( this, compositeQuerySpace );
}
return null;
}
@Override
public PropertyMapping getPropertyMapping(String propertyName) {
return propertyMapping;
}
protected PropertyMapping getBasePropertyMapping() {
return getOrigin().getPropertyMapping( "" );
}
public PropertyMapping getPropertyMapping(String propertyName) {
return elementType.getPropertyMapping( propertyName );
}
String[] toColumns(String tableAlias, String path, boolean inSelect, boolean forceAlias) {
checkInitialized();
PropertyMapping propertyMapping = getPropertyMapping( path );
if ( !inSelect && queryableCollection != null && CollectionProperties.isCollectionProperty( path ) ) {
// If this from element is a collection and the path is a collection property (maxIndex, etc.)
// requiring a sub-query then generate a sub-query.
//h
// Unless we are in the select clause, because some dialects do not support
// Note however, that some dialects do not However, in the case of this being a collection property reference being in the select, not generating the subquery
// will not generally work. The specific cases I am thinking about are the minIndex, maxIndex
// (most likely minElement, maxElement as well) cases.
// todo : if ^^ is the case we should thrown an exception here rather than waiting for the sql error
// if the dialect supports select-clause subqueries we could go ahead and generate the subquery also
// we also need to account for cases where the property name is a CollectionProperty, but
// is also a property on the element-entity. This is handled inside `#getPropertyMapping`
// already; here just check for propertyMapping being the same as the entity persister. Yes
// this is hacky, but really this is difficult to handle given the current codebase.
if ( persister != propertyMapping ) {
// we want the subquery...
// DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfCollectionPropertiesInHql( path, fromElement.getClassAlias() );
return getCollectionPropertyReference( path ).toColumns( tableAlias );
}
}
if ( forceAlias ) {
return propertyMapping.toColumns( tableAlias, path );
}
if ( fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.SELECT ) {
return propertyMapping.toColumns( tableAlias, path );
}
if ( fromElement.getWalker().isSubQuery() ) {
// for a subquery, the alias to use depends on a few things (we
// already know this is not an overall SELECT):
// 1) if this FROM_ELEMENT represents a correlation to the
// outer-most query
// A) if the outer query represents a multi-table
// persister, we need to use the given alias
// in anticipation of one of the multi-table
// executors being used (as this subquery will
// actually be used in the "id select" phase
// of that multi-table executor)
// B) otherwise, we need to use the persister's
// table name as the column qualification
// 2) otherwise (not correlated), use the given alias
if ( isCorrelation() ) {
if ( isMultiTable() || isInsertQuery() ) {
return propertyMapping.toColumns( tableAlias, path );
}
return propertyMapping.toColumns( extractTableName(), path );
}
return propertyMapping.toColumns( tableAlias, path );
}
if ( fromElement.getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.SELECT ) {
return propertyMapping.toColumns( tableAlias, path );
}
if ( isManipulationQuery() && isMultiTable() && inWhereClause() ) {
// the actual where-clause will end up being ripped out the update/delete and used in
// a select to populate the temp table, so its ok to use the table alias to qualify the table refs
// and safer to do so to protect from same-named columns
return propertyMapping.toColumns( tableAlias, path );
}
String[] columns = propertyMapping.toColumns( path );
LOG.tracev( "Using non-qualified column reference [{0} -> ({1})]", path, ArrayHelper.toString( columns ) );
return columns;
}
PropertyMapping getPropertyMapping(String propertyName) {
checkInitialized();
if ( queryableCollection == null ) { // Not a collection?
return (PropertyMapping) persister; // Return the entity property mapping.
}
// indexed, many-to-many collections must be treated specially here if the property to
// be mapped touches on the index as we must adjust the alias to use the alias from
// the association table (which i different than the one passed in
if ( queryableCollection.isManyToMany()
&& queryableCollection.hasIndex()
&& SPECIAL_MANY2MANY_TREATMENT_FUNCTION_NAMES.contains( propertyName ) ) {
return new SpecialManyToManyCollectionPropertyMapping();
}
// If the property is a special collection property name, return a CollectionPropertyMapping.
if ( CollectionProperties.isCollectionProperty( propertyName ) ) {
if ( collectionPropertyMapping == null ) {
// lets additionally make sure that the property name is not also the name
// of a property on the element, assuming that the element is an entity.
// todo : also consider composites?
if ( persister != null ) {
try {
if ( persister.getPropertyType( propertyName ) != null ) {
return (PropertyMapping) persister;
}
}
catch (QueryException ignore) {
}
}
collectionPropertyMapping = new CollectionPropertyMapping( queryableCollection );
}
return collectionPropertyMapping;
}
if ( queryableCollection.getElementType().isAnyType() ) {
// collection of <many-to-any/> mappings...
// used to circumvent the component-collection check below...
return queryableCollection;
}
if ( queryableCollection.getElementType().isComponentType() ) {
// Collection of components.
if ( propertyName.equals( EntityPersister.ENTITY_ID ) ) {
return (PropertyMapping) queryableCollection.getOwnerEntityPersister();
}
}
return queryableCollection;
}
public CollectionPropertyReference getCollectionPropertyReference(final String propertyName) {
if ( queryableCollection == null ) {
throw new QueryException( "Not a collection reference" );
}
final PropertyMapping collectionPropertyMapping;
if ( queryableCollection.isManyToMany()
&& queryableCollection.hasIndex()
&& SPECIAL_MANY2MANY_TREATMENT_FUNCTION_NAMES.contains( propertyName ) ) {
collectionPropertyMapping = new SpecialManyToManyCollectionPropertyMapping();
}
else if ( CollectionProperties.isCollectionProperty( propertyName ) ) {
if ( this.collectionPropertyMapping == null ) {
this.collectionPropertyMapping = new CollectionPropertyMapping( queryableCollection );
}
collectionPropertyMapping = this.collectionPropertyMapping;
}
else {
collectionPropertyMapping = queryableCollection;
}
return new CollectionPropertyReference() {
@Override
public Type getType() {
return collectionPropertyMapping.toType( propertyName );
}
@Override
public String[] toColumns(final String tableAlias) {
if ( propertyName.equalsIgnoreCase( "index" ) ) {
return collectionPropertyMapping.toColumns( tableAlias, propertyName );
}
Map enabledFilters = fromElement.getWalker().getEnabledFilters();
String subquery = CollectionSubqueryFactory.createCollectionSubquery(
joinSequence.copyForCollectionProperty().setUseThetaStyle( true ),
enabledFilters,
collectionPropertyMapping.toColumns( tableAlias, propertyName )
);
LOG.debugf( "toColumns(%s,%s) : subquery = %s", tableAlias, propertyName, subquery );
return new String[] {"(" + subquery + ")"};
}
};
}
private PropertyMapping getPropertyMapping() {
return currentPropertyMapping;
}