use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class QueryTranslatorImpl method addFromAssociation.
/**
* Used for collection filters
*/
private void addFromAssociation(final String elementName, final String collectionRole) throws QueryException {
//q.addCollection(collectionName, collectionRole);
QueryableCollection persister = getCollectionPersister(collectionRole);
Type collectionElementType = persister.getElementType();
if (!collectionElementType.isEntityType()) {
throw new QueryException("collection of values in filter: " + elementName);
}
String[] keyColumnNames = persister.getKeyColumnNames();
//if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);
String collectionName;
JoinSequence join = new JoinSequence(getFactory());
collectionName = persister.isOneToMany() ? elementName : createNameForCollection(collectionRole);
join.setRoot(persister, collectionName);
if (!persister.isOneToMany()) {
//many-to-many
addCollection(collectionName, collectionRole);
try {
join.addJoin((AssociationType) persister.getElementType(), elementName, JoinType.INNER_JOIN, persister.getElementColumnNames(collectionName));
} catch (MappingException me) {
throw new QueryException(me);
}
}
join.addCondition(collectionName, keyColumnNames, " = ?");
//if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
EntityType elemType = (EntityType) collectionElementType;
addFrom(elementName, elemType.getAssociatedEntityName(), join);
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class ForeignGenerator method generate.
@Override
public Serializable generate(SharedSessionContractImplementor sessionImplementor, Object object) {
// needs to be a Session for the #save and #contains calls below...
final Session session = (Session) sessionImplementor;
final EntityPersister persister = sessionImplementor.getFactory().getMetamodel().entityPersister(entityName);
Object associatedObject = persister.getPropertyValue(object, propertyName);
if (associatedObject == null) {
throw new IdentifierGenerationException("attempted to assign id from null one-to-one property [" + getRole() + "]");
}
final EntityType foreignValueSourceType;
final Type propertyType = persister.getPropertyType(propertyName);
if (propertyType.isEntityType()) {
// the normal case
foreignValueSourceType = (EntityType) propertyType;
} else {
// try identifier mapper
foreignValueSourceType = (EntityType) persister.getPropertyType(PropertyPath.IDENTIFIER_MAPPER_PROPERTY + "." + propertyName);
}
Serializable id;
try {
id = ForeignKeys.getEntityIdentifierIfNotUnsaved(foreignValueSourceType.getAssociatedEntityName(), associatedObject, sessionImplementor);
} catch (TransientObjectException toe) {
id = session.save(foreignValueSourceType.getAssociatedEntityName(), associatedObject);
}
if (session.contains(entityName, object)) {
//abort the save (the object is already saved by a circular cascade)
return IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR;
//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
}
return id;
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class WhereParser method getElementName.
private String getElementName(PathExpressionParser.CollectionElement element, QueryTranslatorImpl q) throws QueryException {
String name;
if (element.isOneToMany) {
name = element.alias;
} else {
Type type = element.elementType;
if (type.isEntityType()) {
//ie. a many-to-many
String entityName = ((EntityType) type).getAssociatedEntityName();
name = pathExpressionParser.continueFromManyToMany(entityName, element.elementColumns, q);
} else {
throw new QueryException("illegally dereferenced collection element");
}
}
return name;
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class DotNode method resolve.
public void resolve(boolean generateJoin, boolean implicitJoin, String classAlias, AST parent, AST parentPredicate) throws SemanticException {
// If this dot has already been resolved, stop now.
if (isResolved()) {
return;
}
// Prepare the left hand side and get the data type.
Type propertyType = prepareLhs();
if (parent == null && AbstractEntityPersister.ENTITY_CLASS.equals(propertyName)) {
DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfClassEntityTypeSelector(getLhs().getPath());
}
// this might be a Java constant.
if (propertyType == null) {
if (parent == null) {
getWalker().getLiteralProcessor().lookupConstant(this);
}
// stop now... there was a problem resolving the node anyway.
return;
}
if (propertyType.isComponentType()) {
// The property is a component...
checkLhsIsNotCollection();
dereferenceComponent(parent);
initText();
} else if (propertyType.isEntityType()) {
// The property is another class..
checkLhsIsNotCollection();
dereferenceEntity((EntityType) propertyType, implicitJoin, classAlias, generateJoin, parent, parentPredicate);
initText();
} else if (propertyType.isCollectionType()) {
// The property is a collection...
checkLhsIsNotCollection();
dereferenceCollection((CollectionType) propertyType, implicitJoin, false, classAlias, parent);
} else {
// Otherwise, this is a primitive type.
if (!CollectionProperties.isAnyCollectionProperty(propertyName)) {
checkLhsIsNotCollection();
}
dereferenceType = DereferenceType.PRIMITIVE;
initText();
}
setResolved();
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class MapEntryNode method determineValueSelectExpressions.
private void determineValueSelectExpressions(QueryableCollection collectionPersister, List selections) {
AliasGenerator aliasGenerator = new LocalAliasGenerator(1);
appendSelectExpressions(collectionPersister.getElementColumnNames(), selections, aliasGenerator);
Type valueType = collectionPersister.getElementType();
if (valueType.isAssociationType()) {
EntityType valueEntityType = (EntityType) valueType;
Queryable valueEntityPersister = (Queryable) sfi().getEntityPersister(valueEntityType.getAssociatedEntityName(sfi()));
SelectFragment fragment = valueEntityPersister.propertySelectFragmentFragment(elementTableAlias(), null, false);
appendSelectExpressions(fragment, selections, aliasGenerator);
}
}
Aggregations