use of jakarta.persistence.metamodel.Type in project hibernate-orm by hibernate.
the class AttributeFactory method determineAttributeMetadata.
private static <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(AttributeContext<X> attributeContext, MemberResolver memberResolver, MetadataContext context) {
final Property propertyMapping = attributeContext.getPropertyMapping();
final String propertyName = propertyMapping.getName();
LOG.tracef("Starting attribute metadata determination [%s]", propertyName);
final Member member = memberResolver.resolveMember(attributeContext, context);
LOG.tracef(" Determined member [%s]", member);
final Value value = propertyMapping.getValue();
final org.hibernate.type.Type type = value.getType();
LOG.tracef(" Determined type [name=%s, class=%s]", type.getName(), type.getClass().getName());
if (type.isAnyType()) {
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.ANY, context);
} else if (type.isAssociationType()) {
// collection or entity
if (type.isEntityType()) {
// entity
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, determineSingularAssociationClassification(member), context);
}
// collection
if (value instanceof Collection) {
final Collection collValue = (Collection) value;
final Value elementValue = collValue.getElement();
final org.hibernate.type.Type elementType = elementValue.getType();
final boolean isManyToMany = isManyToMany(member);
// First, determine the type of the elements and use that to help determine the
// collection type
final AttributeClassification elementClassification;
final AttributeClassification attributeClassification;
if (elementType.isAnyType()) {
attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
elementClassification = AttributeClassification.ANY;
} else if (elementValue instanceof Component) {
elementClassification = AttributeClassification.EMBEDDED;
attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
} else if (elementType.isAssociationType()) {
elementClassification = isManyToMany ? AttributeClassification.MANY_TO_MANY : AttributeClassification.ONE_TO_MANY;
attributeClassification = elementClassification;
} else {
elementClassification = AttributeClassification.BASIC;
attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
}
final AttributeClassification indexClassification;
// 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()) {
indexClassification = AttributeClassification.ANY;
} else if (keyValue instanceof Component) {
indexClassification = AttributeClassification.EMBEDDED;
} else if (keyType.isAssociationType()) {
indexClassification = AttributeClassification.MANY_TO_ONE;
} else {
indexClassification = AttributeClassification.BASIC;
}
} else if (value instanceof List) {
indexClassification = AttributeClassification.BASIC;
} else {
indexClassification = null;
}
return new PluralAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, attributeClassification, elementClassification, indexClassification, context);
} 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 (propertyMapping.isComposite()) {
// component
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.EMBEDDED, context);
} else {
// basic type
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.BASIC, context);
}
throw new UnsupportedMappingException("oops, we are missing something: " + propertyMapping);
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testAttribute_getAttribute_of_TransientNonEntityNonMappedSuperclass_SuperclassOfEntity_throws_IAE.
// Test that we are getting an Illegal argument exception when trying to access non-persistent fields from transient classes
public void testAttribute_getAttribute_of_TransientNonEntityNonMappedSuperclass_SuperclassOfEntity_throws_IAE() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
// Verify that the non-Entity/non-MappedSuperclass is a BasicType
Type positionNonEntity = ((MetamodelImpl) metamodel).getType(Position.class);
assertNotNull(positionNonEntity);
assertEquals(Type.PersistenceType.BASIC, positionNonEntity.getPersistenceType());
// Get direct inheriting subclass
EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
assertNotNull(entityLocation_);
// We will be testing that the non-persistent fields
Attribute anAttributeThatShouldNotHaveBeenInherited = entityLocation_.getAttribute("nonPersistentObject");
// we should never get to the following line - go directly to catch block
assertTrue("IllegalArgumentException expected on transient type attribute should not be in subclass for managedType.getAttribute()", exceptionThrown);
} catch (IllegalArgumentException iae) {
// iae.printStackTrace();
exceptionThrown = true;
assertTrue("IllegalArgumentException expected on transient type attribute should not be in subclass for managedType.getAttribute()", exceptionThrown);
} finally {
cleanup(em);
}
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testMapAttribute_getKeyType_294811_UC12_DI86_Embedded_keyType_Method.
// This test verifies the workaround for 294811
public void testMapAttribute_getKeyType_294811_UC12_DI86_Embedded_keyType_Method() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
EntityTypeImpl<Computer> entityComputer_ = (EntityTypeImpl) metamodel.entity(Computer.class);
assertNotNull(entityComputer_);
// Actual Test Case
/**
* Return the type representing the key type of the map.
* @return type representing key type
*/
// Type<K> getKeyType();
MapAttribute<? super Computer, ?, ?> anAttribute = entityComputer_.getMap("positionUC12");
// verify the key type is the Map key - not the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// UC12: mapKey defined via generics and is an Embeddable (EmbeddedId) java class defined as an IdClass on the element(value) class
// @OneToMany(mappedBy="computerUC12", cascade=ALL, fetch=EAGER)
// @MapKey // key defaults to an instance of the composite pk class
// private Map<EmbeddedPK, GalacticPosition> positionUC12;
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present or we use generics
assertEquals(EmbeddedPK.class, keyJavaType);
assertNotNull(keyType);
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
exceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", exceptionThrown);
}
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testMapAttribute_getKeyJavaType_UC9_DI86_Embeddable_IdClass_keyType_Method.
// See
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_86:_20090921:_Handle_Embeddable_Type_keyType_in_MapAttributeImpl_constructor
public void testMapAttribute_getKeyJavaType_UC9_DI86_Embeddable_IdClass_keyType_Method() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
EntityTypeImpl<Manufacturer> entityManufacturer_ = (EntityTypeImpl) metamodel.entity(Manufacturer.class);
assertNotNull(entityManufacturer_);
// Actual Test Case
/**
* Return the Java type of the map key.
* @return Java key type
*/
// Class<K> getKeyJavaType();
MapAttribute<? super Manufacturer, ?, ?> anAttribute = entityManufacturer_.getMap("enclosureByBoardMapUC9");
// verify the key type is the Map key - not the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// UC9: no targetEntity, no MapKey, but generics are set (MapKey has an IdClass with an Embeddable)
// @OneToMany(cascade=CascadeType.ALL, mappedBy="mappedManufacturerUC9")
// private Map<Board, Enclosure> enclosureByBoardMapUC9;
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present or we use generics
assertEquals(Board.class, keyJavaType);
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.ENTITY, keyType.getPersistenceType());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
exceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", exceptionThrown);
}
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testIdentifiableType_getIdType_Method.
public void testIdentifiableType_getIdType_Method() {
EntityManager em = null;
boolean expectedIAExceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull(metamodel);
EntityTypeImpl<Manufacturer> entityManufacturer_ = (EntityTypeImpl) metamodel.entity(Manufacturer.class);
assertNotNull(entityManufacturer_);
EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
assertNotNull(entityLocation_);
EntityTypeImpl<Computer> entityComputer_ = (EntityTypeImpl) metamodel.entity(Computer.class);
assertNotNull(entityComputer_);
// Actual Test Case
/**
* Return the type that represents the type of the id.
* @return type of id
*/
// Type<?> getIdType();
// Test EntityType
// Test normal path for an [Embeddable] type via @EmbeddedId
expectedIAExceptionThrown = false;
Type<?> locationIdType = null;
try {
locationIdType = entityLocation_.getIdType();
} catch (IllegalArgumentException iae) {
// expecting no exception
iae.printStackTrace();
expectedIAExceptionThrown = true;
}
assertFalse(expectedIAExceptionThrown);
assertNotNull(locationIdType);
assertEquals(PersistenceType.EMBEDDABLE, locationIdType.getPersistenceType());
assertEquals(EmbeddedPK.class, locationIdType.getJavaType());
// check that the elementType and the owningType (managedType) are set correctly
// See issue 50 where some mapping types were not setting the elementType correctly (this includes aggregate types like Embeddable)
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_50:_20090727:_Handle_all_mapping_types_in_the_SingularAttribute_constructor
// Get the ManagedType and check this SingularAttribute PK
Attribute locationIdAttribute = entityLocation_.getAttribute("primaryKey");
assertNotNull(locationIdAttribute);
assertTrue(locationIdAttribute instanceof SingularAttributeImpl);
assertFalse(locationIdAttribute.isCollection());
// non-spec.
assertFalse(((AttributeImpl) locationIdAttribute).isPlural());
ManagedType locationIdAttributeManagedType = locationIdAttribute.getDeclaringType();
assertEquals(entityLocation_, locationIdAttributeManagedType);
ManagedTypeImpl locationIdAttributeManagedTypeImpl = ((SingularAttributeImpl) locationIdAttribute).getManagedTypeImpl();
assertEquals(locationIdType.getJavaType(), ((SingularAttributeImpl) locationIdAttribute).getBindableJavaType());
assertEquals(Bindable.BindableType.SINGULAR_ATTRIBUTE, ((SingularAttributeImpl) locationIdAttribute).getBindableType());
assertEquals(locationIdType.getJavaType(), locationIdAttribute.getJavaType());
Type embeddableType = ((SingularAttributeImpl) locationIdAttribute).getType();
assertNotNull(embeddableType);
assertNotSame(embeddableType, locationIdAttributeManagedType);
// Test normal path for a [Basic] type
expectedIAExceptionThrown = false;
Type<?> computerIdType = null;
try {
computerIdType = entityComputer_.getIdType();
} catch (IllegalArgumentException iae) {
// expecting no exception
iae.printStackTrace();
expectedIAExceptionThrown = true;
}
assertFalse(expectedIAExceptionThrown);
assertNotNull(computerIdType);
assertEquals(PersistenceType.BASIC, computerIdType.getPersistenceType());
assertEquals(Integer.class, computerIdType.getJavaType());
// Test MappedSuperclassType
// Test normal path for a [Basic] type
expectedIAExceptionThrown = false;
Type<?> personIdType = null;
MappedSuperclassTypeImpl<Person> msPerson_ = (MappedSuperclassTypeImpl) metamodel.managedType(Person.class);
assertNotNull(msPerson_);
MappedSuperclassTypeImpl<Corporation> msCorporation_ = (MappedSuperclassTypeImpl) metamodel.managedType(Corporation.class);
assertNotNull(msCorporation_);
// Verify all types (entities, embeddables, mappedsuperclasses and basic)
// get all 21 types (a non spec function - for testing introspection)
Map<String, TypeImpl<?>> typesMap = ((MetamodelImpl) metamodel).getTypes();
// verify each one
assertNotNull(typesMap);
((MetamodelImpl) metamodel).printAllTypes();
// Note: Since BasicTypes are lazy - loaded into the metamodel-types Map - this test must preceed any test that verifies all BasicType objects like "testIdentifiableType_getIdType_Method"
// You will get a lower number here - if only this single test is run via the Testing Browser
assertEquals(METAMODEL_ALL_TYPES, typesMap.size());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
expectedIAExceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
}
}
Aggregations