类org.hibernate.loader.custom.ScalarReturn源码实例Demo

下面列出了怎么用org.hibernate.loader.custom.ScalarReturn的API类实例代码及写法,或者点击链接到github查看源代码。

public IgniteQueryParsingResult getResult() {
	StringBuilder queryBuilder = new StringBuilder();
	List<ScalarReturn> selections = select( queryBuilder );
	from( queryBuilder );
	if ( !StringHelper.isEmpty( from ) ) {
		queryBuilder.append( ' ' ).append( from );
	}
	if ( !StringHelper.isEmpty( where ) ) {
		queryBuilder.append( " WHERE " ).append( where );
	}
	if ( !StringHelper.isEmpty( orderBy ) ) {
		queryBuilder.append( " ORDER BY " ).append( orderBy );
	}

	IgniteQueryDescriptor queryDescriptor = new IgniteQueryDescriptor(
		queryBuilder.toString(), indexedParameters, !selections.isEmpty(),
		propertyHelper.getKeyMetaData( propertyHelper.getRootEntity() ), selections );

	List<String> selectionAliases = selections.isEmpty()
		? ENTITY_COLUMN_NAMES
		: selections.stream().map( ScalarReturn::getColumnAlias ).collect( toList() );

	return new IgniteQueryParsingResult( queryDescriptor, selectionAliases );
}
 
public IgniteQueryDescriptor(String sql, List<Object> indexedParameters,
		boolean hasScalar, EntityKeyMetadata rootKeyMetadata,
		List<ScalarReturn> queryReturns) {
	this.sql = sql;
	this.indexedParameters = indexedParameters;
	this.hasScalar = hasScalar;
	this.rootKeyMetadata = rootKeyMetadata;
	this.queryReturns = queryReturns;
}
 
public List<ScalarReturn> getQueryReturns() {
	return queryReturns;
}
 
/**
 * Appends SELECT clause in query builder and returns either
 * list of selections if a query is a projection query, or empty
 * list if a single entity query
 */
private List<ScalarReturn> select(StringBuilder queryBuilder) {
	queryBuilder.append( "SELECT " );
	String rootAlias = propertyHelper.findAliasForType( propertyHelper.getRootEntity() );

	// is selected unqualified root entity (e.g. "from Hypothesis"),
	// or a single entity defined by alias (e.g. "select h from Hypothesis h")
	if ( propertyHelper.getSelections().isEmpty()
		|| ( propertyHelper.getSelections().size() == 1
		&& propertyHelper.getSelections().get( 0 ).getNodeNamesWithoutAlias().isEmpty() ) ) {
		String selectionAlias = propertyHelper.getSelections().isEmpty()
			? rootAlias
			: propertyHelper.getSelections().get( 0 ).getFirstNode().getName();
		queryBuilder
			.append( selectionAlias ).append( "._KEY, " )
			.append( selectionAlias ).append( "._VAL" );
		return Collections.emptyList();
	}

	// else, treat as projection selection
	List<ScalarReturn> selections = new ArrayList<>();
	int columnNumber = 0;
	Iterator<PropertyPath> i = propertyHelper.getSelections().iterator();
	while ( i.hasNext() ) {
		PropertyPath path = i.next();
		String alias = path.getFirstNode().isAlias()
			? path.getFirstNode().getName() : rootAlias;

		String columnName;
		List<String> propertyPath = path.getNodeNamesWithoutAlias();
		String entityType = propertyHelper.getEntityNameByAlias( alias );
		Type type = propertyHelper.getPropertyType( entityType, propertyPath );
		if ( type.isEntityType() ) {
			// though it may be better to load both key and value
			// in one query, OgmQueryLoader requires only key
			columnName = "_KEY";
		}
		else if ( type.isComponentType() ) {
			throw new NotYetImplementedException( "Embeddables in projection selection" );
		}
		else {
			columnName = propertyHelper.getColumnName( entityType, propertyPath );
			EntityKeyMetadata entityKey = propertyHelper.getKeyMetaData( entityType );
			if ( entityKey.getColumnNames().length == 1
				&& entityKey.getColumnNames()[0].equals( columnName ) ) {
				columnName = "_KEY";
			}
		}
		String columnAlias = "col_" + ( columnNumber++ );
		queryBuilder
			.append( alias ).append( '.' ).append( columnName )
			.append( " as " ).append( columnAlias );
		selections.add( new ScalarReturn( type, columnAlias ) );

		if ( i.hasNext() ) {
			queryBuilder.append( ',' ).append( ' ' );
		}
	}
	return selections;
}
 
源代码5 项目: hibernate-ogm-ignite   文件: IgniteDialect.java
ProjectionResultCursor(Iterable<List<?>> resultCursor, List<ScalarReturn> queryReturns, RowSelection rowSelection) {
	super( resultCursor, rowSelection );
	this.queryReturns = queryReturns;
}
 
 类所在包
 同包方法