use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class ListMappingTest method testOrderColumnInNormalBiDirectonalModel.
@Test
public void testOrderColumnInNormalBiDirectonalModel() {
Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Order.class).addAnnotatedClass(LineItem.class).buildMetadata();
Collection lineItemsBinding = metadata.getCollectionBindings().iterator().next();
// make sure it was interpreted as a List (aka, as having an OrderColumn at all)
assertThat(lineItemsBinding, instanceOf(org.hibernate.mapping.List.class));
org.hibernate.mapping.List asList = (org.hibernate.mapping.List) lineItemsBinding;
// assert the OrderColumn details
final Column positionColumn = (Column) asList.getIndex().getColumnIterator().next();
assertThat(positionColumn.getName(), equalTo("position"));
// make sure the OrderColumn is part of the collection table
assertTrue(asList.getCollectionTable().containsColumn(positionColumn));
class TargetImpl extends GenerationTargetToStdout {
boolean found = false;
@Override
public void accept(String action) {
super.accept(action);
if (action.matches("^create( (column|row))? table t_line_item.+")) {
if (action.contains("position")) {
found = true;
}
}
}
}
TargetImpl target = new TargetImpl();
new SchemaCreatorImpl(ssr).doCreation(metadata, true, target);
assertTrue(target.found);
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class ModelBinder method createPluralAttribute.
private Property createPluralAttribute(MappingDocument sourceDocument, PluralAttributeSource attributeSource, PersistentClass entityDescriptor) {
final Collection collectionBinding;
if (attributeSource instanceof PluralAttributeSourceListImpl) {
collectionBinding = new org.hibernate.mapping.List(sourceDocument, entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeListSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.List) collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceSetImpl) {
collectionBinding = new Set(sourceDocument, entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeSetSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceMapImpl) {
collectionBinding = new org.hibernate.mapping.Map(sourceDocument, entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeMapSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.Map) collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceBagImpl) {
collectionBinding = new Bag(sourceDocument, entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceIdBagImpl) {
collectionBinding = new IdentifierBag(sourceDocument, entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeIdBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceArrayImpl) {
final PluralAttributeSourceArray arraySource = (PluralAttributeSourceArray) attributeSource;
collectionBinding = new Array(sourceDocument, entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
((Array) collectionBinding).setElementClassName(sourceDocument.qualifyClassName(arraySource.getElementClass()));
registerSecondPass(new PluralAttributeArraySecondPass(sourceDocument, arraySource, (Array) collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourcePrimitiveArrayImpl) {
collectionBinding = new PrimitiveArray(sourceDocument, entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributePrimitiveArraySecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (PrimitiveArray) collectionBinding), sourceDocument);
} else {
throw new AssertionFailure("Unexpected PluralAttributeSource type : " + attributeSource.getClass().getName());
}
sourceDocument.getMetadataCollector().addCollectionBinding(collectionBinding);
final Property attribute = new Property();
attribute.setValue(collectionBinding);
bindProperty(sourceDocument, attributeSource, attribute);
return attribute;
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class TestHelper method createRegions.
public static void createRegions(Metadata metadata, boolean queryRegions, boolean prefixRegions) {
Set<String> names = new HashSet<>();
final CacheManager cacheManager = locateStandardCacheManager();
for (PersistentClass persistentClass : metadata.getEntityBindings()) {
if (persistentClass.getRootClass().isCached()) {
if (!names.add(persistentClass.getRootClass().getCacheRegionName())) {
continue;
}
createCache(cacheManager, persistentClass.getRootClass().getCacheRegionName(), prefixRegions);
}
if (persistentClass.hasNaturalId()) {
if (persistentClass.getNaturalIdCacheRegionName() != null) {
if (!names.add(persistentClass.getNaturalIdCacheRegionName())) {
continue;
}
createCache(cacheManager, persistentClass.getNaturalIdCacheRegionName(), prefixRegions);
}
}
}
for (Collection collection : metadata.getCollectionBindings()) {
if (collection.getCacheRegionName() == null || collection.getCacheConcurrencyStrategy() == null) {
continue;
}
if (!names.add(collection.getCacheRegionName())) {
continue;
}
createCache(cacheManager, collection.getCacheRegionName(), prefixRegions);
}
if (queryRegions) {
createCache(cacheManager, TimestampsRegion.class.getName(), prefixRegions);
createCache(cacheManager, QueryResultsRegion.class.getName(), prefixRegions);
}
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class AbstractPropertyMapping method addPropertyPath.
protected void addPropertyPath(String path, Type type, String[] columns, String[] columnReaders, String[] columnReaderTemplates, String[] formulaTemplates, Mapping factory) {
Type existingType = typesByPropertyPath.get(path);
if (existingType != null || duplicateIncompatiblePaths.contains(path)) {
// If types match or the new type is not an association type, there is nothing for us to do
if (type == existingType || existingType == null || !(type instanceof AssociationType)) {
logDuplicateRegistration(path, existingType, type);
} else if (!(existingType instanceof AssociationType)) {
// Workaround for org.hibernate.cfg.annotations.PropertyBinder.bind() adding a component for *ToOne ids
logDuplicateRegistration(path, existingType, type);
} else {
if (type instanceof AnyType && existingType instanceof AnyType) {
// TODO: not sure how to handle any types. For now we just return and let the first type dictate what type the property has...
} else {
Type commonType = null;
MetadataImplementor metadata = (MetadataImplementor) factory;
if (type instanceof CollectionType && existingType instanceof CollectionType) {
Collection thisCollection = metadata.getCollectionBinding(((CollectionType) existingType).getRole());
Collection otherCollection = metadata.getCollectionBinding(((CollectionType) type).getRole());
if (thisCollection.isSame(otherCollection)) {
logDuplicateRegistration(path, existingType, type);
return;
} else {
logIncompatibleRegistration(path, existingType, type);
}
} else if (type instanceof EntityType && existingType instanceof EntityType) {
EntityType entityType1 = (EntityType) existingType;
EntityType entityType2 = (EntityType) type;
if (entityType1.getAssociatedEntityName().equals(entityType2.getAssociatedEntityName())) {
logDuplicateRegistration(path, existingType, type);
return;
} else {
commonType = getCommonType(metadata, entityType1, entityType2);
}
} else {
logIncompatibleRegistration(path, existingType, type);
}
if (commonType == null) {
duplicateIncompatiblePaths.add(path);
typesByPropertyPath.remove(path);
// Set everything to empty to signal action has to be taken!
// org.hibernate.hql.internal.ast.tree.DotNode.dereferenceEntityJoin() is reacting to this
String[] empty = new String[0];
columnsByPropertyPath.put(path, empty);
columnReadersByPropertyPath.put(path, empty);
columnReaderTemplatesByPropertyPath.put(path, empty);
if (formulaTemplates != null) {
formulaTemplatesByPropertyPath.put(path, empty);
}
} else {
typesByPropertyPath.put(path, commonType);
}
}
}
} else {
typesByPropertyPath.put(path, type);
columnsByPropertyPath.put(path, columns);
columnReadersByPropertyPath.put(path, columnReaders);
columnReaderTemplatesByPropertyPath.put(path, columnReaderTemplates);
if (formulaTemplates != null) {
formulaTemplatesByPropertyPath.put(path, formulaTemplates);
}
}
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class MetamodelImpl method primeSecondLevelCacheRegions.
private void primeSecondLevelCacheRegions(MetadataImplementor mappingMetadata) {
final Map<String, DomainDataRegionConfigImpl.Builder> regionConfigBuilders = new ConcurrentHashMap<>();
for (PersistentClass bootEntityDescriptor : mappingMetadata.getEntityBindings()) {
final AccessType accessType = AccessType.fromExternalName(bootEntityDescriptor.getCacheConcurrencyStrategy());
if (accessType != null) {
if (bootEntityDescriptor.isCached()) {
regionConfigBuilders.computeIfAbsent(bootEntityDescriptor.getRootClass().getCacheRegionName(), DomainDataRegionConfigImpl.Builder::new).addEntityConfig(bootEntityDescriptor, accessType);
}
if (RootClass.class.isInstance(bootEntityDescriptor) && bootEntityDescriptor.hasNaturalId() && bootEntityDescriptor.getNaturalIdCacheRegionName() != null) {
regionConfigBuilders.computeIfAbsent(bootEntityDescriptor.getNaturalIdCacheRegionName(), DomainDataRegionConfigImpl.Builder::new).addNaturalIdConfig((RootClass) bootEntityDescriptor, accessType);
}
}
}
for (Collection collection : mappingMetadata.getCollectionBindings()) {
final AccessType accessType = AccessType.fromExternalName(collection.getCacheConcurrencyStrategy());
if (accessType != null) {
regionConfigBuilders.computeIfAbsent(collection.getCacheRegionName(), DomainDataRegionConfigImpl.Builder::new).addCollectionConfig(collection, accessType);
}
}
final Set<DomainDataRegionConfig> regionConfigs;
if (regionConfigBuilders.isEmpty()) {
regionConfigs = Collections.emptySet();
} else {
regionConfigs = new HashSet<>();
for (DomainDataRegionConfigImpl.Builder builder : regionConfigBuilders.values()) {
regionConfigs.add(builder.build());
}
}
getSessionFactory().getCache().prime(regionConfigs);
}
Aggregations