Search in sources :

Example 1 with Attribute

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

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

the class FromImpl method join.

@Override
public <T, Y> Join<T, Y> join(String attributeName, JoinType jt) {
    Attribute attribute = this.managedType.getAttribute(attributeName);
    if (attribute.isCollection()) {
        org.eclipse.persistence.expressions.Expression node;
        if (jt.equals(JoinType.INNER)) {
            node = this.currentNode.anyOf(attribute.getName());
        } else if (jt.equals(JoinType.RIGHT)) {
            throw new UnsupportedOperationException(ExceptionLocalization.buildMessage("RIGHT_JOIN_NOT_SUPPORTED"));
        } else {
            node = this.currentNode.anyOfAllowingNone(attribute.getName());
        }
        Join join;
        if (((PluralAttribute) attribute).getElementType().getPersistenceType().equals(PersistenceType.BASIC)) {
            if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
                join = new BasicCollectionJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
                join = new BasicListJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
                join = new BasicSetJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            } else {
                join = new BasicMapJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            }
        } else {
            if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
                join = new CollectionJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
                join = new ListJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
                join = new SetJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            } else {
                join = new MapJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
            }
        }
        this.joins.add(join);
        ((FromImpl) join).isJoin = true;
        return join;
    } else {
        return join(((SingularAttribute) attribute), jt);
    }
}
Also used : CollectionAttribute(jakarta.persistence.metamodel.CollectionAttribute) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Attribute(jakarta.persistence.metamodel.Attribute) MapAttribute(jakarta.persistence.metamodel.MapAttribute) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) ListAttribute(jakarta.persistence.metamodel.ListAttribute) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Bindable(jakarta.persistence.metamodel.Bindable) CollectionJoin(jakarta.persistence.criteria.CollectionJoin) ListJoin(jakarta.persistence.criteria.ListJoin) SetJoin(jakarta.persistence.criteria.SetJoin) MapJoin(jakarta.persistence.criteria.MapJoin) Join(jakarta.persistence.criteria.Join) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute)

Example 3 with Attribute

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

the class FromImpl method get.

@Override
public <Y> Path<Y> get(String attName) {
    Attribute attribute = this.managedType.getAttribute(attName);
    Join join;
    if (attribute.isCollection()) {
        if (!((PluralAttribute) attribute).getElementType().getPersistenceType().equals(PersistenceType.BASIC)) {
            if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
                join = new CollectionJoinImpl<X, Y>(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
                join = new ListJoinImpl<X, Y>(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
                join = new SetJoinImpl<X, Y>(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            } else {
                join = new MapJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            }
        } else {
            if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
                join = new BasicCollectionJoinImpl<X, Y>(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
                join = new BasicListJoinImpl<X, Y>(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            } else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
                join = new BasicSetJoinImpl<X, Y>(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            } else {
                join = new BasicMapJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
            }
        }
    } else {
        Class<Y> clazz = ((SingularAttribute) attribute).getBindableJavaType();
        if (((SingularAttribute) attribute).getType().getPersistenceType().equals(PersistenceType.BASIC)) {
            return new PathImpl<Y>(this, this.metamodel, clazz, this.currentNode.get(attribute.getName()), (Bindable) attribute);
        } else {
            join = new JoinImpl(this, this.metamodel.managedType(clazz), this.metamodel, clazz, this.currentNode.get(attribute.getName()), (Bindable) attribute);
        }
    }
    this.joins.add(join);
    return join;
}
Also used : CollectionAttribute(jakarta.persistence.metamodel.CollectionAttribute) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Attribute(jakarta.persistence.metamodel.Attribute) MapAttribute(jakarta.persistence.metamodel.MapAttribute) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) ListAttribute(jakarta.persistence.metamodel.ListAttribute) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Bindable(jakarta.persistence.metamodel.Bindable) CollectionJoin(jakarta.persistence.criteria.CollectionJoin) ListJoin(jakarta.persistence.criteria.ListJoin) SetJoin(jakarta.persistence.criteria.SetJoin) MapJoin(jakarta.persistence.criteria.MapJoin) Join(jakarta.persistence.criteria.Join) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute)

Example 4 with Attribute

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

the class MetamodelMetamodelTest method testAttribute_getAttribute_of_TransientNonEntityNonMappedSuperclass_SuperclassOfEntity_throws_IAE.

// Test that we are getting an Illegal argument exception when trying to access non-persistent fields from transient classes
public void testAttribute_getAttribute_of_TransientNonEntityNonMappedSuperclass_SuperclassOfEntity_throws_IAE() {
    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);
        // Verify that the non-Entity/non-MappedSuperclass is a BasicType
        Type positionNonEntity = ((MetamodelImpl) metamodel).getType(Position.class);
        assertNotNull(positionNonEntity);
        assertEquals(Type.PersistenceType.BASIC, positionNonEntity.getPersistenceType());
        // Get direct inheriting subclass
        EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
        assertNotNull(entityLocation_);
        // We will be testing that the non-persistent fields
        Attribute anAttributeThatShouldNotHaveBeenInherited = entityLocation_.getAttribute("nonPersistentObject");
        // we should never get to the following line - go directly to catch block
        assertTrue("IllegalArgumentException expected on transient type attribute should not be in subclass for managedType.getAttribute()", exceptionThrown);
    } catch (IllegalArgumentException iae) {
        // iae.printStackTrace();
        exceptionThrown = true;
        assertTrue("IllegalArgumentException expected on transient type attribute should not be in subclass for managedType.getAttribute()", exceptionThrown);
    } finally {
        cleanup(em);
    }
}
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) MetamodelImpl(org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl) 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) EntityTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 5 with Attribute

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

the class MetamodelMetamodelTest method testManagedType_getDeclaredSingularAttribute_on_MappedSuperclass_Method.

public void testManagedType_getDeclaredSingularAttribute_on_MappedSuperclass_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);
        MappedSuperclassTypeImpl<Person> msPerson_ = (MappedSuperclassTypeImpl) metamodel.managedType(Person.class);
        assertNotNull(msPerson_);
        // Test case
        Attribute anAttribute = msPerson_.getSingularAttribute("name");
        assertNotNull(anAttribute);
        assertEquals(PersistentAttributeType.BASIC, anAttribute.getPersistentAttributeType());
        assertFalse(((AttributeImpl) anAttribute).isPlural());
        assertTrue(anAttribute instanceof SingularAttribute);
        assertEquals(String.class, anAttribute.getJavaType());
    } catch (IllegalArgumentException iae) {
        iae.printStackTrace();
        expectedIAExceptionThrown = true;
    } finally {
        cleanup(em);
        assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
    }
}
Also used : SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) 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) MappedSuperclassTypeImpl(org.eclipse.persistence.internal.jpa.metamodel.MappedSuperclassTypeImpl) Metamodel(jakarta.persistence.metamodel.Metamodel) Person(org.eclipse.persistence.testing.models.jpa.metamodel.Person)

Aggregations

Attribute (jakarta.persistence.metamodel.Attribute)54 MapAttribute (jakarta.persistence.metamodel.MapAttribute)54 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)54 CollectionAttribute (jakarta.persistence.metamodel.CollectionAttribute)53 ListAttribute (jakarta.persistence.metamodel.ListAttribute)53 PluralAttribute (jakarta.persistence.metamodel.PluralAttribute)53 EntityManager (jakarta.persistence.EntityManager)51 Metamodel (jakarta.persistence.metamodel.Metamodel)51 SetAttribute (jakarta.persistence.metamodel.SetAttribute)51 EntityTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl)35 Manufacturer (org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer)26 Bindable (jakarta.persistence.metamodel.Bindable)13 GalacticPosition (org.eclipse.persistence.testing.models.jpa.metamodel.GalacticPosition)11 HardwareDesigner (org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner)11 IdentifiableType (jakarta.persistence.metamodel.IdentifiableType)10 Member (java.lang.reflect.Member)10 Computer (org.eclipse.persistence.testing.models.jpa.metamodel.Computer)10 Field (java.lang.reflect.Field)7 Person (org.eclipse.persistence.testing.models.jpa.metamodel.Person)7 MappedSuperclassTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.MappedSuperclassTypeImpl)6