use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class HqlSqlWalker method createFromJoinElement.
@Override
protected void createFromJoinElement(AST path, AST alias, int joinType, AST fetchNode, AST propertyFetch, AST with) throws SemanticException {
boolean fetch = fetchNode != null;
if (fetch && isSubQuery()) {
throw new QueryException("fetch not allowed in subquery from-elements");
}
// the incoming "path" can be either:
// 1) an implicit join path (join p.address.city)
// 2) an entity-join (join com.acme.User)
//
// so make the proper interpretation here...
final EntityPersister entityJoinReferencedPersister = resolveEntityJoinReferencedPersister(path);
if (entityJoinReferencedPersister != null) {
// `path` referenced an entity
final EntityJoinFromElement join = createEntityJoin(entityJoinReferencedPersister, alias, joinType, propertyFetch, with);
((FromReferenceNode) path).setFromElement(join);
} else {
if (path.getType() != SqlTokenTypes.DOT) {
throw new SemanticException("Path expected for join!");
}
DotNode dot = (DotNode) path;
JoinType hibernateJoinType = JoinProcessor.toHibernateJoinType(joinType);
// Tell the dot node about the join type.
dot.setJoinType(hibernateJoinType);
dot.setFetch(fetch);
// Generate an explicit join for the root dot node. The implied joins will be collected and passed up
// to the root dot node.
dot.resolve(true, false, alias == null ? null : alias.getText());
final FromElement fromElement;
if (dot.getDataType() != null && dot.getDataType().isComponentType()) {
if (dot.getDataType().isAnyType()) {
throw new SemanticException("An AnyType attribute cannot be join fetched");
// ^^ because the discriminator (aka, the "meta columns") must be known to the SQL in
// a non-parameterized way.
}
FromElementFactory factory = new FromElementFactory(getCurrentFromClause(), dot.getLhs().getFromElement(), dot.getPropertyPath(), alias == null ? null : alias.getText(), null, false);
fromElement = factory.createComponentJoin((CompositeType) dot.getDataType());
} else {
fromElement = dot.getImpliedJoin();
fromElement.setAllPropertyFetch(propertyFetch != null);
if (with != null) {
if (fetch) {
throw new SemanticException("with-clause not allowed on fetched associations; use filters");
}
handleWithFragment(fromElement, with);
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("createFromJoinElement() : " + getASTPrinter().showAsString(fromElement, "-- join tree --"));
}
}
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class CriteriaQueryTranslator method getPathInfo.
private CriteriaInfoProvider getPathInfo(String path) {
StringTokenizer tokens = new StringTokenizer(path, ".");
String componentPath = "";
// start with the 'rootProvider'
CriteriaInfoProvider provider = nameCriteriaInfoMap.get(rootEntityName);
while (tokens.hasMoreTokens()) {
componentPath += tokens.nextToken();
final Type type = provider.getType(componentPath);
if (type.isAssociationType()) {
// CollectionTypes are always also AssociationTypes - but there's not always an associated entity...
final AssociationType atype = (AssociationType) type;
final CollectionType ctype = type.isCollectionType() ? (CollectionType) type : null;
final Type elementType = (ctype != null) ? ctype.getElementType(sessionFactory) : null;
// is the association a collection of components or value-types? (i.e a colloction of valued types?)
if (ctype != null && elementType.isComponentType()) {
provider = new ComponentCollectionCriteriaInfoProvider(helper.getCollectionPersister(ctype.getRole()));
} else if (ctype != null && !elementType.isEntityType()) {
provider = new ScalarCollectionCriteriaInfoProvider(helper, ctype.getRole());
} else {
provider = new EntityCriteriaInfoProvider((Queryable) sessionFactory.getEntityPersister(atype.getAssociatedEntityName(sessionFactory)));
}
componentPath = "";
} else if (type.isComponentType()) {
if (!tokens.hasMoreTokens()) {
throw new QueryException("Criteria objects cannot be created directly on components. Create a criteria on " + "owning entity and use a dotted property to access component property: " + path);
} else {
componentPath += '.';
}
} else {
throw new QueryException("not an association: " + componentPath);
}
}
return provider;
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class SQLQueryParser method resolveProperties.
private String resolveProperties(String aliasName, String propertyName) {
Map fieldResults = context.getPropertyResultsMapByAlias(aliasName);
SQLLoadable persister = context.getEntityPersisterByAlias(aliasName);
String suffix = context.getEntitySuffixByAlias(aliasName);
if ("*".equals(propertyName)) {
if (!fieldResults.isEmpty()) {
throw new QueryException("Using return-propertys together with * syntax is not supported.");
}
aliasesFound++;
return persister.selectFragment(aliasName, suffix);
} else {
String[] columnAliases;
// Let return-propertys override whatever the persister has for aliases.
columnAliases = (String[]) fieldResults.get(propertyName);
if (columnAliases == null) {
columnAliases = persister.getSubclassPropertyColumnAliases(propertyName, suffix);
}
if (columnAliases == null || columnAliases.length == 0) {
throw new QueryException("No column name found for property [" + propertyName + "] for alias [" + aliasName + "]", originalQueryString);
}
if (columnAliases.length != 1) {
// TODO: better error message since we actually support composites if names are explicitly listed.
throw new QueryException("SQL queries only support properties mapped to a single column - property [" + propertyName + "] is mapped to " + columnAliases.length + " columns.", originalQueryString);
}
aliasesFound++;
return columnAliases[0];
}
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class QueryTranslatorTestCase method assertTranslation.
protected void assertTranslation(String hql, Map replacements, boolean scalar, String sql) {
SessionFactoryImplementor factory = sessionFactory();
// Create an empty replacements map if we don't have one.
if (replacements == null) {
replacements = new HashMap();
}
// steve -> note that the empty maps here represent the currently enabled filters...
QueryTranslator oldQueryTranslator = null;
Exception oldException = null;
try {
System.out.println("Compiling with classic QueryTranslator...");
QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
oldQueryTranslator = classic.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
oldQueryTranslator.compile(replacements, scalar);
} catch (QueryException e) {
oldException = e;
} catch (MappingException e) {
oldException = e;
}
QueryTranslator newQueryTranslator = null;
Exception newException = null;
try {
System.out.println("Compiling with AST QueryTranslator...");
newQueryTranslator = createNewQueryTranslator(hql, replacements, scalar);
} catch (QueryException e) {
newException = e;
} catch (MappingException e) {
newException = e;
}
// If the old QT threw an exception, the new one should too.
if (oldException != null) {
assertNotNull("New query translator did *NOT* throw an exception, the old one did : " + oldException, newException);
assertEquals(oldException.getMessage(), newException.getMessage());
// Don't bother with the rest of the assertions.
return;
} else if (newException != null) {
newException.printStackTrace();
assertNull("Old query translator did not throw an exception, the new one did", newException);
}
// -- check all of the outputs --
checkSql(oldQueryTranslator, newQueryTranslator, hql, scalar, sql);
checkQuerySpaces(oldQueryTranslator, newQueryTranslator);
checkReturnedTypes(oldQueryTranslator, newQueryTranslator);
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class QueryTranslatorTestCase method compileBadHql.
protected Exception compileBadHql(String hql, boolean scalar) {
QueryTranslator newQueryTranslator;
Map replacements = null;
Exception newException = null;
SessionFactoryImplementor factory = sessionFactory();
try {
QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
newQueryTranslator = ast.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
newQueryTranslator.compile(replacements, scalar);
} catch (QueryException e) {
newException = e;
} catch (MappingException e) {
newException = e;
}
assertNotNull("Expected exception from compilation of '" + hql + "'!", newException);
return newException;
}
Aggregations