Search in sources :

Example 21 with ManagedType

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityType(jakarta.persistence.metamodel.EntityType) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) PersistentAttributeType(jakarta.persistence.metamodel.Attribute.PersistentAttributeType) MappedSuperclassType(jakarta.persistence.metamodel.MappedSuperclassType) EmbeddableType(jakarta.persistence.metamodel.EmbeddableType) PersistenceType(jakarta.persistence.metamodel.Type.PersistenceType) CollectionType(jakarta.persistence.metamodel.PluralAttribute.CollectionType) Type(jakarta.persistence.metamodel.Type) ManagedType(jakarta.persistence.metamodel.ManagedType) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 22 with ManagedType

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityType(jakarta.persistence.metamodel.EntityType) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) PersistentAttributeType(jakarta.persistence.metamodel.Attribute.PersistentAttributeType) MappedSuperclassType(jakarta.persistence.metamodel.MappedSuperclassType) EmbeddableType(jakarta.persistence.metamodel.EmbeddableType) PersistenceType(jakarta.persistence.metamodel.Type.PersistenceType) CollectionType(jakarta.persistence.metamodel.PluralAttribute.CollectionType) Type(jakarta.persistence.metamodel.Type) ManagedType(jakarta.persistence.metamodel.ManagedType) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 23 with ManagedType

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityType(jakarta.persistence.metamodel.EntityType) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) PersistentAttributeType(jakarta.persistence.metamodel.Attribute.PersistentAttributeType) MappedSuperclassType(jakarta.persistence.metamodel.MappedSuperclassType) EmbeddableType(jakarta.persistence.metamodel.EmbeddableType) PersistenceType(jakarta.persistence.metamodel.Type.PersistenceType) CollectionType(jakarta.persistence.metamodel.PluralAttribute.CollectionType) Type(jakarta.persistence.metamodel.Type) ManagedType(jakarta.persistence.metamodel.ManagedType) Computer(org.eclipse.persistence.testing.models.jpa.metamodel.Computer) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 24 with ManagedType

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityType(jakarta.persistence.metamodel.EntityType) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) PersistentAttributeType(jakarta.persistence.metamodel.Attribute.PersistentAttributeType) MappedSuperclassType(jakarta.persistence.metamodel.MappedSuperclassType) EmbeddableType(jakarta.persistence.metamodel.EmbeddableType) PersistenceType(jakarta.persistence.metamodel.Type.PersistenceType) CollectionType(jakarta.persistence.metamodel.PluralAttribute.CollectionType) Type(jakarta.persistence.metamodel.Type) ManagedType(jakarta.persistence.metamodel.ManagedType) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 25 with ManagedType

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);
        }
    }
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) ManagedType(jakarta.persistence.metamodel.ManagedType) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) OptimisticLockException(jakarta.persistence.OptimisticLockException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) RemoteException(java.rmi.RemoteException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityManagerSetupException(org.eclipse.persistence.exceptions.EntityManagerSetupException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PersistenceException(jakarta.persistence.PersistenceException) MalformedURLException(java.net.MalformedURLException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)

Aggregations

ManagedType (jakarta.persistence.metamodel.ManagedType)25 EntityManager (jakarta.persistence.EntityManager)23 Metamodel (jakarta.persistence.metamodel.Metamodel)23 EntityTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl)23 PersistentAttributeType (jakarta.persistence.metamodel.Attribute.PersistentAttributeType)22 EmbeddableType (jakarta.persistence.metamodel.EmbeddableType)22 EntityType (jakarta.persistence.metamodel.EntityType)22 IdentifiableType (jakarta.persistence.metamodel.IdentifiableType)22 MappedSuperclassType (jakarta.persistence.metamodel.MappedSuperclassType)22 CollectionType (jakarta.persistence.metamodel.PluralAttribute.CollectionType)22 Type (jakarta.persistence.metamodel.Type)22 PersistenceType (jakarta.persistence.metamodel.Type.PersistenceType)22 Manufacturer (org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer)18 Computer (org.eclipse.persistence.testing.models.jpa.metamodel.Computer)6 ListAttribute (jakarta.persistence.metamodel.ListAttribute)5 MapAttribute (jakarta.persistence.metamodel.MapAttribute)5 Attribute (jakarta.persistence.metamodel.Attribute)4 CollectionAttribute (jakarta.persistence.metamodel.CollectionAttribute)4 PluralAttribute (jakarta.persistence.metamodel.PluralAttribute)4 SetAttribute (jakarta.persistence.metamodel.SetAttribute)4