Search in sources :

Example 46 with DatabaseSessionImpl

use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.

the class EntityMappingsAdvancedJUnitTestCase method testSexObjectTypeConverterDefaultValue.

/**
 * Verifies a default object type value is set from metadata processing.
 */
public void testSexObjectTypeConverterDefaultValue() {
    DatabaseSessionImpl session = getDatabaseSession();
    ClassDescriptor employeeDescriptor = session.getDescriptor(Employee.class);
    DirectToFieldMapping mapping = (DirectToFieldMapping) employeeDescriptor.getMappingForAttributeName("gender");
    assertNotNull("Gender mapping from Employee not found.", mapping);
    assertTrue("No object type converter found on the gender mapping.", ObjectTypeConverter.class.isAssignableFrom(mapping.getConverter().getClass()));
    assertTrue("Default object value on the object type converter for gender was not set to Male.", ((ObjectTypeConverter) mapping.getConverter()).getDefaultAttributeValue().equals(Employee.Gender.Male.name()));
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) ObjectTypeConverter(org.eclipse.persistence.mappings.converters.ObjectTypeConverter)

Example 47 with DatabaseSessionImpl

use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testObjectReferencedInBothEmAndSharedCache_AggregateObjectMapping.

// Bug 336280 - Same object referenced from both EM cache and shared cache
public void testObjectReferencedInBothEmAndSharedCache_AggregateObjectMapping() {
    EntityManager em = createEntityManager();
    DatabaseSessionImpl dbs = null;
    // persist a new Employee object
    Employee emp = new Employee();
    emp.setFirstName("A");
    EmploymentPeriod period = new EmploymentPeriod(Helper.dateFromYearMonthDate(1993, 0, 1), Helper.dateFromYearMonthDate(1996, 11, 31));
    emp.setPeriod(period);
    beginTransaction(em);
    em.persist(emp);
    // in JTA case transaction required to obtain ServerSession through getServersession method.
    dbs = getDatabaseSession();
    commitTransaction(em);
    closeEntityManager(em);
    // using query by example read empShared corresponding to emp in ghe share cache
    Employee empShared = (Employee) dbs.readObject(emp);
    // these are really to distinct objects
    assertTrue(emp != empShared);
    // they should not share the aggragate
    assertTrue(emp.getPeriod() != empShared.getPeriod());
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.EmploymentPeriod) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)

Example 48 with DatabaseSessionImpl

use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testClearEntityManagerWithoutPersistenceContextSimulateJTA.

// gf2074: EM.clear throws NPE (JTA case)
public void testClearEntityManagerWithoutPersistenceContextSimulateJTA() {
    EntityManager em = createEntityManager();
    DatabaseSessionImpl dbs = getDatabaseSession();
    closeEntityManager(em);
    // in non-JTA case session doesn't have external transaction controller
    boolean hasExternalTransactionController = dbs.hasExternalTransactionController();
    if (!hasExternalTransactionController) {
        // simulate JTA case
        dbs.setExternalTransactionController(new DummyExternalTransactionController());
    }
    try {
        testClearEntityManagerWithoutPersistenceContext();
    } finally {
        if (!hasExternalTransactionController) {
            // remove the temporary set TransactionController
            dbs.setExternalTransactionController(null);
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)

Example 49 with DatabaseSessionImpl

use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testAggregateObjectMappingReferenceDescriptor.

public void testAggregateObjectMappingReferenceDescriptor() {
    Set<String> errors = new HashSet();
    DatabaseSessionImpl dbs = getDatabaseSession();
    for (ClassDescriptor descriptor : dbs.getDescriptors().values()) {
        if (!descriptor.isDescriptorTypeAggregate()) {
            String path = descriptor.getJavaClassName();
            verifyAggregateObjectMappingReferenceDescriptor(descriptor, path, errors);
        }
    }
    if (!errors.isEmpty()) {
        String errorMsg = "The following AggregateObjectMappings have ReferenceDescriptors without any mappings:\n";
        for (String errorStr : errors) {
            errorMsg += errorStr + "\n";
        }
        fail(errorMsg);
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) HashSet(java.util.HashSet)

Example 50 with DatabaseSessionImpl

use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testObjectReferencedInBothEmAndSharedCache_ObjectReferenceMappingVH.

// Bug 336280 - Same object referenced from both EM cache and shared cache
public void testObjectReferencedInBothEmAndSharedCache_ObjectReferenceMappingVH() {
    EntityManager em = createEntityManager();
    Employee emp = new Employee();
    emp.setFirstName("Manager");
    Employee emp1 = new Employee();
    emp1.setFirstName("1");
    emp.addManagedEmployee(emp1);
    Employee emp2 = new Employee();
    emp2.setFirstName("2");
    emp.addManagedEmployee(emp2);
    DatabaseSessionImpl dbs = null;
    beginTransaction(em);
    em.persist(emp);
    // in JTA case transaction required to obtain ServerSession through getServersession method.
    dbs = getDatabaseSession();
    commitTransaction(em);
    CopyGroup copyGroup = new CopyGroup();
    copyGroup.cascadeAllParts();
    dbs.copy(emp, copyGroup);
    Set originalObjects = copyGroup.getCopies().keySet();
    // copyGroup cascades through all mappings.
    // originalObjects should consist of just three objects: emp, emp1, emp2.
    // However if manager_vh is wrapped around manager instance from the shared cache (empShared),
    // the size will be 6: emp, emp1, emp2 and empShared, emp1Shared, emp2Shared.
    assertTrue(originalObjects.size() == 3);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) Set(java.util.Set) HashSet(java.util.HashSet) UnitOfWorkChangeSet(org.eclipse.persistence.sessions.changesets.UnitOfWorkChangeSet) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) CopyGroup(org.eclipse.persistence.sessions.CopyGroup)

Aggregations

DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)64 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)18 EntityManager (jakarta.persistence.EntityManager)15 HashMap (java.util.HashMap)14 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)14 Map (java.util.Map)13 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)13 Project (org.eclipse.persistence.sessions.Project)12 StringReader (java.io.StringReader)9 StreamSource (javax.xml.transform.stream.StreamSource)9 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)9 MetadataProcessor (org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor)9 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)9 XMLContext (org.eclipse.persistence.oxm.XMLContext)9 DatasourceLogin (org.eclipse.persistence.sessions.DatasourceLogin)9 JAXBContext (jakarta.xml.bind.JAXBContext)8 JAXBException (jakarta.xml.bind.JAXBException)8 Unmarshaller (jakarta.xml.bind.Unmarshaller)8 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)8 XRDynamicClassLoader (org.eclipse.persistence.internal.xr.XRDynamicClassLoader)8