use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testMapAttribute_getKeyJavaType_UC0_Method.
public void testMapAttribute_getKeyJavaType_UC0_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("hardwareDesignersMap");
// 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);
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, 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.ManagedType in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testMapAttribute_getKeyType_UC8_Method.
public void testMapAttribute_getKeyType_UC8_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 type representing the key type of the map.
* @return type representing key type
*/
// Type<K> getKeyType();
MapAttribute<? super Manufacturer, ?, ?> anAttribute = entityManufacturer_.getMap("hardwareDesignersMapUC8");
// verify the key type is the Map key - not the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// UC 8: Generics KV set, targetEntity not set, @MapKey is set but name attribute is defaulted
// @OneToMany(targetEntity=HardwareDesigner.class, cascade=ALL, mappedBy="mappedEmployerUC8")
// Same as UC1a - that is missing the @MapKey name attribute
// private Map<String, HardwareDesigner> hardwareDesignersMapUC8;
Type keyType = anAttribute.getKeyType();
// When @MapKey is not present or missing name attribute - we default to the PK
assertEquals(Integer.class, keyJavaType);
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, 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.ManagedType in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testMapAttribute_getKeyType_294811_UC10_DI86_Embeddable_IdClass_keyType_Method.
// This test verifies the workaround for 294811
public void testMapAttribute_getKeyType_294811_UC10_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<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("enclosuresUC10");
// verify the key type is the Map key - not the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// UC10: no targetEntity, no MapKey attribute, but generics are set (MapKey has an IdClass with an Embeddable)
// @OneToMany(mappedBy="computerUC10", cascade=ALL, fetch=EAGER)
// @MapKey // key defaults to an instance of the composite pk class
// private Map<EnclosureIdClassPK, Enclosure> enclosuresUC10;
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present or we use generics
assertEquals(EnclosureIdClassPK.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_UC8_Method.
public void testMapAttribute_getKeyJavaType_UC8_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("hardwareDesignersMapUC8");
// verify the key type is the Map key - not the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// UC 8: Generics KV set, targetEntity not set, @MapKey is set but name attribute is defaulted
// @OneToMany(targetEntity=HardwareDesigner.class, cascade=ALL, mappedBy="mappedEmployerUC8")
// Same as UC1a - that is missing the @MapKey name attribute
// private Map<String, HardwareDesigner> hardwareDesignersMapUC8;
Type keyType = anAttribute.getKeyType();
// When @MapKey or generics are not present - we default to the PK
assertEquals(Integer.class, keyJavaType);
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, 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.ManagedType in project eclipselink by eclipse-ee4j.
the class EntityManagerSetupImpl method initializeCanonicalMetamodel.
/**
* INTERNAL:
* Initialize the Canonical Metamodel classes generated by EclipseLink
* @since Java Persistence 2.0
*/
protected void initializeCanonicalMetamodel(Metamodel model) {
// 338837: verify that the collection is not empty - this would mean entities did not make it into the search path
if (null == model.getManagedTypes() || model.getManagedTypes().isEmpty()) {
getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_type_collection_empty");
}
for (ManagedType manType : model.getManagedTypes()) {
boolean classInitialized = false;
String className = MetadataHelper.getQualifiedCanonicalName(manType.getJavaType().getName(), 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);
String fieldName = "";
for (Object attribute : manType.getDeclaredAttributes()) {
try {
fieldName = ((Attribute) attribute).getName();
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
AccessController.doPrivileged(new PrivilegedGetDeclaredField(clazz, fieldName, false)).set(clazz, attribute);
} else {
PrivilegedAccessHelper.getDeclaredField(clazz, fieldName, false).set(clazz, attribute);
}
} catch (NoSuchFieldException nsfe) {
// Ignore fields missing in canonical model (dclarke bug 346106)
} catch (Exception e) {
ValidationException v = ValidationException.invalidFieldForClass(fieldName, clazz);
v.setInternalException(e);
throw v;
}
}
} catch (ConversionException exception) {
}
if (!classInitialized) {
getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_not_found", className);
}
}
}
Aggregations