Search in sources :

Example 11 with InheritancePolicy

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

the class CommitOrderDependencyNode method withAllSubclasses.

// Return an enumeration of all mappings for my descriptor, including those inherited
public Vector withAllSubclasses(CommitOrderDependencyNode node) {
    Vector results = new Vector();
    results.addElement(node);
    if (node.getDescriptor().hasInheritance()) {
        InheritancePolicy policy = node.getDescriptor().getInheritancePolicy();
        // For bug 3019934 replace getChildDescriptors with getAllChildDescriptors.
        List<ClassDescriptor> childDescriptors = new ArrayList<>();
        childDescriptors.addAll(policy.getAllChildDescriptors());
        // Sort Child Descriptors before adding them to related nodes.
        Collections.sort(childDescriptors, new DescriptorCompare());
        for (ClassDescriptor child : childDescriptors) {
            results.add(getOwner().nodeFor(child));
        }
    }
    return results;
}
Also used : InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor)

Example 12 with InheritancePolicy

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

the class EntityManagerJUnitTestSuite method testEnabledPersistNonEntitySubclass.

// bug 237281 - ensure seeting InheritancePolicy to allow non-entity subclasses to be persisted as their
// superclass works
public void testEnabledPersistNonEntitySubclass() {
    EntityManager em = createEntityManager();
    InheritancePolicy ip = getDatabaseSession().getDescriptor(Project.class).getInheritancePolicy();
    boolean describesNonPersistentSubclasses = ip.getDescribesNonPersistentSubclasses();
    ip.setDescribesNonPersistentSubclasses(true);
    beginTransaction(em);
    SuperLargeProject s1 = new SuperLargeProject("Super 1");
    try {
        em.persist(s1);
    } catch (Exception e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        fail("Persist operation was not cascaded to related non-entity, thrown: " + e);
    } finally {
        rollbackTransaction(em);
        ip.setDescribesNonPersistentSubclasses(describesNonPersistentSubclasses);
        closeEntityManager(em);
    }
}
Also used : LargeProject(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.SuperLargeProject) Project(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) 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.composite.advanced.member_3.SuperLargeProject)

Example 13 with InheritancePolicy

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

the class EntityMappingsMergeAdvancedJUnitTestCase method testInheritanceDiscriminatorFieldValue.

public void testInheritanceDiscriminatorFieldValue() {
    InheritancePolicy projectInheritancePolicy = getServerSession().getDescriptor(Project.class).getInheritancePolicy();
    // defined in xml
    DatabaseField classIndicatorField = projectInheritancePolicy.getClassIndicatorField();
    String classIndicatorFieldName = classIndicatorField.getName();
    if (!classIndicatorFieldName.equals("XML_MERGE_PROJ_TYPE")) {
        fail("Wrong classIndicatorField name '" + classIndicatorFieldName + "'");
    }
    Class<?> classIndicatorFieldType = classIndicatorField.getType();
    if (!classIndicatorFieldType.equals(String.class)) {
        fail("Wrong classIndicatorField type '" + classIndicatorFieldType.getName() + "'");
    }
    // defined in xml
    String classIndicatorFieldTableName = classIndicatorField.getTableName();
    if (!classIndicatorFieldTableName.equals("CMP3_XML_MERGE_PROJECT")) {
        fail("Wrong classIndicatorField table '" + classIndicatorFieldTableName + "'");
    }
    Map classNameIndicators = projectInheritancePolicy.getClassNameIndicatorMapping();
    // defined in xml
    String projectIndicator = (String) classNameIndicators.get(packageName + "Project");
    if (!projectIndicator.equals("XML_MERGE_P")) {
        fail("Wrong classIndicatorField value for Project '" + projectIndicator + "'");
    }
    // defaulted in annotations
    String smallProjectIndicator = (String) classNameIndicators.get(packageName + "SmallProject");
    if (!smallProjectIndicator.equals("XMLMergeSmallProject")) {
        fail("Wrong classIndicatorField value for SmallProject '" + smallProjectIndicator + "'");
    }
    String largeProjectIndicator = (String) classNameIndicators.get(packageName + "LargeProject");
    if (!largeProjectIndicator.equals("XMLMergeLargeProject")) {
        fail("Wrong classIndicatorField value for LargeProject '" + largeProjectIndicator + "'");
    }
}
Also used : InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Map(java.util.Map)

Example 14 with InheritancePolicy

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

the class ObjectBuilder method buildObjectsFromCursorInto.

/**
 * 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 buildObjectsFromCursorInto(ReadAllQuery query, List databaseRows, Object domainObjects) {
    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();
        }
        ContainerPolicy policy = query.getContainerPolicy();
        if (policy.shouldAddAll()) {
            List domainObjectsIn = new ArrayList();
            List<AbstractRecord> databaseRowsIn = new ArrayList();
            for (Enumeration iterator = ((Vector) databaseRows).elements(); iterator.hasMoreElements(); ) {
                AbstractRecord databaseRow = (AbstractRecord) iterator.nextElement();
                // 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 (Enumeration iterator = ((Vector) databaseRows).elements(); iterator.hasMoreElements(); ) {
                AbstractRecord databaseRow = (AbstractRecord) iterator.nextElement();
                // 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 : Enumeration(java.util.Enumeration) JoinedAttributeManager(org.eclipse.persistence.internal.queries.JoinedAttributeManager) ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) Collection(java.util.Collection) ThreadCursoredList(org.eclipse.persistence.internal.helper.ThreadCursoredList) ArrayList(java.util.ArrayList) List(java.util.List) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) Vector(java.util.Vector) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 15 with InheritancePolicy

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

the class CascadeLockingPolicy method getParentDescriptorFromInheritancePolicy.

/**
 * Get the descriptor that really represents this object
 * In the case of inheritance, the object may represent a subclass of class the descriptor
 * represents.
 *
 * If there is no InheritancePolicy, we return our parentDescriptor
 * If there is inheritance we will search for a descriptor that represents parentObj and
 * return that descriptor
 */
protected ClassDescriptor getParentDescriptorFromInheritancePolicy(Object parentObj) {
    ClassDescriptor realParentDescriptor = m_parentDescriptor;
    if (realParentDescriptor.hasInheritance()) {
        InheritancePolicy inheritancePolicy = realParentDescriptor.getInheritancePolicy();
        ClassDescriptor childDescriptor = inheritancePolicy.getDescriptor(parentObj.getClass());
        if (childDescriptor != null) {
            realParentDescriptor = childDescriptor;
        }
    }
    return realParentDescriptor;
}
Also used : InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor)

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