use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class BaseNamingTests method validateCustomerOrders.
protected void validateCustomerOrders(Metadata metadata) {
final Collection collectionBinding = metadata.getCollectionBinding(Customer.class.getName() + ".orders");
assertNotNull(collectionBinding);
validateCustomerOrdersTableName(collectionBinding.getCollectionTable().getQuotedName());
assertEquals(1, collectionBinding.getKey().getColumnSpan());
validateCustomerOrdersKeyColumn((Column) collectionBinding.getKey().getColumnIterator().next());
assertEquals(1, collectionBinding.getElement().getColumnSpan());
validateCustomerOrdersElementColumn((Column) collectionBinding.getElement().getColumnIterator().next());
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class AttributeFactory method determineAttributeMetadata.
/**
* Here is most of the nuts and bolts of this factory, where we interpret the known JPA metadata
* against the known Hibernate metadata and build a descriptor for the attribute.
*
* @param attributeContext The attribute to be described
* @param memberResolver Strategy for how to resolve the member defining the attribute.
* @param <X> The owner type
* @param <Y> The attribute type
*
* @return The attribute description
*/
@SuppressWarnings({ "unchecked" })
private <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(AttributeContext<X> attributeContext, MemberResolver memberResolver) {
LOG.trace("Starting attribute metadata determination [" + attributeContext.getPropertyMapping().getName() + "]");
final Member member = memberResolver.resolveMember(attributeContext);
LOG.trace(" Determined member [" + member + "]");
final Value value = attributeContext.getPropertyMapping().getValue();
final org.hibernate.type.Type type = value.getType();
LOG.trace(" Determined type [name=" + type.getName() + ", class=" + type.getClass().getName() + "]");
if (type.isAnyType()) {
// ANY mappings are currently not supported in the JPA metamodel; see HHH-6589
if (context.isIgnoreUnsupported()) {
return null;
} else {
throw new UnsupportedOperationException("ANY not supported");
}
} else if (type.isAssociationType()) {
// collection or entity
if (type.isEntityType()) {
// entity
return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, determineSingularAssociationAttributeType(member));
}
// collection
if (value instanceof Collection) {
final Collection collValue = (Collection) value;
final Value elementValue = collValue.getElement();
final org.hibernate.type.Type elementType = elementValue.getType();
// First, determine the type of the elements and use that to help determine the
// collection type)
final Attribute.PersistentAttributeType elementPersistentAttributeType;
final Attribute.PersistentAttributeType persistentAttributeType;
if (elementType.isAnyType()) {
if (context.isIgnoreUnsupported()) {
return null;
} else {
throw new UnsupportedOperationException("collection of any not supported yet");
}
}
final boolean isManyToMany = isManyToMany(member);
if (elementValue instanceof Component) {
elementPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
} else if (elementType.isAssociationType()) {
elementPersistentAttributeType = isManyToMany ? Attribute.PersistentAttributeType.MANY_TO_MANY : Attribute.PersistentAttributeType.ONE_TO_MANY;
persistentAttributeType = elementPersistentAttributeType;
} else {
elementPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
}
final Attribute.PersistentAttributeType keyPersistentAttributeType;
// Finally, we determine the type of the map key (if needed)
if (value instanceof Map) {
final Value keyValue = ((Map) value).getIndex();
final org.hibernate.type.Type keyType = keyValue.getType();
if (keyType.isAnyType()) {
if (context.isIgnoreUnsupported()) {
return null;
} else {
throw new UnsupportedOperationException("collection of any not supported yet");
}
}
if (keyValue instanceof Component) {
keyPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
} else if (keyType.isAssociationType()) {
keyPersistentAttributeType = Attribute.PersistentAttributeType.MANY_TO_ONE;
} else {
keyPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
}
} else {
keyPersistentAttributeType = null;
}
return new PluralAttributeMetadataImpl(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, persistentAttributeType, elementPersistentAttributeType, keyPersistentAttributeType);
} else if (value instanceof OneToMany) {
// element value within a o.h.mapping.Collection (see logic branch above)
throw new IllegalArgumentException("HUH???");
// final boolean isManyToMany = isManyToMany( member );
// //one to many with FK => entity
// return new PluralAttributeMetadataImpl(
// attributeContext.getPropertyMapping(),
// attributeContext.getOwnerType(),
// member,
// isManyToMany
// ? Attribute.PersistentAttributeType.MANY_TO_MANY
// : Attribute.PersistentAttributeType.ONE_TO_MANY
// value,
// AttributeContext.TypeStatus.ENTITY,
// Attribute.PersistentAttributeType.ONE_TO_MANY,
// null, null, null
// );
}
} else if (attributeContext.getPropertyMapping().isComposite()) {
// component
return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.EMBEDDED);
} else {
// basic type
return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.BASIC);
}
throw new UnsupportedOperationException("oops, we are missing something: " + attributeContext.getPropertyMapping());
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class CollectionBinderTest method testAssociatedClassException.
@Test
@TestForIssue(jiraKey = "HHH-10106")
public void testAssociatedClassException() throws SQLException {
final Collection collection = mock(Collection.class);
final Map persistentClasses = mock(Map.class);
final XClass collectionType = mock(XClass.class);
final MetadataBuildingContext buildingContext = mock(MetadataBuildingContext.class);
final InFlightMetadataCollector inFly = mock(InFlightMetadataCollector.class);
final PersistentClass persistentClass = mock(PersistentClass.class);
final Table table = mock(Table.class);
when(buildingContext.getMetadataCollector()).thenReturn(inFly);
when(persistentClasses.get(null)).thenReturn(null);
when(collection.getOwner()).thenReturn(persistentClass);
when(collectionType.getName()).thenReturn("List");
when(persistentClass.getTable()).thenReturn(table);
when(table.getName()).thenReturn("Hibernate");
CollectionBinder collectionBinder = new CollectionBinder(false) {
@Override
protected Collection createCollection(PersistentClass persistentClass) {
return null;
}
{
final PropertyHolder propertyHolder = Mockito.mock(PropertyHolder.class);
when(propertyHolder.getClassName()).thenReturn(CollectionBinderTest.class.getSimpleName());
this.propertyName = "abc";
this.propertyHolder = propertyHolder;
}
};
String expectMessage = "Association [abc] for entity [CollectionBinderTest] references unmapped class [List]";
try {
collectionBinder.bindOneToManySecondPass(collection, persistentClasses, null, collectionType, false, false, buildingContext, null);
} catch (MappingException e) {
assertEquals(expectMessage, e.getMessage());
}
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class PutFromLoadStressTestCase method beforeClass.
@BeforeClass
public static void beforeClass() {
// Extra options located in src/test/resources/hibernate.properties
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true").applySetting(Environment.USE_QUERY_CACHE, "true").applySetting(Environment.CACHE_REGION_FACTORY, "org.hibernate.cache.infinispan.InfinispanRegionFactory").applySetting(Environment.JTA_PLATFORM, "org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform").applySetting(Environment.USE_MINIMAL_PUTS, "false").applySetting(Environment.HBM2DDL_AUTO, "create-drop");
StandardServiceRegistry serviceRegistry = ssrb.build();
MetadataSources metadataSources = new MetadataSources(serviceRegistry).addResource("cache/infinispan/functional/Item.hbm.xml").addResource("cache/infinispan/functional/Customer.hbm.xml").addResource("cache/infinispan/functional/Contact.hbm.xml").addAnnotatedClass(Age.class);
Metadata metadata = metadataSources.buildMetadata();
for (PersistentClass entityBinding : metadata.getEntityBindings()) {
if (entityBinding instanceof RootClass) {
((RootClass) entityBinding).setCacheConcurrencyStrategy("transactional");
}
}
for (Collection collectionBinding : metadata.getCollectionBindings()) {
collectionBinding.setCacheConcurrencyStrategy("transactional");
}
sessionFactory = metadata.buildSessionFactory();
tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
}
use of org.hibernate.mapping.Collection in project hibernate-orm by hibernate.
the class SecondLevelCacheStressTestCase method buildMetadata.
private static Metadata buildMetadata(StandardServiceRegistry registry) {
final String cacheStrategy = "transactional";
MetadataSources metadataSources = new MetadataSources(registry);
for (Class entityClass : getAnnotatedClasses()) {
metadataSources.addAnnotatedClass(entityClass);
}
Metadata metadata = metadataSources.buildMetadata();
for (PersistentClass entityBinding : metadata.getEntityBindings()) {
if (!entityBinding.isInherited()) {
((RootClass) entityBinding).setCacheConcurrencyStrategy(cacheStrategy);
}
}
for (Collection collectionBinding : metadata.getCollectionBindings()) {
collectionBinding.setCacheConcurrencyStrategy(cacheStrategy);
}
return metadata;
}
Aggregations