Search in sources :

Example 21 with Type

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

the class MetamodelMetamodelTest method testAttribute_isCollection_false_Method.

public void testAttribute_isCollection_false_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<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
        assertNotNull(entityLocation_);
        Type<?> locationIdType = entityLocation_.getIdType();
        assertNotNull(locationIdType);
        assertEquals(PersistenceType.EMBEDDABLE, locationIdType.getPersistenceType());
        assertEquals(EmbeddedPK.class, locationIdType.getJavaType());
        Attribute locationIdAttribute = entityLocation_.getAttribute("primaryKey");
        assertNotNull(locationIdAttribute);
        assertTrue(locationIdAttribute instanceof SingularAttributeImpl);
        /**
         *  Is the attribute collection-valued.
         *  @return boolean indicating whether attribute is
         *          collection-valued
         */
        // boolean isCollection();
        assertFalse(locationIdAttribute.isCollection());
        // non-spec.
        assertFalse(((AttributeImpl) locationIdAttribute).isPlural());
        ManagedType locationIdAttributeManagedType = locationIdAttribute.getDeclaringType();
        assertEquals(entityLocation_, locationIdAttributeManagedType);
        ManagedTypeImpl locationIdAttributeManagedTypeImpl = ((SingularAttributeImpl) locationIdAttribute).getManagedTypeImpl();
        assertEquals(locationIdType.getJavaType(), ((SingularAttributeImpl) locationIdAttribute).getBindableJavaType());
        assertEquals(Bindable.BindableType.SINGULAR_ATTRIBUTE, ((SingularAttributeImpl) locationIdAttribute).getBindableType());
        assertEquals(locationIdType.getJavaType(), locationIdAttribute.getJavaType());
        Type embeddableType = ((SingularAttributeImpl) locationIdAttribute).getType();
        assertNotNull(embeddableType);
        assertNotSame(embeddableType, locationIdAttributeManagedType);
    } catch (IllegalArgumentException iae) {
        iae.printStackTrace();
        exceptionThrown = true;
    } finally {
        cleanup(em);
        assertFalse("An IAE exception should not occur here.", exceptionThrown);
    }
}
Also used : ManagedType(jakarta.persistence.metamodel.ManagedType) 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) GalacticPosition(org.eclipse.persistence.testing.models.jpa.metamodel.GalacticPosition) SingularAttributeImpl(org.eclipse.persistence.internal.jpa.metamodel.SingularAttributeImpl) 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) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) ManagedTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.ManagedTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 22 with Type

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

the class MetamodelMetamodelTest method testMapAttribute_getKeyType_294811_UC13_DI86_Embedded_keyType_Method.

// This test verifies the workaround for 294811
public void testMapAttribute_getKeyType_294811_UC13_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("positionUniUC13");
        // verify the key type is the Map key - not the managedType PK
        Class<?> keyJavaType = anAttribute.getKeyJavaType();
        // UC13:  mapKey defined via generics and is an Embeddable (EmbeddedId) java class defined as an IdClass on the element(value) class
        // However, here we make the owning OneToMany - unidirectional and an effective ManyToMany
        // @MapKey // key defaults to an instance of the composite pk class
        // private Map<EmbeddedPK, GalacticPosition> positionUniUC13;
        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);
    }
}
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 23 with Type

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

the class MetamodelMetamodelTest method testMapAttribute_getKeyType_UC4_Method.

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

use of jakarta.persistence.metamodel.Type 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 Type

use of jakarta.persistence.metamodel.Type 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)

Aggregations

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