use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class SubselectFetchWithFormulaTransactSqlTest method checkSubselectWithFormula.
@Test
public void checkSubselectWithFormula() throws Exception {
// as a pre-condition make sure that subselect fetching is enabled for the collection...
Collection collectionBinding = metadata().getCollectionBinding(Name.class.getName() + ".values");
assertThat(collectionBinding.isSubselectLoadable(), is(true));
// Now force the subselect fetch and make sure we do not get SQL errors
Session session = openSession();
session.getTransaction().begin();
List results = session.createCriteria(Name.class).list();
for (Object result : results) {
Name name = (Name) result;
name.getValues().size();
}
session.getTransaction().commit();
session.close();
}
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.getMetadataCollector(), 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.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeSetSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceMapImpl) {
collectionBinding = new org.hibernate.mapping.Map(sourceDocument.getMetadataCollector(), 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.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceIdBagImpl) {
collectionBinding = new IdentifierBag(sourceDocument.getMetadataCollector(), 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.getMetadataCollector(), 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.getMetadataCollector(), 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 DefaultNamingCollectionElementTest method isCollectionColumnPresent.
private void isCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) {
final Collection collection = metadata().getCollectionBinding(collectionOwner + "." + propertyName);
final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
boolean hasDefault = false;
while (columnIterator.hasNext()) {
Column column = (Column) columnIterator.next();
if (columnName.equals(column.getName()))
hasDefault = true;
}
assertTrue("Could not find " + columnName, hasDefault);
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class ElementCollectionTests method testSimpleConvertUsage.
@Test
public void testSimpleConvertUsage() throws MalformedURLException {
// first some assertions of the metamodel
PersistentClass entityBinding = metadata().getEntityBinding(TheEntity.class.getName());
assertNotNull(entityBinding);
Property setAttributeBinding = entityBinding.getProperty("set");
Collection setBinding = (Collection) setAttributeBinding.getValue();
assertTyping(AttributeConverterTypeAdapter.class, setBinding.getElement().getType());
Property mapAttributeBinding = entityBinding.getProperty("map");
IndexedCollection mapBinding = (IndexedCollection) mapAttributeBinding.getValue();
assertTyping(AttributeConverterTypeAdapter.class, mapBinding.getIndex().getType());
assertTyping(AttributeConverterTypeAdapter.class, mapBinding.getElement().getType());
// now lets try to use the model, integration-testing-style!
TheEntity entity = new TheEntity(1);
Session s = openSession();
s.beginTransaction();
s.save(entity);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
TheEntity retrieved = (TheEntity) s.load(TheEntity.class, 1);
assertEquals(1, retrieved.getSet().size());
assertEquals(new ValueType("set_value"), retrieved.getSet().iterator().next());
assertEquals(1, retrieved.getMap().size());
assertEquals(new ValueType("map_value"), retrieved.getMap().get(new ValueType("map_key")));
s.delete(retrieved);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class InFlightMetadataCollectorImpl method processCachingOverrides.
private void processCachingOverrides() {
if (options.getCacheRegionDefinitions() == null) {
return;
}
for (CacheRegionDefinition cacheRegionDefinition : options.getCacheRegionDefinitions()) {
if (cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.ENTITY) {
final PersistentClass entityBinding = getEntityBinding(cacheRegionDefinition.getRole());
if (entityBinding == null) {
throw new HibernateException("Cache override referenced an unknown entity : " + cacheRegionDefinition.getRole());
}
if (!RootClass.class.isInstance(entityBinding)) {
throw new HibernateException("Cache override referenced a non-root entity : " + cacheRegionDefinition.getRole());
}
((RootClass) entityBinding).setCacheRegionName(cacheRegionDefinition.getRegion());
((RootClass) entityBinding).setCacheConcurrencyStrategy(cacheRegionDefinition.getUsage());
((RootClass) entityBinding).setLazyPropertiesCacheable(cacheRegionDefinition.isCacheLazy());
} else if (cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.COLLECTION) {
final Collection collectionBinding = getCollectionBinding(cacheRegionDefinition.getRole());
if (collectionBinding == null) {
throw new HibernateException("Cache override referenced an unknown collection role : " + cacheRegionDefinition.getRole());
}
collectionBinding.setCacheRegionName(cacheRegionDefinition.getRegion());
collectionBinding.setCacheConcurrencyStrategy(cacheRegionDefinition.getUsage());
}
}
}
Aggregations