Search in sources :

Example 1 with IdentifiableType

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

the class MetamodelMetamodelTest method testAttribute_getJavaType_BasicType_Method.

public void testAttribute_getJavaType_BasicType_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);
        /**
         *  Return the Java type of the represented attribute.
         *  @return Java type
         */
        // Class<Y> getJavaType();
        ManagedType<Person> msPerson = metamodel.managedType(Person.class);
        assertNotNull(msPerson);
        assertEquals(Type.PersistenceType.MAPPED_SUPERCLASS, msPerson.getPersistenceType());
        Attribute idAttribute = ((IdentifiableType) msPerson).getDeclaredId(Integer.class);
        assertNotNull(idAttribute);
        assertEquals(Integer.class, idAttribute.getJavaType());
    } 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) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) 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) Metamodel(jakarta.persistence.metamodel.Metamodel) Person(org.eclipse.persistence.testing.models.jpa.metamodel.Person)

Example 2 with IdentifiableType

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

the class MetamodelMetamodelTest method testAttribute_getJavaMember_BasicType_on_MappedSuperclass_Method.

public void testAttribute_getJavaMember_BasicType_on_MappedSuperclass_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);
        ManagedType<Person> msPerson = metamodel.managedType(Person.class);
        assertNotNull(msPerson);
        assertEquals(Type.PersistenceType.MAPPED_SUPERCLASS, msPerson.getPersistenceType());
        /**
         *  Return the java.lang.reflect.Member for the represented
         *  attribute.
         *  @return corresponding java.lang.reflect.Member
         */
        // java.lang.reflect.Member getJavaMember();
        Attribute idAttribute = ((IdentifiableType) msPerson).getDeclaredId(Integer.class);
        assertNotNull(idAttribute);
        assertEquals(Integer.class, idAttribute.getJavaType());
        // For primitive and basic types - we should not return null - the attributeAccessor on the MappedSuperclass is not initialized - see
        // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_95:_20091017:_Attribute.getJavaMember.28.29_returns_null_for_a_BasicType_on_a_MappedSuperclass_because_of_an_uninitialized_accessor
        Member aMember = idAttribute.getJavaMember();
        assertNotNull("id java member is null", aMember);
        assertTrue("id attribute java member is not an instance of Field", aMember instanceof Field);
        assertEquals("id", aMember.getName());
        assertNotNull(((Field) aMember).getType());
        assertEquals(Integer.class.getName(), ((Field) aMember).getType().getName());
    } catch (IllegalArgumentException iae) {
        iae.printStackTrace();
        exceptionThrown = true;
    } finally {
        cleanup(em);
        assertFalse("An IAE exception should not occur here.", exceptionThrown);
    }
}
Also used : BigInteger(java.math.BigInteger) Field(java.lang.reflect.Field) EntityManager(jakarta.persistence.EntityManager) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) 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) Metamodel(jakarta.persistence.metamodel.Metamodel) Person(org.eclipse.persistence.testing.models.jpa.metamodel.Person) Member(java.lang.reflect.Member)

Example 3 with IdentifiableType

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

the class MetamodelMetamodelTest method testIdentifiableType_getDeclaredId_variant_execution_attribute_is_declared_above.

public void testIdentifiableType_getDeclaredId_variant_execution_attribute_is_declared_above() {
    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);
        ManagedType<Manufacturer> entityManufacturer_ = metamodel.managedType(Manufacturer.class);
        assertNotNull(entityManufacturer_);
        assertEquals(Type.PersistenceType.ENTITY, entityManufacturer_.getPersistenceType());
        /**
         *  Return the attribute that corresponds to the id attribute
         *  declared by the entity or mapped superclass.
         *  @param type  the type of the represented declared id attribute
         *  @return declared id attribute
         *  @throws IllegalArgumentException if id attribute of the given
         *          type is not declared in the identifiable type or if
         *          the identifiable type has an id class
         */
        // <Y> SingularAttribute<X, Y> getDeclaredId(Class<Y> type);
        // Not declared  - invalid - IAE thrown (actually declared 2 levels above in "Person")
        Attribute idAttribute = ((IdentifiableType) entityManufacturer_).getDeclaredId(Integer.class);
    } catch (IllegalArgumentException iae) {
        // iae.printStackTrace();
        exceptionThrown = true;
    } finally {
        cleanup(em);
        assertTrue("non-declared attribute should throw an IAE", exceptionThrown);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) 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) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 4 with IdentifiableType

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

the class MetamodelMetamodelTest method testAttribute_getName_Method.

public void testAttribute_getName_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);
        /**
         * Return the name of the attribute.
         * @return name
         */
        // String getName();
        ManagedType<Person> msPerson = metamodel.managedType(Person.class);
        assertNotNull(msPerson);
        assertEquals(Type.PersistenceType.MAPPED_SUPERCLASS, msPerson.getPersistenceType());
        Attribute idAttribute = ((IdentifiableType) msPerson).getDeclaredId(Integer.class);
        assertNotNull(idAttribute);
        assertEquals(Integer.class, idAttribute.getJavaType());
        assertEquals("id", idAttribute.getName());
    } 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) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) 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) Metamodel(jakarta.persistence.metamodel.Metamodel) Person(org.eclipse.persistence.testing.models.jpa.metamodel.Person)

Example 5 with IdentifiableType

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

the class MetamodelMetamodelTest method testIdentifiableType_getDeclaredId_normal_execution_attribute_is_declared.

public void testIdentifiableType_getDeclaredId_normal_execution_attribute_is_declared() {
    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);
        ManagedType<Person> msPerson = metamodel.managedType(Person.class);
        assertNotNull(msPerson);
        assertEquals(Type.PersistenceType.MAPPED_SUPERCLASS, msPerson.getPersistenceType());
        /**
         *  Return the attribute that corresponds to the id attribute
         *  declared by the entity or mapped superclass.
         *  @param type  the type of the represented declared id attribute
         *  @return declared id attribute
         *  @throws IllegalArgumentException if id attribute of the given
         *          type is not declared in the identifiable type or if
         *          the identifiable type has an id class
         */
        // <Y> SingularAttribute<X, Y> getDeclaredId(Class<Y> type);
        // declared  - valid
        Attribute idAttribute = ((IdentifiableType) msPerson).getDeclaredId(Integer.class);
        assertNotNull(idAttribute);
        assertEquals(Integer.class, idAttribute.getJavaType());
    // declared and valid
    // *********************************************/
    // FUTURE: Require a version on a MappedSuperclass
    // assertNotNull(msPerson1_.getDeclaredId(Integer.class)); - keep commented
    } 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) IdentifiableType(jakarta.persistence.metamodel.IdentifiableType) 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) Metamodel(jakarta.persistence.metamodel.Metamodel) Person(org.eclipse.persistence.testing.models.jpa.metamodel.Person)

Aggregations

EntityManager (jakarta.persistence.EntityManager)6 Attribute (jakarta.persistence.metamodel.Attribute)6 CollectionAttribute (jakarta.persistence.metamodel.CollectionAttribute)6 IdentifiableType (jakarta.persistence.metamodel.IdentifiableType)6 ListAttribute (jakarta.persistence.metamodel.ListAttribute)6 MapAttribute (jakarta.persistence.metamodel.MapAttribute)6 Metamodel (jakarta.persistence.metamodel.Metamodel)6 PluralAttribute (jakarta.persistence.metamodel.PluralAttribute)6 SetAttribute (jakarta.persistence.metamodel.SetAttribute)6 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)6 Person (org.eclipse.persistence.testing.models.jpa.metamodel.Person)5 BigInteger (java.math.BigInteger)2 Manufacturer (org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer)2 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 EntityTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl)1 MappedSuperclassTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.MappedSuperclassTypeImpl)1 Computer (org.eclipse.persistence.testing.models.jpa.metamodel.Computer)1 Corporation (org.eclipse.persistence.testing.models.jpa.metamodel.Corporation)1 GalacticPosition (org.eclipse.persistence.testing.models.jpa.metamodel.GalacticPosition)1