Search in sources :

Example 1 with Metamodel

use of jakarta.persistence.metamodel.Metamodel in project hibernate-orm by hibernate.

the class GetJavaTypeTest method getJavaType.

@Test
public void getJavaType(EntityManagerFactoryScope scope) {
    scope.inEntityManager(entityManager -> {
        Metamodel metaModel = entityManager.getMetamodel();
        if (metaModel != null) {
            ManagedType<Person> mTypeOrder = metaModel.managedType(Person.class);
            assertNotNull(mTypeOrder);
            Attribute<Person, ?> attrib = mTypeOrder.getDeclaredAttribute("age");
            assertNotNull(attrib);
            Class pAttribJavaType = attrib.getJavaType();
            assertEquals("int", pAttribJavaType.getName());
        }
    });
}
Also used : Metamodel(jakarta.persistence.metamodel.Metamodel) Test(org.junit.jupiter.api.Test)

Example 2 with Metamodel

use of jakarta.persistence.metamodel.Metamodel in project hibernate-orm by hibernate.

the class QueryBuilderTest method testEqualityComparisonLiteralConversion.

@Test
public void testEqualityComparisonLiteralConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    Metamodel mm = em.getMetamodel();
    CriteriaQuery<Integer> cquery = cb.createQuery(Integer.class);
    Root<Product> product = cquery.from(Product.class);
    EntityType<Product> Product_ = mm.entity(Product.class);
    cquery.select(cb.toInteger(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
    SqmComparisonPredicate predicate = (SqmComparisonPredicate) cb.equal(product.get(Product_.getSingularAttribute("partNumber", Long.class)), 373767373);
    assertEquals(Long.class, predicate.getLeftHandExpression().getJavaType());
    cquery.where(predicate);
    em.createQuery(cquery).getResultList();
    predicate = (SqmComparisonPredicate) cb.ge(cb.length(product.get(Product_.getSingularAttribute("name", String.class))), 4L);
    assertEquals(Integer.class, predicate.getLeftHandExpression().getJavaType());
    cquery.where(predicate);
    em.createQuery(cquery).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) Product(org.hibernate.orm.test.jpa.metamodel.Product) Metamodel(jakarta.persistence.metamodel.Metamodel) Test(org.junit.Test)

Example 3 with Metamodel

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

the class AdvancedCriteriaQueryTestSuite method testAlternateSelection.

public void testAlternateSelection() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        em.createQuery("select p.teamLeader from Project p where p.name = 'Sales Reporting'").getResultList();
        Metamodel mm = em.getMetamodel();
        CriteriaBuilder qbuilder = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cquery = qbuilder.createQuery(Employee.class);
        Root<Project> spouse = cquery.from(Project.class);
        cquery.where(qbuilder.equal(spouse.get("name"), "Sales Reporting")).select(spouse.<Employee>get("teamLeader"));
        TypedQuery<Employee> tquery = em.createQuery(cquery);
        assertTrue("Did not find the correct leaders of Project Swirly Dirly.", tquery.getResultList().size() > 1);
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 4 with Metamodel

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

the class AdvancedCriteriaQueryTestSuite method testNullRestrictionGetRestriction.

public void testNullRestrictionGetRestriction() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Metamodel mm = em.getMetamodel();
        CriteriaBuilder qbuilder = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cquery = qbuilder.createQuery(Employee.class);
        cquery.getRestriction();
    } catch (NullPointerException ex) {
        fail("'getRestriction()' with null restriction threw NullPointerException");
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 5 with Metamodel

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

Aggregations

Metamodel (jakarta.persistence.metamodel.Metamodel)170 EntityManager (jakarta.persistence.EntityManager)166 EntityTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl)100 Manufacturer (org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer)84 PluralAttribute (jakarta.persistence.metamodel.PluralAttribute)54 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)53 Attribute (jakarta.persistence.metamodel.Attribute)51 MapAttribute (jakarta.persistence.metamodel.MapAttribute)51 CollectionAttribute (jakarta.persistence.metamodel.CollectionAttribute)50 ListAttribute (jakarta.persistence.metamodel.ListAttribute)50 SetAttribute (jakarta.persistence.metamodel.SetAttribute)50 Computer (org.eclipse.persistence.testing.models.jpa.metamodel.Computer)45 GalacticPosition (org.eclipse.persistence.testing.models.jpa.metamodel.GalacticPosition)43 HardwareDesigner (org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner)37 Person (org.eclipse.persistence.testing.models.jpa.metamodel.Person)36 MappedSuperclassTypeImpl (org.eclipse.persistence.internal.jpa.metamodel.MappedSuperclassTypeImpl)34 IdentifiableType (jakarta.persistence.metamodel.IdentifiableType)30 Corporation (org.eclipse.persistence.testing.models.jpa.metamodel.Corporation)28 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)25 ManagedType (jakarta.persistence.metamodel.ManagedType)25