use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.
the class EntityManagerSetupImpl method preInitializeCanonicalMetamodel.
/**
* INTERNAL:
* First phase of canonical metamodel initialization. For each class the metamodel is aware of, check
* for a canonical metamodel class and initialize each attribute in it with a proxy that can cause the
* rest of the metamodel population. Attributes are found reflectively rather than through the metamodel
* to avoid having to further initialize the metamodel.
*/
public void preInitializeCanonicalMetamodel(EntityManagerFactoryImpl factory) {
// 338837: verify that the collection is not empty - this would mean entities did not make it into the search path
if (null == metaModel.getManagedTypes() || metaModel.getManagedTypes().isEmpty()) {
getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_type_collection_empty");
}
for (ManagedType manType : metaModel.getManagedTypes()) {
boolean classInitialized = false;
String className = MetadataHelper.getQualifiedCanonicalName(((ManagedTypeImpl) manType).getJavaTypeName(), getSession());
try {
Class<?> clazz = this.getSession().getDatasourcePlatform().convertObject(className, ClassConstants.CLASS);
classInitialized = true;
this.getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_found", className);
Field[] fields = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
fields = AccessController.doPrivileged(new PrivilegedGetDeclaredFields(clazz));
} else {
fields = PrivilegedAccessHelper.getDeclaredFields(clazz);
}
for (Field attribute : fields) {
if (Attribute.class.isAssignableFrom(attribute.getType())) {
Object assignedAttribute = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
assignedAttribute = AccessController.doPrivileged(new PrivilegedGetValueFromField(attribute, null));
} else {
assignedAttribute = PrivilegedAccessHelper.getValueFromField(attribute, null);
}
AttributeProxyImpl proxy = null;
if (assignedAttribute == null) {
if (SingularAttribute.class.isAssignableFrom(attribute.getType())) {
proxy = new SingularAttributeProxyImpl();
} else if (MapAttribute.class.isAssignableFrom(attribute.getType())) {
proxy = new MapAttributeProxyImpl();
} else if (SetAttribute.class.isAssignableFrom(attribute.getType())) {
proxy = new SetAttributeProxyImpl();
} else if (ListAttribute.class.isAssignableFrom(attribute.getType())) {
proxy = new ListAttributeProxyImpl();
} else if (CollectionAttribute.class.isAssignableFrom(attribute.getType())) {
proxy = new CollectionAttributeProxyImpl();
}
if (proxy != null) {
attribute.setAccessible(true);
attribute.set(null, proxy);
}
} else if (assignedAttribute instanceof AttributeProxyImpl) {
proxy = (AttributeProxyImpl) assignedAttribute;
}
if (proxy != null) {
proxy.addFactory(factory);
}
}
}
} catch (PrivilegedActionException pae) {
getSession().logThrowable(SessionLog.FINEST, SessionLog.METAMODEL, pae);
} catch (IllegalAccessException iae) {
getSession().logThrowable(SessionLog.FINEST, SessionLog.METAMODEL, iae);
} catch (ConversionException ce) {
}
if (!classInitialized) {
getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_not_found", className);
}
}
}
use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testManagedType_getDeclaredMap_Type_param_Method.
public void testManagedType_getDeclaredMap_Type_param_Method() {
EntityManager em = null;
boolean expectedIAExceptionThrown = 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_);
MappedSuperclassTypeImpl<Person> msPerson_ = (MappedSuperclassTypeImpl) metamodel.managedType(Person.class);
assertNotNull(msPerson_);
MappedSuperclassTypeImpl<Corporation> msCorporation_ = (MappedSuperclassTypeImpl) metamodel.managedType(Corporation.class);
assertNotNull(msCorporation_);
EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
assertNotNull(entityLocation_);
EntityTypeImpl<Computer> entityComputer_ = (EntityTypeImpl) metamodel.entity(Computer.class);
assertNotNull(entityComputer_);
EntityTypeImpl<HardwareDesigner> entityHardwareDesigner_ = (EntityTypeImpl) metamodel.entity(HardwareDesigner.class);
assertNotNull(entityHardwareDesigner_);
/**
* Return the Map-valued attribute declared by the managed
* type that corresponds to the specified name and Java key
* and value types.
* @param name the name of the represented attribute
* @param keyType the key type of the represented attribute
* @param valueType the value type of the represented attribute
* @return declared MapAttribute of the given name and key
* and value types
* @throws IllegalArgumentException if attribute of the given
* name and type is not declared in the managed type
*/
// <K, V> MapAttribute<X, K, V> getDeclaredMap(String name, Class<K> keyType, Class<V> valueType);
expectedIAExceptionThrown = false;
MapAttribute<? super Manufacturer, ?, ?> anAttribute = entityManufacturer_.getDeclaredMap("hardwareDesignersMap", Integer.class, HardwareDesigner.class);
// verify the default key type is the not the Map key - rather that is is the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// @OneToMany(cascade=ALL, mappedBy="mappedEmployer")
// private Map<String, HardwareDesigner> hardwareDesignersMap;// = new HashMap<String, HardwareDesigner>();
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
// Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present - or generics are set
assertEquals(String.class, keyJavaType);
// assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
expectedIAExceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
}
}
use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testOutOfSpecificationInternalAPI.
public void testOutOfSpecificationInternalAPI() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull(metamodel);
// Actual Test Case
EntityTypeImpl<ArrayProcessor> entityArrayProcessor_ = (EntityTypeImpl) metamodel.entity(ArrayProcessor.class);
assertNotNull(entityArrayProcessor_);
EntityTypeImpl<Processor> entityProcessor_ = (EntityTypeImpl) metamodel.entity(Processor.class);
assertNotNull(entityProcessor_);
// verify all Types have their javaClass set
Collection<TypeImpl<?>> types = ((MetamodelImpl) metamodel).getTypes().values();
assertNotNull(types);
for (TypeImpl type : types) {
assertNotNull(type);
assertNotNull(type.getJavaType());
}
// verify all embeddables are only embeddables
Set<EmbeddableType<?>> embeddables = metamodel.getEmbeddables();
assertNotNull(embeddables);
for (EmbeddableType embeddable : embeddables) {
// This method works only on EntityType
assertNotNull(embeddable);
assertTrue(embeddable instanceof EmbeddableTypeImpl);
}
// verify all entities are only entities
Set<EntityType<?>> entities = metamodel.getEntities();
assertNotNull(entities);
for (EntityType entity : entities) {
// This method works only on EntityType
assertNotNull(entity.getName());
assertTrue(entity instanceof EntityTypeImpl);
}
// Verify all Attributes and their element and declaring/managed types
List<Attribute> allAttributes = ((MetamodelImpl) metamodel).getAllManagedTypeAttributes();
assertNotNull(allAttributes);
assertEquals(METAMODEL_ALL_ATTRIBUTES_SIZE, allAttributes.size());
// Why do we have this function? So we can verify attribute integrity
for (Attribute anAttribute : allAttributes) {
ManagedType declaringType = anAttribute.getDeclaringType();
assertNotNull(declaringType);
Type elementType = null;
if (((AttributeImpl) anAttribute).isPlural()) {
elementType = ((PluralAttributeImpl) anAttribute).getElementType();
} else {
elementType = ((SingularAttributeImpl) anAttribute).getType();
}
assertNotNull("elementType should not be null", elementType);
// Since the javaType may be computed off the elementType - it must not be null or we will get a NPE below
Class<?> javaType = anAttribute.getJavaType();
}
boolean expectedIAExceptionThrown = false;
// Check entity-->entity hierarchy
// Processor:entity (Board boards)
// +--VectorProcessor
Set<Attribute<ArrayProcessor, ?>> entityArrayProcessorDeclaredAttributes = entityArrayProcessor_.getDeclaredAttributes();
assertEquals(1, entityArrayProcessorDeclaredAttributes.size());
// verify getting the attribute directly
Attribute<ArrayProcessor, ?> entityArrayProcessorDeclaredAttribute = entityArrayProcessor_.getDeclaredAttribute("speed");
// verify we do get an IAE on declared type above
try {
Attribute<ArrayProcessor, ?> entityArrayProcessorDeclaredAttributeThatIsNonExistent = entityArrayProcessor_.getDeclaredAttribute("non-existent");
} catch (IllegalArgumentException iae) {
// expecting no exception
expectedIAExceptionThrown = true;
}
assertTrue("Expected thrown IllegalArgumentException", expectedIAExceptionThrown);
// Verify we get an IAE on a type declared above
try {
Attribute<ArrayProcessor, ?> entityArrayProcessorDeclaredAttributeThatIsDeclaredAbove = entityArrayProcessor_.getDeclaredAttribute("id");
} catch (IllegalArgumentException iae) {
// expecting no exception
expectedIAExceptionThrown = true;
}
assertTrue("Expected thrown IllegalArgumentException", expectedIAExceptionThrown);
Set<Attribute<Processor, ?>> entityProcessorDeclaredAttributes = entityProcessor_.getDeclaredAttributes();
assertEquals(3, entityProcessorDeclaredAttributes.size());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
exceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", exceptionThrown);
}
}
use of jakarta.persistence.metamodel.ManagedType 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.ManagedType 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);
}
}
Aggregations