use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.
the class Cascade method cascadeProperty.
/**
* Cascade an action to the child or children
*/
private static void cascadeProperty(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final int componentPathStackDepth, final Object parent, final Object child, final Type type, final CascadeStyle style, final String propertyName, final Object anything, final boolean isCascadeDeleteEnabled) throws HibernateException {
if (child != null) {
if (type.isAssociationType()) {
final AssociationType associationType = (AssociationType) type;
if (cascadeAssociationNow(cascadePoint, associationType)) {
cascadeAssociation(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, type, style, anything, isCascadeDeleteEnabled);
}
} else if (type.isComponentType()) {
cascadeComponent(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, (CompositeType) type, anything);
}
}
// potentially we need to handle orphan deletes for one-to-ones here...
if (isLogicalOneToOne(type)) {
// orphan checking
if (style.hasOrphanDelete() && action.deleteOrphans()) {
// value is orphaned if loaded state for this property shows not null
// because it is currently null.
final EntityEntry entry = eventSource.getPersistenceContext().getEntry(parent);
if (entry != null && entry.getStatus() != Status.SAVING) {
final Object loadedValue;
if (componentPathStackDepth == 0) {
// association defined on entity
loadedValue = entry.getLoadedValue(propertyName);
} else {
// association defined on component
// todo : this is currently unsupported because of the fact that
// we do not know the loaded state of this value properly
// and doing so would be very difficult given how components and
// entities are loaded (and how 'loaded state' is put into the
// EntityEntry). Solutions here are to either:
// 1) properly account for components as a 2-phase load construct
// 2) just assume the association was just now orphaned and
// issue the orphan delete. This would require a special
// set of SQL statements though since we do not know the
// orphaned value, something a delete with a subquery to
// match the owner.
// final EntityType entityType = (EntityType) type;
// final String getPropertyPath = composePropertyPath( entityType.getPropertyName() );
loadedValue = null;
}
// entity is managed (without first nulling and manually flushing).
if (child == null || (loadedValue != null && child != loadedValue)) {
final EntityEntry valueEntry = eventSource.getPersistenceContext().getEntry(loadedValue);
// already been flushed. See HHH-7829.
if (valueEntry != null) {
final String entityName = valueEntry.getPersister().getEntityName();
if (LOG.isTraceEnabled()) {
final Serializable id = valueEntry.getPersister().getIdentifier(loadedValue, eventSource);
final String description = MessageHelper.infoString(entityName, id);
LOG.tracev("Deleting orphaned entity instance: {0}", description);
}
if (type.isAssociationType() && ((AssociationType) type).getForeignKeyDirection().equals(ForeignKeyDirection.TO_PARENT)) {
// If FK direction is to-parent, we must remove the orphan *beforeQuery* the queued update(s)
// occur. Otherwise, replacing the association on a managed entity, without manually
// nulling and flushing, causes FK constraint violations.
eventSource.removeOrphanBeforeUpdates(entityName, loadedValue);
} else {
// Else, we must delete afterQuery the updates.
eventSource.delete(entityName, loadedValue, isCascadeDeleteEnabled, new HashSet());
}
}
}
}
}
}
}
use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.
the class Cascade method cascadeComponent.
private static void cascadeComponent(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final int componentPathStackDepth, final Object parent, final Object child, final CompositeType componentType, final Object anything) {
Object[] children = null;
final Type[] types = componentType.getSubtypes();
final String[] propertyNames = componentType.getPropertyNames();
for (int i = 0; i < types.length; i++) {
final CascadeStyle componentPropertyStyle = componentType.getCascadeStyle(i);
final String subPropertyName = propertyNames[i];
if (componentPropertyStyle.doCascade(action)) {
if (children == null) {
// Get children on demand.
children = componentType.getPropertyValues(child, eventSource);
}
cascadeProperty(action, cascadePoint, eventSource, componentPathStackDepth + 1, parent, children[i], types[i], componentPropertyStyle, subPropertyName, anything, false);
}
}
}
use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.
the class AbstractPropertyMapping method initPropertyPaths.
/*protected void initPropertyPaths(
final String path,
final Type type,
final String[] columns,
final String[] formulaTemplates,
final Mapping factory)
throws MappingException {
//addFormulaPropertyPath(path, type, formulaTemplates);
initPropertyPaths(path, type, columns, formulaTemplates, factory);
}*/
protected void initPropertyPaths(final String path, final Type type, String[] columns, String[] columnReaders, String[] columnReaderTemplates, final String[] formulaTemplates, final Mapping factory) throws MappingException {
assert columns != null : "Incoming columns should not be null : " + path;
assert type != null : "Incoming type should not be null : " + path;
if (columns.length != type.getColumnSpan(factory)) {
throw new MappingException("broken column mapping for: " + path + " of: " + getEntityName());
}
if (type.isAssociationType()) {
AssociationType actype = (AssociationType) type;
if (actype.useLHSPrimaryKey()) {
columns = getIdentifierColumnNames();
columnReaders = getIdentifierColumnReaders();
columnReaderTemplates = getIdentifierColumnReaderTemplates();
} else {
String foreignKeyProperty = actype.getLHSPropertyName();
if (foreignKeyProperty != null && !path.equals(foreignKeyProperty)) {
//TODO: this requires that the collection is defined afterQuery the
// referenced property in the mapping file (ok?)
columns = columnsByPropertyPath.get(foreignKeyProperty);
if (columns == null) {
//get em on the second pass!
return;
}
columnReaders = columnReadersByPropertyPath.get(foreignKeyProperty);
columnReaderTemplates = columnReaderTemplatesByPropertyPath.get(foreignKeyProperty);
}
}
}
if (path != null) {
addPropertyPath(path, type, columns, columnReaders, columnReaderTemplates, formulaTemplates);
}
if (type.isComponentType()) {
CompositeType actype = (CompositeType) type;
initComponentPropertyPaths(path, actype, columns, columnReaders, columnReaderTemplates, formulaTemplates, factory);
if (actype.isEmbedded()) {
initComponentPropertyPaths(path == null ? null : StringHelper.qualifier(path), actype, columns, columnReaders, columnReaderTemplates, formulaTemplates, factory);
}
} else if (type.isEntityType()) {
initIdentifierPropertyPaths(path, (EntityType) type, columns, columnReaders, columnReaderTemplates, factory);
}
}
use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.
the class CompositionSingularSubAttributesHelper method getSingularSubAttributes.
private static Iterable<AttributeDefinition> getSingularSubAttributes(final AttributeSource source, final OuterJoinLoadable ownerEntityPersister, final CompositeType compositeType, final String lhsTableName, final String[] lhsColumns) {
return new Iterable<AttributeDefinition>() {
@Override
public Iterator<AttributeDefinition> iterator() {
return new Iterator<AttributeDefinition>() {
private final int numberOfAttributes = compositeType.getSubtypes().length;
private int currentSubAttributeNumber;
private int currentColumnPosition;
@Override
public boolean hasNext() {
return currentSubAttributeNumber < numberOfAttributes;
}
@Override
public AttributeDefinition next() {
final int subAttributeNumber = currentSubAttributeNumber;
currentSubAttributeNumber++;
final String name = compositeType.getPropertyNames()[subAttributeNumber];
final Type type = compositeType.getSubtypes()[subAttributeNumber];
final int columnPosition = currentColumnPosition;
final int columnSpan = type.getColumnSpan(ownerEntityPersister.getFactory());
final String[] subAttributeLhsColumns = ArrayHelper.slice(lhsColumns, columnPosition, columnSpan);
final boolean[] propertyNullability = compositeType.getPropertyNullability();
final boolean nullable = propertyNullability == null || propertyNullability[subAttributeNumber];
currentColumnPosition += columnSpan;
if (type.isAssociationType()) {
final AssociationType aType = (AssociationType) type;
return new AssociationAttributeDefinition() {
@Override
public AssociationKey getAssociationKey() {
return new AssociationKey(lhsTableName, subAttributeLhsColumns);
}
@Override
public AssociationNature getAssociationNature() {
if (type.isAnyType()) {
return AssociationNature.ANY;
} else {
// cannot be a collection
return AssociationNature.ENTITY;
}
}
@Override
public EntityDefinition toEntityDefinition() {
if (getAssociationNature() != AssociationNature.ENTITY) {
throw new WalkingException("Cannot build EntityDefinition from non-entity-typed attribute");
}
return (EntityPersister) aType.getAssociatedJoinable(ownerEntityPersister.getFactory());
}
@Override
public AnyMappingDefinition toAnyDefinition() {
if (getAssociationNature() != AssociationNature.ANY) {
throw new WalkingException("Cannot build AnyMappingDefinition from non-any-typed attribute");
}
// todo : not sure how lazy is propogated into the component for a subattribute of type any
return new StandardAnyTypeDefinition((AnyType) aType, false);
}
@Override
public CollectionDefinition toCollectionDefinition() {
throw new WalkingException("A collection cannot be mapped to a composite ID sub-attribute.");
}
@Override
public FetchStrategy determineFetchPlan(LoadQueryInfluencers loadQueryInfluencers, PropertyPath propertyPath) {
return new FetchStrategy(FetchTiming.IMMEDIATE, FetchStyle.JOIN);
}
@Override
public CascadeStyle determineCascadeStyle() {
return CascadeStyles.NONE;
}
@Override
public HydratedCompoundValueHandler getHydratedCompoundValueExtractor() {
return null;
}
@Override
public String getName() {
return name;
}
@Override
public AssociationType getType() {
return aType;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public AttributeSource getSource() {
return source;
}
};
} else if (type.isComponentType()) {
return new CompositionDefinition() {
@Override
public String getName() {
return name;
}
@Override
public CompositeType getType() {
return (CompositeType) type;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public AttributeSource getSource() {
return source;
}
@Override
public Iterable<AttributeDefinition> getAttributes() {
return CompositionSingularSubAttributesHelper.getSingularSubAttributes(this, ownerEntityPersister, (CompositeType) type, lhsTableName, subAttributeLhsColumns);
}
};
} else {
return new AttributeDefinition() {
@Override
public String getName() {
return name;
}
@Override
public Type getType() {
return type;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public AttributeSource getSource() {
return source;
}
};
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove operation not supported here");
}
};
}
};
}
use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.
the class DefaultRefreshEventListener method evictCachedCollections.
private void evictCachedCollections(Type[] types, Serializable id, EventSource source) throws HibernateException {
for (Type type : types) {
if (type.isCollectionType()) {
CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister(((CollectionType) type).getRole());
if (collectionPersister.hasCache()) {
final CollectionRegionAccessStrategy cache = collectionPersister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(id, collectionPersister, source.getFactory(), source.getTenantIdentifier());
final SoftLock lock = cache.lockItem(source, ck, null);
source.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
cache.unlockItem(session, ck, lock);
}
});
cache.remove(source, ck);
}
} else if (type.isComponentType()) {
CompositeType actype = (CompositeType) type;
evictCachedCollections(actype.getSubtypes(), id, source);
}
}
}
Aggregations