Search in sources :

Example 21 with SmallProject

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

the class EntityManagerJUnitTestSuite method testCollectionAddNewObjectUpdate.

/**
 * Test adding a new object to a collection.
 */
public void testCollectionAddNewObjectUpdate() {
    int id = createEmployee("testCollectionAddNew").getId();
    clearCache();
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Employee employee = em.find(Employee.class, id);
    SmallProject project = new SmallProject();
    employee.getProjects().add(project);
    project.getTeamMembers().add(employee);
    commitTransaction(em);
    // Note: even in this case the entity should still be in the shared cache and database below
    if (em.contains(employee)) {
        verifyObject(project);
        verifyObject(employee);
    }
    clearCache();
    if (em.contains(employee)) {
        verifyObject(project);
        verifyObject(employee);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee)

Example 22 with SmallProject

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

the class EntityManagerJUnitTestSuite method testAddAndDeleteSameObject.

// Bug 346022
public void testAddAndDeleteSameObject() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Employee emp = new Employee();
    emp.setFirstName("A");
    em.persist(emp);
    Project project = new SmallProject();
    project.setDescription("B");
    em.persist(project);
    emp.addProject(project);
    project.addTeamMember(emp);
    em.flush();
    em.clear();
    clearCache();
    try {
        emp = em.find(Employee.class, emp.getId());
        project = em.find(SmallProject.class, project.getId());
        emp.removeProject(project);
        project.removeTeamMember(emp);
        em.flush();
        emp.addProject(project);
        project.addTeamMember(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        project = em.find(SmallProject.class, project.getId());
        assertTrue("Employee projects were not the correct size", emp.getProjects().size() == 1);
        assertTrue("Project did not have the correct number of team members", project.getTeamMembers().size() == 1);
    } finally {
        rollbackTransaction(em);
    }
}
Also used : Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee)

Example 23 with SmallProject

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

the class EntityManagerJUnitTestSuite method createProjectsWithName.

/*this creates project types both with and without a teamLeader if one is passed in, and
   * returns the number of project types created (2x the number of project instances will have been created if a team lead is specified)
   */
protected int createProjectsWithName(String name, Employee teamLeader, EntityManager em) {
    Project p = new Project();
    p.setName(name);
    em.persist(p);
    SmallProject sp = new SmallProject();
    sp.setName(name);
    em.persist(sp);
    LargeProject lp = new LargeProject();
    lp.setName(name);
    em.persist(lp);
    HugeProject hp = new HugeProject();
    hp.setName(name);
    em.persist(hp);
    SpecialHugeProject shp = new SpecialHugeProject();
    shp.setName(name);
    em.persist(shp);
    if (teamLeader != null) {
        Project pWTL = new Project();
        pWTL.setName(name);
        pWTL.setTeamLeader(teamLeader);
        em.persist(pWTL);
        SmallProject spWTL = new SmallProject();
        spWTL.setName(name);
        spWTL.setTeamLeader(teamLeader);
        em.persist(spWTL);
        LargeProject lpWTL = new LargeProject();
        lpWTL.setName(name);
        lpWTL.setTeamLeader(teamLeader);
        em.persist(lpWTL);
        HugeProject hpWTL = new HugeProject();
        hpWTL.setName(name);
        hpWTL.setTeamLeader(teamLeader);
        em.persist(hpWTL);
        SpecialHugeProject shpWTL = new SpecialHugeProject();
        shpWTL.setName(name);
        shpWTL.setTeamLeader(teamLeader);
        em.persist(shpWTL);
    }
    // change this if this method creates a new project subclass type.
    return 5;
}
Also used : Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 24 with SmallProject

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

the class QueryCastTestSuite method testDowncastInSelect.

public void testDowncastInSelect() {
    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);
        em.flush();
        clearCache();
        em.clear();
        ExpressionBuilder builder = new ExpressionBuilder(Project.class);
        ReportQuery rq = new ReportQuery(Project.class, builder);
        rq.addAttribute("project", builder.treat(LargeProject.class).get("budget"));
        rq.setSelectionCriteria(builder.type().equal(LargeProject.class));
        List resultList = (List) ((JpaEntityManager) em.getDelegate()).getActiveSession().executeQuery(rq);
        assertTrue("Incorrect results returned", resultList.size() == 1);
        // Test equivalent JPQL as well
        Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
        List JPQLresultList = query.getResultList();
        assertTrue("Incorrect results returned", JPQLresultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ReportQuery(org.eclipse.persistence.queries.ReportQuery) List(java.util.List) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 25 with SmallProject

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

the class QueryCastTestSuite method testTreatInSelect.

public void testTreatInSelect() {
    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);
        em.flush();
        clearCache();
        em.clear();
        Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
        List resultList = query.getResultList();
        assertTrue("Incorrect results returned, expected 1, received: " + resultList.size(), resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Aggregations

SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)30 EntityManager (jakarta.persistence.EntityManager)27 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)21 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)19 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)17 List (java.util.List)13 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)11 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)11 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)9 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)9 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)8 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)8 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)8 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)7 Query (jakarta.persistence.Query)6 ArrayList (java.util.ArrayList)6 ReportQuery (org.eclipse.persistence.queries.ReportQuery)6 Vector (java.util.Vector)5 Expression (org.eclipse.persistence.expressions.Expression)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)5