Search in sources :

Example 1 with MapAttribute

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

the class AdvancedJPAJunitTest method testMetamodelMinimalSanityTest.

/**
 * This test performs minimal sanity testing on the advanced JPA model
 * in order to verify metamodel creation.<p>
 * See the metamodel test package suite for full regression tests.
 * See SVN rev# 5124
 * http://fisheye2.atlassian.com/changelog/~author=mobrien/eclipselink/?cs=5124
 */
public void testMetamodelMinimalSanityTest() {
    EntityManager em = createEntityManager();
    // pre-clear metamodel to enable test reentry (SE only - not EE)
    if (!isOnServer()) {
        ((EntityManagerFactoryDelegate) em.getEntityManagerFactory()).setMetamodel(null);
    }
    Metamodel metamodel = em.getMetamodel();
    // get declared attributes
    EntityType<LargeProject> entityLargeProject = metamodel.entity(LargeProject.class);
    Set<Attribute<LargeProject, ?>> declaredAttributes = entityLargeProject.getDeclaredAttributes();
    // instead of a assertEquals(1, size) for future compatibility with changes to Buyer
    assertTrue(declaredAttributes.size() > 0);
    // check that getDeclaredAttribute and getDeclaredAttributes return the same attribute
    Attribute<LargeProject, ?> budgetAttribute = entityLargeProject.getDeclaredAttribute("budget");
    assertNotNull(budgetAttribute);
    Attribute<LargeProject, ?> budgetSingularAttribute = entityLargeProject.getDeclaredSingularAttribute("budget");
    assertNotNull(budgetSingularAttribute);
    assertEquals(budgetSingularAttribute, budgetAttribute);
    assertTrue(declaredAttributes.contains(budgetSingularAttribute));
    // check the type
    Class<?> budgetClass = budgetSingularAttribute.getJavaType();
    // Verify whether we expect a boxed class or not
    assertEquals(double.class, budgetClass);
    // assertEquals(Double.class, budgetClass);
    // Test LargeProject.budget.buyingDays
    // Check an EnumSet on an Entity
    EntityType<Buyer> entityBuyer = metamodel.entity(Buyer.class);
    // public enum Weekdays { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
    // private EnumSet<Weekdays> buyingDays;
    assertNotNull(entityBuyer);
    // check persistence type
    assertEquals(PersistenceType.ENTITY, entityBuyer.getPersistenceType());
    assertEquals(Buyer.class, entityBuyer.getJavaType());
    // verify EnumSet is a SingularAttribute
    Attribute<? super Buyer, ?> buyingDaysAttribute = entityBuyer.getAttribute("buyingDays");
    assertNotNull(buyingDaysAttribute);
    // Check persistent attribute type
    assertEquals(PersistentAttributeType.BASIC, buyingDaysAttribute.getPersistentAttributeType());
    // Non-spec check on the attribute impl type
    // EnumSet is not a Set in the Metamodel - it is a treated as a BasicType single object (SingularAttributeType)
    // BasicTypeImpl@8980685:EnumSet [ javaType: class java.util.EnumSet]
    assertFalse(((SingularAttributeImpl) buyingDaysAttribute).isPlural());
    BindableType buyingDaysElementBindableType = ((SingularAttributeImpl) buyingDaysAttribute).getBindableType();
    assertEquals(BindableType.SINGULAR_ATTRIBUTE, buyingDaysElementBindableType);
    SingularAttribute<? super Buyer, EnumSet> buyingDaysSingularAttribute = entityBuyer.getSingularAttribute("buyingDays", EnumSet.class);
    assertNotNull(buyingDaysSingularAttribute);
    assertFalse(buyingDaysSingularAttribute.isCollection());
    // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_74:_20090909:_Implement_IdentifiableType.hasSingleIdAttribute.28.29
    // Check for Id that exists
    boolean expectedIAExceptionThrown = false;
    boolean hasSingleIdAttribute = false;
    try {
        hasSingleIdAttribute = entityBuyer.hasSingleIdAttribute();
    } catch (IllegalArgumentException iae) {
        // iae.printStackTrace();
        expectedIAExceptionThrown = true;
    }
    assertFalse(expectedIAExceptionThrown);
    assertTrue(hasSingleIdAttribute);
    // Verify that the BasicMap Buyer.creditCards is picked up properly
    // * @param <X> The type the represented Map belongs to
    // * @param <K> The type of the key of the represented Map
    // * @param <V> The type of the value of the represented Map
    // public class MapAttributeImpl<X, K, V> extends PluralAttributeImpl<X, java.util.Map<K, V>, V>
    Attribute<? super Buyer, ?> buyerCreditCards = entityBuyer.getAttribute("creditCards");
    assertNotNull(buyerCreditCards);
    assertTrue(buyerCreditCards.isCollection());
    assertTrue(buyerCreditCards instanceof MapAttributeImpl);
    MapAttribute<? super Buyer, ?, ?> buyerCreditCardsMap = entityBuyer.getMap("creditCards");
    // Verify owning type
    assertNotNull(buyerCreditCardsMap);
    assertEquals(entityBuyer, buyerCreditCardsMap.getDeclaringType());
    // Verify Map Key
    assertEquals(String.class, buyerCreditCardsMap.getKeyJavaType());
    // Verify Map Value
    assertEquals(Long.class, buyerCreditCardsMap.getElementType().getJavaType());
}
Also used : EntityManagerFactoryDelegate(org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate) MapAttribute(jakarta.persistence.metamodel.MapAttribute) Attribute(jakarta.persistence.metamodel.Attribute) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) MapAttributeImpl(org.eclipse.persistence.internal.jpa.metamodel.MapAttributeImpl) EnumSet(java.util.EnumSet) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) Buyer(org.eclipse.persistence.testing.models.jpa.advanced.Buyer) BindableType(jakarta.persistence.metamodel.Bindable.BindableType) SingularAttributeImpl(org.eclipse.persistence.internal.jpa.metamodel.SingularAttributeImpl) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Metamodel(jakarta.persistence.metamodel.Metamodel) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 2 with MapAttribute

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

the class MetamodelMetamodelTest method testMapAtributeElementTypeWhenMapKeySetButNameAttributeIsDefaulted.

/**
 * This test will verify that MapAttribute instance have their elementType set correctly.
 * The elementType corresponds to the 3rd V parameter on the class definition - which is the Map value.
 * {@code MapAttributeImpl<X, K, V>}
 */
public void testMapAtributeElementTypeWhenMapKeySetButNameAttributeIsDefaulted() {
    boolean exceptionThrown = false;
    EntityManager em = null;
    try {
        em = privateTestSetup();
        assertNotNull(em);
        Metamodel metamodel = em.getMetamodel();
        assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
        EntityType<Manufacturer> entityManufacturer = metamodel.entity(Manufacturer.class);
        assertNotNull(entityManufacturer);
        Attribute hardwareDesignersMap = entityManufacturer.getAttribute("hardwareDesignersMapUC8");
        assertNotNull(hardwareDesignersMap);
        assertTrue(hardwareDesignersMap.isCollection());
        assertTrue(hardwareDesignersMap instanceof MapAttributeImpl);
        MapAttribute<? super Manufacturer, ?, ?> manufactuerHardwareDesignersMap = entityManufacturer.getMap("hardwareDesignersMapUC8");
        // Verify owning type
        assertNotNull(manufactuerHardwareDesignersMap);
        assertEquals(entityManufacturer, manufactuerHardwareDesignersMap.getDeclaringType());
        // Verify Map Key - should be PK of owning type
        assertEquals(Integer.class, manufactuerHardwareDesignersMap.getKeyJavaType());
        // Verify Map Value
        assertEquals(HardwareDesigner.class, manufactuerHardwareDesignersMap.getElementType().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) 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) MapAttributeImpl(org.eclipse.persistence.internal.jpa.metamodel.MapAttributeImpl) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 3 with MapAttribute

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

the class MetamodelMetamodelTest method testManagedType_getDeclaredAttribute_Method.

public void testManagedType_getDeclaredAttribute_Method() {
    EntityManager em = null;
    boolean expectedIAExceptionThrown = 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_);
        // Test case
        Attribute anAttribute = entityManufacturer_.getDeclaredAttribute("hardwareDesignersMapUC4");
        assertNotNull(anAttribute);
        assertEquals(PersistentAttributeType.ONE_TO_MANY, anAttribute.getPersistentAttributeType());
        assertTrue(((AttributeImpl) anAttribute).isPlural());
        assertTrue(anAttribute instanceof MapAttribute);
        assertEquals(entityHardwareDesigner_, ((MapAttribute) anAttribute).getElementType());
        assertEquals(CollectionType.MAP, ((MapAttribute) anAttribute).getCollectionType());
        assertEquals(String.class, ((MapAttribute) anAttribute).getKeyJavaType());
        assertTrue(((AttributeImpl) anAttribute).isPlural());
        assertTrue(anAttribute instanceof PluralAttribute);
        Bindable aPluralAttribute = entityManufacturer_.getMap("hardwareDesignersMapUC4");
        assertEquals(HardwareDesigner.class, aPluralAttribute.getBindableJavaType());
        assertEquals(Map.class, anAttribute.getJavaType());
        assertEquals(PersistentAttributeType.ONE_TO_MANY, anAttribute.getPersistentAttributeType());
    } catch (IllegalArgumentException iae) {
        iae.printStackTrace();
        expectedIAExceptionThrown = true;
    } finally {
        cleanup(em);
        assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
    }
}
Also used : 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) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Bindable(jakarta.persistence.metamodel.Bindable) MapAttribute(jakarta.persistence.metamodel.MapAttribute) EntityManager(jakarta.persistence.EntityManager) HardwareDesigner(org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 4 with MapAttribute

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

the class MetamodelMetamodelTest method testManagedType_getAttribute_on_Entity_Method.

public void testManagedType_getAttribute_on_Entity_Method() {
    EntityManager em = null;
    boolean expectedIAExceptionThrown = 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_);
        // Test case
        Attribute anAttribute = entityManufacturer_.getAttribute("hardwareDesignersMapUC4");
        assertNotNull(anAttribute);
        assertEquals(PersistentAttributeType.ONE_TO_MANY, anAttribute.getPersistentAttributeType());
        assertTrue(((AttributeImpl) anAttribute).isPlural());
        assertTrue(anAttribute instanceof MapAttribute);
        assertEquals(entityHardwareDesigner_, ((MapAttribute) anAttribute).getElementType());
        assertEquals(CollectionType.MAP, ((MapAttribute) anAttribute).getCollectionType());
        assertEquals(String.class, ((MapAttribute) anAttribute).getKeyJavaType());
        assertTrue(((AttributeImpl) anAttribute).isPlural());
        assertTrue(anAttribute instanceof PluralAttribute);
        Bindable aPluralAttribute = entityManufacturer_.getMap("hardwareDesignersMapUC4");
        assertEquals(HardwareDesigner.class, aPluralAttribute.getBindableJavaType());
        assertEquals(Map.class, anAttribute.getJavaType());
        assertEquals(PersistentAttributeType.ONE_TO_MANY, anAttribute.getPersistentAttributeType());
    } catch (IllegalArgumentException iae) {
        iae.printStackTrace();
        expectedIAExceptionThrown = true;
    } finally {
        cleanup(em);
        assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
    }
}
Also used : 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) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Bindable(jakarta.persistence.metamodel.Bindable) MapAttribute(jakarta.persistence.metamodel.MapAttribute) EntityManager(jakarta.persistence.EntityManager) HardwareDesigner(org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 5 with MapAttribute

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

the class MetamodelMetamodelTest method testMapAtributeElementTypeWhenMapKeySetAndNameAttributeSet.

public void testMapAtributeElementTypeWhenMapKeySetAndNameAttributeSet() {
    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);
        EntityType<Manufacturer> entityManufacturer = metamodel.entity(Manufacturer.class);
        assertNotNull(entityManufacturer);
        Attribute hardwareDesignersMap = entityManufacturer.getAttribute("hardwareDesignersMapUC4");
        assertNotNull(hardwareDesignersMap);
        assertTrue(hardwareDesignersMap.isCollection());
        assertTrue(hardwareDesignersMap instanceof MapAttributeImpl);
        MapAttribute<? super Manufacturer, ?, ?> manufactuerHardwareDesignersMap = entityManufacturer.getMap("hardwareDesignersMapUC4");
        // Verify owning type
        assertNotNull(manufactuerHardwareDesignersMap);
        assertEquals(entityManufacturer, manufactuerHardwareDesignersMap.getDeclaringType());
        // Verify Map Key - should not be PK of owning type
        assertEquals(String.class, manufactuerHardwareDesignersMap.getKeyJavaType());
        // Verify Map Value
        assertEquals(HardwareDesigner.class, manufactuerHardwareDesignersMap.getElementType().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) 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) MapAttributeImpl(org.eclipse.persistence.internal.jpa.metamodel.MapAttributeImpl) Manufacturer(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer) Metamodel(jakarta.persistence.metamodel.Metamodel)

Aggregations

EntityManager (jakarta.persistence.EntityManager)6 Attribute (jakarta.persistence.metamodel.Attribute)6 MapAttribute (jakarta.persistence.metamodel.MapAttribute)6 Metamodel (jakarta.persistence.metamodel.Metamodel)6 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)6 CollectionAttribute (jakarta.persistence.metamodel.CollectionAttribute)5 ListAttribute (jakarta.persistence.metamodel.ListAttribute)5 PluralAttribute (jakarta.persistence.metamodel.PluralAttribute)5 SetAttribute (jakarta.persistence.metamodel.SetAttribute)5 Manufacturer (org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer)5 EntityTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl)3 MapAttributeImpl (org.eclipse.persistence.internal.jpa.metamodel.MapAttributeImpl)3 HardwareDesigner (org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner)3 Bindable (jakarta.persistence.metamodel.Bindable)2 BindableType (jakarta.persistence.metamodel.Bindable.BindableType)1 IdentifiableType (jakarta.persistence.metamodel.IdentifiableType)1 BigInteger (java.math.BigInteger)1 EnumSet (java.util.EnumSet)1 EntityManagerFactoryDelegate (org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate)1 MappedSuperclassTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.MappedSuperclassTypeImpl)1