Java Code Examples for org.hibernate.engine.spi.SessionFactoryImplementor.getImplementors()

The following are Jave code examples for showing how to use getImplementors() of the org.hibernate.engine.spi.SessionFactoryImplementor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: QuerySplitter.java   View Source Code Vote up 4 votes
/**
 * Handle Hibernate "implicit" polymorphism, by translating the query string into
 * several "concrete" queries against mapped classes.
 */
public static String[] concreteQueries(String query, SessionFactoryImplementor factory) throws MappingException {

	//scan the query string for class names appearing in the from clause and replace
	//with all persistent implementors of the class/interface, returning multiple
	//query strings (make sure we don't pick up a class in the select clause!)

	//TODO: this is one of the ugliest and most fragile pieces of code in Hibernate....

	String[] tokens = StringHelper.split( StringHelper.WHITESPACE + "(),", query, true );
	if ( tokens.length == 0 ) {
		// just especially for the trivial collection filter
		return new String[] { query };
	}
	ArrayList<String> placeholders = new ArrayList<String>();
	ArrayList<String[]> replacements = new ArrayList<String[]>();
	StringBuilder templateQuery = new StringBuilder( 40 );

	int start = getStartingPositionFor( tokens, templateQuery );
	int count = 0;
	String next;
	String last = tokens[start - 1].toLowerCase();

	for ( int i = start; i < tokens.length; i++ ) {

		String token = tokens[i];

		if ( ParserHelper.isWhitespace( token ) ) {
			templateQuery.append( token );
			continue;
		}

		next = nextNonWhite( tokens, i ).toLowerCase();

		boolean process = isJavaIdentifier( token ) &&
				isPossiblyClassName( last, next );

		last = token.toLowerCase();

		if ( process ) {
			String importedClassName = getImportedClass( token, factory );
			if ( importedClassName != null ) {
				String[] implementors = factory.getImplementors( importedClassName );
				token = "$clazz" + count++ + "$";
				if ( implementors != null ) {
					placeholders.add( token );
					replacements.add( implementors );
				}
			}
		}

		templateQuery.append( token );

	}
	String[] results = StringHelper.multiply(
			templateQuery.toString(),
			placeholders.iterator(),
			replacements.iterator()
	);
	if ( results.length == 0 ) {
		LOG.noPersistentClassesFound( query );
	}
	return results;
}