use of org.hibernate.SharedSessionContract in project infinispan by infinispan.
the class AbstractGeneralDataRegionTest method testEvict.
@Test
public void testEvict() throws Exception {
Assume.assumeFalse("Per-key eviction is not supported in 5.3", org.hibernate.Version.getVersionString().startsWith("5.3"));
withSessionFactoriesAndRegions(2, ((sessionFactories, regions) -> {
InfinispanBaseRegion localRegion = regions.get(0);
TestRegion testLocalRegion = TEST_SESSION_ACCESS.fromRegion(localRegion);
InfinispanBaseRegion remoteRegion = regions.get(1);
TestRegion testRemoteRegion = TEST_SESSION_ACCESS.fromRegion(remoteRegion);
Object localSession = sessionFactories.get(0).openSession();
Object remoteSession = sessionFactories.get(1).openSession();
AdvancedCache<?, ?> localCache = localRegion.getCache();
AdvancedCache<?, ?> remoteCache = remoteRegion.getCache();
try {
assertNull("local is clean", testLocalRegion.get(localSession, KEY));
assertNull("remote is clean", testRemoteRegion.get(remoteSession, KEY));
// If this node is backup owner, it will see the update once as originator and then when getting the value from primary
boolean isLocalNodeBackupOwner = extractCacheTopology(localCache).getDistribution(KEY).writeOwners().indexOf(localCache.getCacheManager().getAddress()) > 0;
CountDownLatch insertLatch = new CountDownLatch(isLocalNodeBackupOwner ? 3 : 2);
ExpectingInterceptor.get(localCache).when((ctx, cmd) -> cmd instanceof PutKeyValueCommand).countDown(insertLatch);
ExpectingInterceptor.get(remoteCache).when((ctx, cmd) -> cmd instanceof PutKeyValueCommand).countDown(insertLatch);
Transaction tx = ((SharedSessionContract) localSession).getTransaction();
tx.begin();
try {
testLocalRegion.put(localSession, KEY, VALUE1);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw e;
}
assertTrue(insertLatch.await(2, TimeUnit.SECONDS));
assertEquals(VALUE1, testLocalRegion.get(localSession, KEY));
assertEquals(VALUE1, testRemoteRegion.get(remoteSession, KEY));
CountDownLatch removeLatch = new CountDownLatch(isLocalNodeBackupOwner ? 3 : 2);
ExpectingInterceptor.get(localCache).when((ctx, cmd) -> cmd instanceof RemoveCommand).countDown(removeLatch);
ExpectingInterceptor.get(remoteCache).when((ctx, cmd) -> cmd instanceof RemoveCommand).countDown(removeLatch);
regionEvict(localRegion);
assertTrue(removeLatch.await(2, TimeUnit.SECONDS));
assertEquals(null, testLocalRegion.get(localSession, KEY));
assertEquals(null, testRemoteRegion.get(remoteSession, KEY));
} finally {
((Session) localSession).close();
((Session) remoteSession).close();
ExpectingInterceptor.cleanup(localCache, remoteCache);
}
}));
}
use of org.hibernate.SharedSessionContract in project hibernate-orm by hibernate.
the class EmbeddableMappingTypeImpl method finishInitialization.
private boolean finishInitialization(Component bootDescriptor, CompositeType compositeType, String rootTableExpression, String[] rootTableKeyColumnNames, MappingModelCreationProcess creationProcess) {
// for some reason I cannot get this to work, though only a single test fails - `CompositeElementTest`
// return finishInitialization(
// getNavigableRole(),
// bootDescriptor,
// compositeType,
// rootTableExpression,
// rootTableKeyColumnNames,
// this,
// representationStrategy,
// (name, type) -> {},
// (column, jdbcEnvironment) -> getTableIdentifierExpression(
// column.getValue().getTable(),
// jdbcEnvironment
// ),
// this::addAttribute,
// () -> {
// // We need the attribute mapping types to finish initialization first before we can build the column mappings
// creationProcess.registerInitializationCallback(
// "EmbeddableMappingType(" + getEmbeddedValueMapping().getNavigableRole().getFullPath() + ")#initColumnMappings",
// this::initColumnMappings
// );
// },
// creationProcess
// );
// todo (6.0) - get this ^^ to work, or drop the comment
final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
final TypeConfiguration typeConfiguration = sessionFactory.getTypeConfiguration();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final String baseTableExpression = valueMapping.getContainingTableExpression();
final Type[] subtypes = compositeType.getSubtypes();
int attributeIndex = 0;
int columnPosition = 0;
// Reset the attribute mappings that were added in previous attempts
this.attributeMappings.clear();
for (Property bootPropertyDescriptor : bootDescriptor.getProperties()) {
final AttributeMapping attributeMapping;
final Type subtype = subtypes[attributeIndex];
if (subtype instanceof BasicType) {
final BasicValue basicValue = (BasicValue) bootPropertyDescriptor.getValue();
final Selectable selectable = basicValue.getColumn();
final String containingTableExpression;
final String columnExpression;
if (rootTableKeyColumnNames == null) {
if (selectable.isFormula()) {
columnExpression = selectable.getTemplate(dialect, creationProcess.getSqmFunctionRegistry());
} else {
columnExpression = selectable.getText(dialect);
}
if (selectable instanceof Column) {
containingTableExpression = MappingModelCreationHelper.getTableIdentifierExpression(((Column) selectable).getValue().getTable(), creationProcess);
} else {
containingTableExpression = baseTableExpression;
}
} else {
containingTableExpression = rootTableExpression;
columnExpression = rootTableKeyColumnNames[columnPosition];
}
final String columnDefinition;
final Long length;
final Integer precision;
final Integer scale;
if (selectable instanceof Column) {
Column column = (Column) selectable;
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
} else {
columnDefinition = null;
length = null;
precision = null;
scale = null;
}
attributeMapping = MappingModelCreationHelper.buildBasicAttributeMapping(bootPropertyDescriptor.getName(), valueMapping.getNavigableRole().append(bootPropertyDescriptor.getName()), attributeIndex, bootPropertyDescriptor, this, (BasicType<?>) subtype, containingTableExpression, columnExpression, selectable.isFormula(), selectable.getCustomReadExpression(), selectable.getCustomWriteExpression(), columnDefinition, length, precision, scale, representationStrategy.resolvePropertyAccess(bootPropertyDescriptor), compositeType.getCascadeStyle(attributeIndex), creationProcess);
columnPosition++;
} else if (subtype instanceof AnyType) {
final Any bootValueMapping = (Any) bootPropertyDescriptor.getValue();
final AnyType anyType = (AnyType) subtype;
final PropertyAccess propertyAccess = representationStrategy.resolvePropertyAccess(bootPropertyDescriptor);
final boolean nullable = bootValueMapping.isNullable();
final boolean insertable = bootPropertyDescriptor.isInsertable();
final boolean updateable = bootPropertyDescriptor.isUpdateable();
final boolean includeInOptimisticLocking = bootPropertyDescriptor.isOptimisticLocked();
final CascadeStyle cascadeStyle = compositeType.getCascadeStyle(attributeIndex);
final MutabilityPlan<?> mutabilityPlan;
if (updateable) {
mutabilityPlan = new MutabilityPlan<Object>() {
@Override
public boolean isMutable() {
return true;
}
@Override
public Object deepCopy(Object value) {
if (value == null) {
return null;
}
return anyType.deepCopy(value, creationProcess.getCreationContext().getSessionFactory());
}
@Override
public Serializable disassemble(Object value, SharedSessionContract session) {
throw new NotYetImplementedFor6Exception(getClass());
}
@Override
public Object assemble(Serializable cached, SharedSessionContract session) {
throw new NotYetImplementedFor6Exception(getClass());
}
};
} else {
mutabilityPlan = ImmutableMutabilityPlan.INSTANCE;
}
final StateArrayContributorMetadataAccess attributeMetadataAccess = entityMappingType -> new StateArrayContributorMetadata() {
@Override
public PropertyAccess getPropertyAccess() {
return propertyAccess;
}
@Override
public MutabilityPlan<?> getMutabilityPlan() {
return mutabilityPlan;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public boolean isInsertable() {
return insertable;
}
@Override
public boolean isUpdatable() {
return updateable;
}
@Override
public boolean isIncludedInDirtyChecking() {
// todo (6.0) : do not believe this is correct
return updateable;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return includeInOptimisticLocking;
}
@Override
public CascadeStyle getCascadeStyle() {
return cascadeStyle;
}
};
attributeMapping = new DiscriminatedAssociationAttributeMapping(valueMapping.getNavigableRole().append(bootPropertyDescriptor.getName()), typeConfiguration.getJavaTypeRegistry().getDescriptor(Object.class), this, attributeIndex, attributeMetadataAccess, bootPropertyDescriptor.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE, propertyAccess, bootPropertyDescriptor, anyType, bootValueMapping, creationProcess);
} else if (subtype instanceof CompositeType) {
final CompositeType subCompositeType = (CompositeType) subtype;
final int columnSpan = subCompositeType.getColumnSpan(sessionFactory);
final String subTableExpression;
final String[] subRootTableKeyColumnNames;
if (rootTableKeyColumnNames == null) {
subTableExpression = baseTableExpression;
subRootTableKeyColumnNames = null;
} else {
subTableExpression = rootTableExpression;
subRootTableKeyColumnNames = new String[columnSpan];
System.arraycopy(rootTableKeyColumnNames, columnPosition, subRootTableKeyColumnNames, 0, columnSpan);
}
attributeMapping = MappingModelCreationHelper.buildEmbeddedAttributeMapping(bootPropertyDescriptor.getName(), attributeIndex, bootPropertyDescriptor, this, subCompositeType, subTableExpression, subRootTableKeyColumnNames, representationStrategy.resolvePropertyAccess(bootPropertyDescriptor), compositeType.getCascadeStyle(attributeIndex), creationProcess);
columnPosition += columnSpan;
} else if (subtype instanceof CollectionType) {
final EntityPersister entityPersister = creationProcess.getEntityPersister(bootDescriptor.getOwner().getEntityName());
attributeMapping = MappingModelCreationHelper.buildPluralAttributeMapping(bootPropertyDescriptor.getName(), attributeIndex, bootPropertyDescriptor, entityPersister, representationStrategy.resolvePropertyAccess(bootPropertyDescriptor), compositeType.getCascadeStyle(attributeIndex), compositeType.getFetchMode(attributeIndex), creationProcess);
} else if (subtype instanceof EntityType) {
final EntityPersister entityPersister = creationProcess.getEntityPersister(bootDescriptor.getOwner().getEntityName());
attributeMapping = MappingModelCreationHelper.buildSingularAssociationAttributeMapping(bootPropertyDescriptor.getName(), valueMapping.getNavigableRole().append(bootPropertyDescriptor.getName()), attributeIndex, bootPropertyDescriptor, entityPersister, entityPersister, (EntityType) subtype, getRepresentationStrategy().resolvePropertyAccess(bootPropertyDescriptor), compositeType.getCascadeStyle(attributeIndex), creationProcess);
columnPosition += bootPropertyDescriptor.getColumnSpan();
} else {
throw new MappingException(String.format(Locale.ROOT, "Unable to determine attribute nature : %s#%s", bootDescriptor.getOwner().getEntityName(), bootPropertyDescriptor.getName()));
}
addAttribute(attributeMapping);
attributeIndex++;
}
// We need the attribute mapping types to finish initialization first before we can build the column mappings
creationProcess.registerInitializationCallback("EmbeddableMappingType(" + getEmbeddedValueMapping().getNavigableRole().getFullPath() + ")#initColumnMappings", this::initColumnMappings);
return true;
}
use of org.hibernate.SharedSessionContract in project hibernate-orm by hibernate.
the class AbstractEmbeddableMapping method finishInitialization.
protected static boolean finishInitialization(NavigableRole navigableRole, Component bootDescriptor, CompositeType compositeType, String rootTableExpression, String[] rootTableKeyColumnNames, EmbeddableMappingType declarer, EmbeddableRepresentationStrategy representationStrategy, AttributeTypeValidator attributeTypeValidator, ConcreteTableResolver concreteTableResolver, Consumer<AttributeMapping> attributeConsumer, SuccessfulCompletionCallback completionCallback, MappingModelCreationProcess creationProcess) {
final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
final TypeConfiguration typeConfiguration = sessionFactory.getTypeConfiguration();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final Type[] subtypes = compositeType.getSubtypes();
int attributeIndex = 0;
int columnPosition = 0;
for (Property bootPropertyDescriptor : bootDescriptor.getProperties()) {
final Type subtype = subtypes[attributeIndex];
attributeTypeValidator.check(bootPropertyDescriptor.getName(), subtype);
final PropertyAccess propertyAccess = representationStrategy.resolvePropertyAccess(bootPropertyDescriptor);
final AttributeMapping attributeMapping;
if (subtype instanceof BasicType) {
final BasicValue basicValue = (BasicValue) bootPropertyDescriptor.getValue();
final Selectable selectable = basicValue.getColumn();
final String containingTableExpression;
final String columnExpression;
if (rootTableKeyColumnNames == null) {
if (selectable.isFormula()) {
columnExpression = selectable.getTemplate(dialect, creationProcess.getSqmFunctionRegistry());
} else {
columnExpression = selectable.getText(dialect);
}
if (selectable instanceof Column) {
containingTableExpression = concreteTableResolver.resolve((Column) selectable, jdbcEnvironment);
} else {
containingTableExpression = rootTableExpression;
}
} else {
containingTableExpression = rootTableExpression;
columnExpression = rootTableKeyColumnNames[columnPosition];
}
final String columnDefinition;
final Long length;
final Integer precision;
final Integer scale;
if (selectable instanceof Column) {
Column column = (Column) selectable;
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
} else {
columnDefinition = null;
length = null;
precision = null;
scale = null;
}
attributeMapping = MappingModelCreationHelper.buildBasicAttributeMapping(bootPropertyDescriptor.getName(), navigableRole.append(bootPropertyDescriptor.getName()), attributeIndex, bootPropertyDescriptor, declarer, (BasicType<?>) subtype, containingTableExpression, columnExpression, selectable.isFormula(), selectable.getCustomReadExpression(), selectable.getCustomWriteExpression(), columnDefinition, length, precision, scale, propertyAccess, compositeType.getCascadeStyle(attributeIndex), creationProcess);
columnPosition++;
} else if (subtype instanceof AnyType) {
final Any bootValueMapping = (Any) bootPropertyDescriptor.getValue();
final AnyType anyType = (AnyType) subtype;
final boolean nullable = bootValueMapping.isNullable();
final boolean insertable = bootPropertyDescriptor.isInsertable();
final boolean updateable = bootPropertyDescriptor.isUpdateable();
final boolean includeInOptimisticLocking = bootPropertyDescriptor.isOptimisticLocked();
final CascadeStyle cascadeStyle = compositeType.getCascadeStyle(attributeIndex);
final MutabilityPlan<?> mutabilityPlan;
if (updateable) {
mutabilityPlan = new MutabilityPlan<>() {
@Override
public boolean isMutable() {
return true;
}
@Override
public Object deepCopy(Object value) {
if (value == null) {
return null;
}
return anyType.deepCopy(value, creationProcess.getCreationContext().getSessionFactory());
}
@Override
public Serializable disassemble(Object value, SharedSessionContract session) {
throw new NotYetImplementedFor6Exception(getClass());
}
@Override
public Object assemble(Serializable cached, SharedSessionContract session) {
throw new NotYetImplementedFor6Exception(getClass());
}
};
} else {
mutabilityPlan = ImmutableMutabilityPlan.INSTANCE;
}
final StateArrayContributorMetadataAccess attributeMetadataAccess = entityMappingType -> new StateArrayContributorMetadata() {
@Override
public PropertyAccess getPropertyAccess() {
return propertyAccess;
}
@Override
public MutabilityPlan<?> getMutabilityPlan() {
return mutabilityPlan;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public boolean isInsertable() {
return insertable;
}
@Override
public boolean isUpdatable() {
return updateable;
}
@Override
public boolean isIncludedInDirtyChecking() {
// todo (6.0) : do not believe this is correct
return updateable;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return includeInOptimisticLocking;
}
@Override
public CascadeStyle getCascadeStyle() {
return cascadeStyle;
}
};
attributeMapping = new DiscriminatedAssociationAttributeMapping(navigableRole.append(bootPropertyDescriptor.getName()), typeConfiguration.getJavaTypeRegistry().getDescriptor(Object.class), declarer, attributeIndex, attributeMetadataAccess, bootPropertyDescriptor.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE, propertyAccess, bootPropertyDescriptor, anyType, bootValueMapping, creationProcess);
} else if (subtype instanceof CompositeType) {
final CompositeType subCompositeType = (CompositeType) subtype;
final int columnSpan = subCompositeType.getColumnSpan(sessionFactory);
final String subTableExpression;
final String[] subRootTableKeyColumnNames;
if (rootTableKeyColumnNames == null) {
subTableExpression = rootTableExpression;
subRootTableKeyColumnNames = null;
} else {
subTableExpression = rootTableExpression;
subRootTableKeyColumnNames = new String[columnSpan];
System.arraycopy(rootTableKeyColumnNames, columnPosition, subRootTableKeyColumnNames, 0, columnSpan);
}
attributeMapping = MappingModelCreationHelper.buildEmbeddedAttributeMapping(bootPropertyDescriptor.getName(), attributeIndex, bootPropertyDescriptor, declarer, subCompositeType, subTableExpression, subRootTableKeyColumnNames, propertyAccess, compositeType.getCascadeStyle(attributeIndex), creationProcess);
columnPosition += columnSpan;
} else if (subtype instanceof CollectionType) {
final EntityPersister entityPersister = creationProcess.getEntityPersister(bootDescriptor.getOwner().getEntityName());
attributeMapping = MappingModelCreationHelper.buildPluralAttributeMapping(bootPropertyDescriptor.getName(), attributeIndex, bootPropertyDescriptor, entityPersister, propertyAccess, compositeType.getCascadeStyle(attributeIndex), compositeType.getFetchMode(attributeIndex), creationProcess);
} else if (subtype instanceof EntityType) {
final EntityPersister entityPersister = creationProcess.getEntityPersister(bootDescriptor.getOwner().getEntityName());
attributeMapping = MappingModelCreationHelper.buildSingularAssociationAttributeMapping(bootPropertyDescriptor.getName(), navigableRole.append(bootPropertyDescriptor.getName()), attributeIndex, bootPropertyDescriptor, entityPersister, entityPersister, (EntityType) subtype, representationStrategy.resolvePropertyAccess(bootPropertyDescriptor), compositeType.getCascadeStyle(attributeIndex), creationProcess);
columnPosition += bootPropertyDescriptor.getColumnSpan();
} else {
throw new MappingException(String.format(Locale.ROOT, "Unable to determine attribute nature : %s#%s", bootDescriptor.getOwner().getEntityName(), bootPropertyDescriptor.getName()));
}
attributeConsumer.accept(attributeMapping);
attributeIndex++;
}
completionCallback.success();
return true;
}
Aggregations