use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class EntityGraphQueryHint method getFromElements.
private List<FromElement> getFromElements(List attributeNodes, FromElement origin, FromClause fromClause, HqlSqlWalker walker, Map<String, FromElement> explicitFetches) {
final List<FromElement> fromElements = new ArrayList<FromElement>();
for (Object obj : attributeNodes) {
final AttributeNode<?> attributeNode = (AttributeNode<?>) obj;
final String attributeName = attributeNode.getAttributeName();
final String className = origin.getClassName();
// TODO: This is ignored by collection types and probably wrong for entity types. Presumably it screws
// with inheritance.
final String role = className + "." + attributeName;
final String classAlias = origin.getClassAlias();
final String originTableAlias = origin.getTableAlias();
final Type propertyType = origin.getPropertyType(attributeName, attributeName);
try {
FromElement fromElement = explicitFetches.get(role);
boolean explicitFromElement = false;
if (fromElement == null) {
if (propertyType.isEntityType()) {
final EntityType entityType = (EntityType) propertyType;
final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
final String tableAlias = walker.getAliasGenerator().createName(entityType.getAssociatedEntityName());
final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
final JoinSequence joinSequence = walker.getSessionFactoryHelper().createJoinSequence(false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns);
fromElement = fromElementFactory.createEntityJoin(entityType.getAssociatedEntityName(), tableAlias, joinSequence, true, walker.isInFrom(), entityType, role, null);
} else if (propertyType.isCollectionType()) {
CollectionType collectionType = (CollectionType) propertyType;
final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
final QueryableCollection queryableCollection = walker.getSessionFactoryHelper().requireQueryableCollection(collectionType.getRole());
fromElement = fromElementFactory.createCollection(queryableCollection, collectionType.getRole(), JoinType.LEFT_OUTER_JOIN, true, false);
}
} else {
explicitFromElement = true;
fromElement.setInProjectionList(true);
fromElement.setFetch(true);
}
if (fromElement != null) {
if (!explicitFromElement) {
fromElements.add(fromElement);
}
// recurse into subgraphs
for (Subgraph<?> subgraph : attributeNode.getSubgraphs().values()) {
fromElements.addAll(getFromElements(subgraph.getAttributeNodes(), fromElement, fromClause, walker, explicitFetches));
}
}
} catch (Exception e) {
throw new QueryException("Could not apply the EntityGraph to the Query!", e);
}
}
return fromElements;
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class CriteriaQueryTranslator method createAssociationPathCriteriaMap.
private void createAssociationPathCriteriaMap() {
final Iterator<CriteriaImpl.Subcriteria> iter = rootCriteria.iterateSubcriteria();
while (iter.hasNext()) {
CriteriaImpl.Subcriteria crit = iter.next();
String wholeAssociationPath = getWholeAssociationPath(crit);
Object old = associationPathCriteriaMap.put(wholeAssociationPath, crit);
if (old != null) {
throw new QueryException("duplicate association path: " + wholeAssociationPath);
}
JoinType joinType = crit.getJoinType();
old = associationPathJoinTypesMap.put(wholeAssociationPath, joinType);
if (old != null) {
// TODO : not so sure this is needed...
throw new QueryException("duplicate association path: " + wholeAssociationPath);
}
if (crit.getWithClause() != null) {
this.withClauseMap.put(wholeAssociationPath, crit.getWithClause());
}
}
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class CriteriaQueryTranslator method createAliasCriteriaMap.
private void createAliasCriteriaMap() {
aliasCriteriaMap.put(rootCriteria.getAlias(), rootCriteria);
Iterator<CriteriaImpl.Subcriteria> iter = rootCriteria.iterateSubcriteria();
while (iter.hasNext()) {
Criteria subcriteria = iter.next();
if (subcriteria.getAlias() != null) {
Object old = aliasCriteriaMap.put(subcriteria.getAlias(), subcriteria);
if (old != null) {
throw new QueryException("duplicate alias: " + subcriteria.getAlias());
}
}
}
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class CriteriaQueryTranslator method getTypeUsingProjection.
@Override
public Type getTypeUsingProjection(Criteria subcriteria, String propertyName) throws HibernateException {
//first look for a reference to a projection alias
final Projection projection = rootCriteria.getProjection();
Type[] projectionTypes = projection == null ? null : projection.getTypes(propertyName, subcriteria, this);
if (projectionTypes == null) {
try {
//look for a property
return getType(subcriteria, propertyName);
} catch (HibernateException he) {
//not found in inner query , try the outer query
if (outerQueryTranslator != null) {
return outerQueryTranslator.getType(subcriteria, propertyName);
} else {
throw he;
}
}
} else {
if (projectionTypes.length != 1) {
//should never happen, i think
throw new QueryException("not a single-length projection: " + propertyName);
}
return projectionTypes[0];
}
}
use of org.hibernate.QueryException in project hibernate-orm by hibernate.
the class BulkManipulationTest method testUpdateOnImplicitJoinFails.
@Test
public void testUpdateOnImplicitJoinFails() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human human = new Human();
human.setName(new Name("Steve", 'E', null));
Human mother = new Human();
mother.setName(new Name("Jane", 'E', null));
human.setMother(mother);
s.save(human);
s.save(mother);
s.flush();
t.commit();
t = s.beginTransaction();
try {
s.createQuery("update Human set mother.name.initial = :initial").setString("initial", "F").executeUpdate();
fail("update allowed across implicit join");
} catch (IllegalArgumentException e) {
assertTyping(QueryException.class, e.getCause());
} catch (QueryException e) {
}
s.createQuery("delete Human where mother is not null").executeUpdate();
s.createQuery("delete Human").executeUpdate();
t.commit();
s.close();
}
Aggregations