Search in sources :

Example 11 with ManagedType

use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.

the class MetamodelMetamodelTest method testMapAttribute_getKeyJavaType_UC7_Method.

public void testMapAttribute_getKeyJavaType_UC7_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("hardwareDesignersMapUC7");
        // verify the key type is the Map key - not the managedType PK
        Class<?> keyJavaType = anAttribute.getKeyJavaType();
        // UC 7: Generics KV set, targetEntity is also set, @MapKey is *(set/unset)
        // @OneToMany(targetEntity=HardwareDesigner.class, cascade=ALL, mappedBy="mappedEmployerUC7")
        // Same as UC1a - that is missing the @MapKey
        // private Map<String, HardwareDesigner> hardwareDesignersMapUC7;
        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 12 with ManagedType

use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.

the class MetamodelMetamodelTest method testMapAttribute_getKeyJavaType_UC4_Method.

public void testMapAttribute_getKeyJavaType_UC4_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("hardwareDesignersMapUC4");
        // verify the key type is the Map key - not the managedType PK
        Class<?> keyJavaType = anAttribute.getKeyJavaType();
        // UC 4: No Generics KV set, @MapKey is present
        // @OneToMany(targetEntity=HardwareDesigner.class, cascade=ALL, mappedBy="mappedEmployerUC4")
        // @MapKey(name="name")
        // private Map hardwareDesignersMapUC4;
        Type keyType = anAttribute.getKeyType();
        // When @MapKey(name="name") is present
        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 13 with ManagedType

use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.

the class MetamodelMetamodelTest method testMapAttribute_getKeyType_UC0_Method.

public void testMapAttribute_getKeyType_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 type representing the key type of the map.
         * @return type representing key type
         */
        // Type<K> getKeyType();
        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 14 with ManagedType

use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.

the class MetamodelMetamodelTest method testMapAttribute_getKeyJavaType_UC2_Method.

public void testMapAttribute_getKeyJavaType_UC2_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("hardwareDesignersMapUC2");
        // verify the key type is the Map key - not the managedType PK
        Class<?> keyJavaType = anAttribute.getKeyJavaType();
        // UC 2: Generics KV set, @MapKey is present
        // @OneToMany(cascade=ALL, mappedBy="mappedEmployerUC2")
        // @MapKey(name="name")
        // private Map<String, HardwareDesigner> hardwareDesignersMapUC2;
        Type keyType = anAttribute.getKeyType();
        // When @MapKey(name="name") is present
        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 15 with ManagedType

use of jakarta.persistence.metamodel.ManagedType in project eclipselink by eclipse-ee4j.

the class MetamodelMetamodelTest method testAttribute_getDeclaringType_Method.

public void testAttribute_getDeclaringType_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_);
        EntityTypeImpl<HardwareDesigner> entityHardwareDesigner_ = (EntityTypeImpl) metamodel.entity(HardwareDesigner.class);
        assertNotNull(entityHardwareDesigner_);
        /**
         *  Return the managed type representing the type in which
         *  the attribute was declared.
         *  @return declaring type
         */
        // ManagedType<X> getDeclaringType();
        // Test case
        Attribute anAttribute = entityManufacturer_.getDeclaredAttribute("hardwareDesignersMapUC4");
        ManagedType aManagedType = anAttribute.getDeclaringType();
        assertEquals(entityManufacturer_, aManagedType);
        assertNotNull(anAttribute);
        assertEquals(PersistentAttributeType.ONE_TO_MANY, anAttribute.getPersistentAttributeType());
    } 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) ManagedType(jakarta.persistence.metamodel.ManagedType) HardwareDesigner(org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Attribute(jakarta.persistence.metamodel.Attribute) MapAttribute(jakarta.persistence.metamodel.MapAttribute) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) SetAttribute(jakarta.persistence.metamodel.SetAttribute) ListAttribute(jakarta.persistence.metamodel.ListAttribute) CollectionAttribute(jakarta.persistence.metamodel.CollectionAttribute) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

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