Search in sources :

Example 1 with VegetablePK

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

the class EntityManagerJUnitTestSuite method testCharFieldDefaultNullValue.

public void testCharFieldDefaultNullValue() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Vegetable vegetable;
    Vegetable vegetable2;
    VegetablePK pk = new VegetablePK("Eggplant", "Purple");
    VegetablePK pk2 = new VegetablePK("Capsicum", "Green");
    String[] tags = { "California", "DE" };
    String[] tags2 = { "Florida", "FD" };
    try {
        // Usecase for char with value ' ' (space)
        vegetable = new Vegetable();
        vegetable.setId(pk);
        vegetable.setCost(2.09);
        vegetable.setTags(tags);
        vegetable.setType(' ');
        em.persist(vegetable);
        // Usecase for char with value null
        vegetable2 = new Vegetable();
        vegetable2.setId(pk2);
        vegetable2.setCost(1.35);
        vegetable2.setTags(tags2);
        em.persist(vegetable2);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    } finally {
        closeEntityManager(em);
    }
    em = createEntityManager();
    em.clear();
    clearCache();
    beginTransaction(em);
    try {
        vegetable = em.find(Vegetable.class, pk);
        vegetable2 = em.find(Vegetable.class, pk2);
        commitTransaction(em);
        assertNotNull(vegetable);
        assertNotNull(vegetable2);
        assertTrue(vegetable.getType() == '\u0000');
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    } finally {
        closeEntityManager(em);
    }
}
Also used : VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable)

Example 2 with VegetablePK

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

the class AdvancedJunitTest method testSwitchBatchDuringSessionEvent.

public void testSwitchBatchDuringSessionEvent() {
    // Test for bug#419326
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        Vegetable vegetable = em.find(Vegetable.class, new VegetablePK("Potato", "Yellow"));
        if (null != vegetable) {
            // cleanup old data
            em.remove(vegetable);
            em.flush();
            commitTransaction(em);
        }
    } catch (RuntimeException e) {
        throw e;
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        clearCache();
        closeEntityManager(em);
    }
    em = createEntityManager();
    DatabasePlatform platform = getServerSession().getPlatform();
    boolean usesBatchWriting = platform.usesBatchWriting();
    boolean usesJDBCBatchWriting = platform.usesJDBCBatchWriting();
    boolean usesExternalConnectionPooling = getServerSession().getLogin().shouldUseExternalConnectionPooling();
    boolean usesExternalTransactionController = getServerSession().getLogin().shouldUseExternalTransactionController();
    final SessionEventAdapter listener = new PostAcquireExclusiveConnectionSqlExecutorListener();
    Vegetable vegetable;
    try {
        // Simulate JDBC batching with external connection pooling
        getServerSession().getLogin().useExternalConnectionPooling();
        getServerSession().getLogin().useExternalTransactionController();
        platform.setUsesBatchWriting(true);
        platform.setUsesJDBCBatchWriting(true);
        em.setProperty(EntityManagerProperties.EXCLUSIVE_CONNECTION_MODE, ExclusiveConnectionMode.Always);
        getServerSession().getEventManager().addListener(listener);
        beginTransaction(em);
        em.setFlushMode(FlushModeType.COMMIT);
        vegetable = em.find(Vegetable.class, new VegetablePK("Potato", "Yellow"));
        if (vegetable == null) {
            vegetable = new Vegetable();
        }
        vegetable.setId(new VegetablePK("Potato", "Yellow"));
        vegetable.setCost(1.10);
        em.persist(vegetable);
        // Here the transaction will be lazily opened, old connection handles released and new
        // connection obtained, triggering postAcquireExclusiveConnection event.
        // The postAcquireExclusiveConnection() invokes a non-batchable statement, due to which
        // current batch is cleared.  This should not cause a duplicate execution of insert.
        commitTransaction(em);
    } catch (jakarta.persistence.RollbackException r) {
        fail("RollbackException exception occurred : " + r.getMessage());
    } finally {
        // Cleanup and revert back to original conditions
        getServerSession().getEventManager().removeListener(listener);
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        platform.setUsesBatchWriting(usesBatchWriting);
        platform.setUsesJDBCBatchWriting(usesJDBCBatchWriting);
        if (!usesExternalConnectionPooling) {
            getServerSession().getLogin().dontUseExternalConnectionPooling();
        }
        if (!usesExternalTransactionController) {
            getServerSession().getLogin().dontUseExternalTransactionController();
        }
        clearCache();
        closeEntityManager(em);
    }
}
Also used : VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SessionEventAdapter(org.eclipse.persistence.sessions.SessionEventAdapter) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable)

Example 3 with VegetablePK

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

the class AdvancedJunitTest method testStringArrayField.

// GF1673, 2674 Java SE 6 classloading error for String[] field
public void testStringArrayField() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    VegetablePK pk = new VegetablePK("Tomato", "Red");
    String[] tags = { "California", "XE" };
    try {
        Vegetable vegetable = new Vegetable();
        vegetable.setId(pk);
        vegetable.setCost(2.09);
        vegetable.setTags(tags);
        em.persist(vegetable);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    } finally {
        closeEntityManager(em);
    }
    em = createEntityManager();
    beginTransaction(em);
    Vegetable vegetable;
    try {
        vegetable = em.find(Vegetable.class, pk);
        commitTransaction(em);
        assertNotNull(vegetable);
        assertTrue(Arrays.equals(tags, vegetable.getTags()));
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    } finally {
        closeEntityManager(em);
    }
}
Also used : VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable)

Example 4 with VegetablePK

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

the class EntityManagerJUnitTestSuite method testWeaving.

/**
 * Test that all of the classes in the advanced model were weaved as expected.
 */
public void testWeaving() {
    // Only test if weaving was on, test runs without weaving must set this system property.
    if (isWeavingEnabled()) {
        internalTestWeaving(new Employee(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), true);
        internalTestWeaving(new FormerEmployment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Address(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PhoneNumber(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EmploymentPeriod(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new Buyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new GoldBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new PlatinumBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // eager 1-m
        internalTestWeaving(new Department(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Golfer(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new GolferPK(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new SmallProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new LargeProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Man(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Woman(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // serialized
        internalTestWeaving(new Vegetable(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new VegetablePK(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new WorldRank(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Equipment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EquipmentCode(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PartnerLink(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
    }
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) WorldRank(org.eclipse.persistence.testing.models.jpa.advanced.WorldRank) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EquipmentCode(org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode) FormerEmployment(org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Buyer(org.eclipse.persistence.testing.models.jpa.advanced.Buyer) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) GolferPK(org.eclipse.persistence.testing.models.jpa.advanced.GolferPK) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Golfer(org.eclipse.persistence.testing.models.jpa.advanced.Golfer) Man(org.eclipse.persistence.testing.models.jpa.advanced.Man) Woman(org.eclipse.persistence.testing.models.jpa.advanced.Woman) PartnerLink(org.eclipse.persistence.testing.models.jpa.advanced.PartnerLink) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 5 with VegetablePK

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

the class AdvancedJunitTest method testGF1818.

public void testGF1818() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Vegetable vegetable = new Vegetable();
        vegetable.setId(new VegetablePK("Carrot", "Orange"));
        vegetable.setCost(2.09);
        em.persist(vegetable);
        commitTransaction(em);
    } catch (Exception e) {
        fail("An exception was caught: [" + e.getMessage() + "]");
    }
    closeEntityManager(em);
}
Also used : VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable)

Aggregations

Vegetable (org.eclipse.persistence.testing.models.jpa.advanced.Vegetable)5 VegetablePK (org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK)5 EntityManager (jakarta.persistence.EntityManager)4 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)4 DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)1 SessionEventAdapter (org.eclipse.persistence.sessions.SessionEventAdapter)1 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)1 Buyer (org.eclipse.persistence.testing.models.jpa.advanced.Buyer)1 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)1 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)1 EmploymentPeriod (org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod)1 Equipment (org.eclipse.persistence.testing.models.jpa.advanced.Equipment)1 EquipmentCode (org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode)1 FormerEmployment (org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment)1 GoldBuyer (org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer)1 Golfer (org.eclipse.persistence.testing.models.jpa.advanced.Golfer)1 GolferPK (org.eclipse.persistence.testing.models.jpa.advanced.GolferPK)1 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)1 Man (org.eclipse.persistence.testing.models.jpa.advanced.Man)1 PartnerLink (org.eclipse.persistence.testing.models.jpa.advanced.PartnerLink)1