Search in sources :

Example 16 with InheritancePolicy

use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.

the class ExpressionQueryMechanism method buildConcreteSelectStatement.

/**
 * Return the appropriate select statement containing the fields in the table.
 * This is used as a second read to a concrete class with subclasses in an abstract-multiple table read.
 */
protected SQLSelectStatement buildConcreteSelectStatement() {
    // 2612538 - the default size of Map (32) is appropriate
    Map clonedExpressions = new IdentityHashMap();
    SQLSelectStatement selectStatement = buildBaseSelectStatement(false, clonedExpressions);
    ClassDescriptor descriptor = getDescriptor();
    InheritancePolicy policy = descriptor.getInheritancePolicy();
    // so if a root or branch (!shouldReadSubclasses means leaf), then it must be appended.
    if (policy.shouldReadSubclasses()) {
        Expression indicatorExpression = null;
        // If the descriptor is a single table branch, then select the whole branch in a single query.
        if (this.query.isReadAllQuery() && policy.hasChildren() && !policy.hasMultipleTableChild()) {
            indicatorExpression = policy.getWithAllSubclassesExpression();
        } else {
            indicatorExpression = policy.getOnlyInstancesExpression();
        }
        if ((indicatorExpression != null) && (selectStatement.getWhereClause() != null)) {
            selectStatement.setWhereClause(selectStatement.getWhereClause().and(indicatorExpression));
        } else if (indicatorExpression != null) {
            selectStatement.setWhereClause((Expression) indicatorExpression.clone());
        }
    }
    selectStatement.setFields(getSelectionFields(selectStatement, false));
    selectStatement.normalize(getSession(), descriptor, clonedExpressions);
    // Allow for joining indexes to be computed to ensure distinct rows.
    if (((ObjectLevelReadQuery) this.query).hasJoining()) {
        ((ObjectLevelReadQuery) this.query).getJoinedAttributeManager().computeJoiningMappingIndexes(false, getSession(), 0);
    }
    return selectStatement;
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) DataExpression(org.eclipse.persistence.internal.expressions.DataExpression) Expression(org.eclipse.persistence.expressions.Expression) IdentityHashMap(java.util.IdentityHashMap) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 17 with InheritancePolicy

use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.

the class ExpressionQueryMechanism method prepareCursorSelectAllRows.

/**
 * Pre-build the SQL statement from the expression.
 */
@Override
public void prepareCursorSelectAllRows() {
    if (getQuery().isReportQuery()) {
        SQLSelectStatement statement = buildReportQuerySelectStatement(false);
        setSQLStatement(statement);
    // For bug 2718118 inheritance with cursors is supported provided there is a read all subclasses view.
    } else if (getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().requiresMultipleTableSubclassRead() && getDescriptor().getInheritancePolicy().hasView()) {
        InheritancePolicy inheritancePolicy = getDescriptor().getInheritancePolicy();
        SQLSelectStatement statement = inheritancePolicy.buildViewSelectStatement((ObjectLevelReadQuery) getQuery());
        setSQLStatement(statement);
    } else {
        setSQLStatement(buildNormalSelectStatement());
    }
    super.prepareCursorSelectAllRows();
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Example 18 with InheritancePolicy

use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testCascadePersistToNonEntitySubclass.

// Glassfish bug 1021 - allow cascading persist operation to non-entities
public void testCascadePersistToNonEntitySubclass() {
    EntityManager em = createEntityManager();
    InheritancePolicy ip = getDatabaseSession().getDescriptor(Project.class).getInheritancePolicy();
    boolean describesNonPersistentSubclasses = ip.getDescribesNonPersistentSubclasses();
    ip.setDescribesNonPersistentSubclasses(true);
    beginTransaction(em);
    Employee emp = new Employee();
    emp.setFirstName("Albert");
    emp.setLastName("Einstein");
    SuperLargeProject s1 = new SuperLargeProject("Super 1");
    Collection projects = new ArrayList();
    projects.add(s1);
    emp.setProjects(projects);
    em.persist(emp);
    try {
        commitTransaction(em);
    } catch (Exception e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        fail("Persist operation was not cascaded to related non-entity, thrown: " + e);
    } finally {
        ip.setDescribesNonPersistentSubclasses(describesNonPersistentSubclasses);
        closeEntityManager(em);
    }
}
Also used : LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) ArrayList(java.util.ArrayList) Collection(java.util.Collection) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EntityExistsException(jakarta.persistence.EntityExistsException) OptimisticLockException(jakarta.persistence.OptimisticLockException) RollbackException(jakarta.persistence.RollbackException) PersistenceException(jakarta.persistence.PersistenceException) QueryException(org.eclipse.persistence.exceptions.QueryException) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)

Example 19 with InheritancePolicy

use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.

the class EntityManagerTLRJUnitTestSuite method testCascadePersistToNonEntitySubclass.

// Glassfish bug 1021 - allow cascading persist operation to non-entities
public void testCascadePersistToNonEntitySubclass() {
    EntityManager em = createEntityManager("fieldaccess");
    InheritancePolicy ip = getServerSession("fieldaccess").getDescriptor(Project.class).getInheritancePolicy();
    boolean describesNonPersistentSubclasses = ip.getDescribesNonPersistentSubclasses();
    ip.setDescribesNonPersistentSubclasses(true);
    beginTransaction(em);
    Employee emp = new Employee();
    emp.setFirstName("Albert");
    emp.setLastName("Einstein");
    SuperLargeProject s1 = new SuperLargeProject("Super 1");
    Collection projects = new ArrayList();
    projects.add(s1);
    emp.setProjects(projects);
    em.persist(emp);
    try {
        commitTransaction(em);
    } catch (Exception e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        fail("Persist operation was not cascaded to related non-entity, thrown: " + e);
    } finally {
        ip.setDescribesNonPersistentSubclasses(describesNonPersistentSubclasses);
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) ArrayList(java.util.ArrayList) Collection(java.util.Collection) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) RollbackException(jakarta.persistence.RollbackException) PersistenceException(jakarta.persistence.PersistenceException) EntityExistsException(jakarta.persistence.EntityExistsException) QueryException(org.eclipse.persistence.exceptions.QueryException) OptimisticLockException(jakarta.persistence.OptimisticLockException) TransactionRequiredException(jakarta.persistence.TransactionRequiredException)

Example 20 with InheritancePolicy

use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method buildObjectsInto.

/**
 * Return a container which contains the instances of the receivers javaClass.
 * Set the fields of the instance to the values stored in the database rows.
 */
public Object buildObjectsInto(ReadAllQuery query, List databaseRows, Object domainObjects) {
    if (databaseRows instanceof ThreadCursoredList) {
        return buildObjectsFromCursorInto(query, databaseRows, domainObjects);
    }
    int size = databaseRows.size();
    if (size > 0) {
        AbstractSession session = query.getSession();
        session.startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
        try {
            InheritancePolicy inheritancePolicy = null;
            if (this.descriptor.hasInheritance()) {
                inheritancePolicy = this.descriptor.getInheritancePolicy();
            }
            boolean isUnitOfWork = session.isUnitOfWork();
            boolean shouldCacheQueryResults = query.shouldCacheQueryResults();
            boolean shouldUseWrapperPolicy = query.shouldUseWrapperPolicy();
            // PERF: Avoid lazy init of join manager if no joining.
            JoinedAttributeManager joinManager = null;
            if (query.hasJoining()) {
                joinManager = query.getJoinedAttributeManager();
            }
            if (this.descriptor.getCachePolicy().shouldPrefetchCacheKeys() && query.shouldMaintainCache() && !query.shouldRetrieveBypassCache()) {
                Object[] pkList = new Object[size];
                for (int i = 0; i < size; ++i) {
                    pkList[i] = extractPrimaryKeyFromRow((AbstractRecord) databaseRows.get(i), session);
                }
                query.setPrefetchedCacheKeys(session.getIdentityMapAccessorInstance().getAllCacheKeysFromIdentityMapWithEntityPK(pkList, descriptor));
            }
            ContainerPolicy policy = query.getContainerPolicy();
            if (policy.shouldAddAll()) {
                List domainObjectsIn = new ArrayList(size);
                List<AbstractRecord> databaseRowsIn = new ArrayList(size);
                for (int index = 0; index < size; index++) {
                    AbstractRecord databaseRow = (AbstractRecord) databaseRows.get(index);
                    // PERF: 1-m joining nulls out duplicate rows.
                    if (databaseRow != null) {
                        domainObjectsIn.add(buildObject(query, databaseRow, joinManager, session, this.descriptor, inheritancePolicy, isUnitOfWork, shouldCacheQueryResults, shouldUseWrapperPolicy));
                        databaseRowsIn.add(databaseRow);
                    }
                }
                policy.addAll(domainObjectsIn, domainObjects, session, databaseRowsIn, query, null, true);
            } else {
                boolean quickAdd = (domainObjects instanceof Collection) && !this.hasWrapperPolicy;
                for (int index = 0; index < size; index++) {
                    AbstractRecord databaseRow = (AbstractRecord) databaseRows.get(index);
                    // PERF: 1-m joining nulls out duplicate rows.
                    if (databaseRow != null) {
                        Object domainObject = buildObject(query, databaseRow, joinManager, session, this.descriptor, inheritancePolicy, isUnitOfWork, shouldCacheQueryResults, shouldUseWrapperPolicy);
                        if (quickAdd) {
                            ((Collection) domainObjects).add(domainObject);
                        } else {
                            policy.addInto(domainObject, domainObjects, session, databaseRow, query, null, true);
                        }
                    }
                }
            }
        } finally {
            session.endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
        }
    }
    return domainObjects;
}
Also used : ThreadCursoredList(org.eclipse.persistence.internal.helper.ThreadCursoredList) JoinedAttributeManager(org.eclipse.persistence.internal.queries.JoinedAttributeManager) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ArrayList(java.util.ArrayList) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) Collection(java.util.Collection) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) ThreadCursoredList(org.eclipse.persistence.internal.helper.ThreadCursoredList) ArrayList(java.util.ArrayList) List(java.util.List) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

InheritancePolicy (org.eclipse.persistence.descriptors.InheritancePolicy)22 Collection (java.util.Collection)7 EntityExistsException (jakarta.persistence.EntityExistsException)6 EntityManager (jakarta.persistence.EntityManager)6 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)6 OptimisticLockException (jakarta.persistence.OptimisticLockException)6 PersistenceException (jakarta.persistence.PersistenceException)6 RollbackException (jakarta.persistence.RollbackException)6 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)6 ArrayList (java.util.ArrayList)6 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)6 QueryException (org.eclipse.persistence.exceptions.QueryException)6 ValidationException (org.eclipse.persistence.exceptions.ValidationException)6 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)6 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)5 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)4 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)4 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)4 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)4 TestProblemException (org.eclipse.persistence.testing.framework.TestProblemException)4