Search in sources :

Example 11 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class CriteriaQueryCastTestSuite method testDowncastManyToManyQueryKey.

public void testDowncastManyToManyQueryKey() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        SmallProject sp = new SmallProject();
        sp.setName("sp1");
        em.persist(sp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        em.flush();
        clearCache();
        em.clear();
        // Query query = em.createQuery("Select e from Employee e join treat(e.projects as LargeProject) p where p.budget > 100");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> root = cq.from(Employee.class);
        Join projectsJoin = root.join("projects");
        Join largeProjectJoin = qb.treat(projectsJoin, LargeProject.class);
        cq.where(qb.gt(largeProjectJoin.get("budget"), 100));
        List resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Join(jakarta.persistence.criteria.Join) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 12 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class CriteriaQueryCastTestSuite method testCastInSubselect.

public void testCastInSubselect() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        LargeProject lp = new LargeProject();
        lp.setBudget(100);
        lp.setName("sp1");
        em.persist(lp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(lp);
        lp.addTeamMember(emp);
        emp.setSalary(10000);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(lp);
        lp.addTeamMember(emp);
        em.persist(emp);
        emp.setSalary(100);
        em.flush();
        clearCache();
        em.clear();
        // Query query = em.createQuery("select e from Employee e where e.salary > (Select max(l.budget) from Employee emp join treat(emp.projects as LargeProject) l)");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> root = cq.from(Employee.class);
        Subquery<Number> sq = cq.subquery(Number.class);
        Root<Employee> sRoot = sq.from(Employee.class);
        Join l = qb.treat(root.join("projects"), LargeProject.class);
        sq.select(qb.max(l.get("budget")));
        cq.where(qb.gt(root.<Number>get("salary"), sq));
        List resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect result size returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Join(jakarta.persistence.criteria.Join) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 13 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class CriteriaQueryCastTestSuite method testDowncastManyToManyExpressionBuilder.

public void testDowncastManyToManyExpressionBuilder() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        SmallProject sp = new SmallProject();
        sp.setName("sp1");
        em.persist(sp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        em.flush();
        clearCache();
        em.clear();
        // JpaQuery query = (JpaQuery)em.createQuery("Select p from Project p where treat (p as LargeProject).budget > 100");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Project> cq = qb.createQuery(Project.class);
        Root<Project> root = cq.from(Project.class);
        Root largeProjectRoot = qb.treat(root, LargeProject.class);
        cq.where(qb.gt(largeProjectRoot.get("budget"), 100));
        List resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Root(jakarta.persistence.criteria.Root) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 14 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject 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 15 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class AdvancedJunitTest method testEL254937.

public void testEL254937() {
    // Should not run in the server - bug 264589
    if (!isOnServer()) {
        EntityManager em = createEntityManager();
        beginTransaction(em);
        LargeProject lp1 = new LargeProject();
        lp1.setName("one");
        em.persist(lp1);
        commitTransaction(em);
        em = createEntityManager();
        beginTransaction(em);
        em.remove(em.find(LargeProject.class, lp1.getId()));
        em.flush();
        JpaEntityManager eclipselinkEm = (JpaEntityManager) em.getDelegate();
        RepeatableWriteUnitOfWork uow = (RepeatableWriteUnitOfWork) eclipselinkEm.getActiveSession();
        // duplicate the beforeCompletion call
        uow.issueSQLbeforeCompletion();
        // commit the transaction
        uow.setShouldTerminateTransaction(true);
        uow.commitTransaction();
        // duplicate the AfterCompletion call.  This should merge, removing the LargeProject from the shared cache
        uow.mergeClonesAfterCompletion();
        em = createEntityManager();
        LargeProject cachedLargeProject = em.find(LargeProject.class, lp1.getId());
        closeEntityManager(em);
        assertTrue("Entity removed during flush was not removed from the shared cache on commit", cachedLargeProject == null);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Aggregations

LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)36 EntityManager (jakarta.persistence.EntityManager)33 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)24 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)23 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)22 List (java.util.List)15 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)14 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)13 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)12 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)11 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)11 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)9 ReportQuery (org.eclipse.persistence.queries.ReportQuery)9 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)9 Query (jakarta.persistence.Query)8 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 PersistenceException (jakarta.persistence.PersistenceException)4 TypedQuery (jakarta.persistence.TypedQuery)3