Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Lazy loading can operate when persistence context is closed?

Doesn't the persistence context have to be open for a lazy loaded collection
to be fetched?

I thought if the persistence context was closed and you attempted to get a
lazy loaded collection, you'd get a lazy load exception?  (is that just with
Hibernate?)

I ran this bare bones test and my final assertion fails unexpectedly since
when I call
contract.getSupplierContracts it actual fires the SQL query to get the
collection without error even though the persistence context is closed (and
I even committed the xact just to make sure the loop was closed)

I am making sure to run the static weaver
(org.eclipse.persistence.tools.weaving.jpa.StaticWeave) before running my
test.

Why is the collection being lazy loaded even when the persistence context is
closed???

I'm using EclipseLink 1.1.1, JPA 1.0, against Oracle 10g, on Java SE 6

Here's my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd";>
	<persistence-unit name="JPASpike" transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<class>jpa.Contract</class>
		<class>jpa.SupplierContract</class>
		<class>jpa.Supplier</class>
		<exclude-unlisted-classes>true</exclude-unlisted-classes>
		<properties>
			<property name="eclipselink.jdbc.driver"
value="oracle.jdbc.OracleDriver"/>
			<property name="eclipselink.jdbc.read-connections.min" value="1"/>
			<property name="eclipselink.jdbc.read-connections.max" value="2"/>
			<property name="eclipselink.jdbc.write-connections.min" value="1"/>
			<property name="eclipselink.jdbc.write-connections.max" value="2"/>
			<property name="eclipselink.weaving" value="static"/>
			<property name="eclipselink.weaving.lazy" value="true"/>
			<property name="eclipselink.weaving.fetchgroups" value="false"/>
			<property name="eclipselink.weaving.changetracking" value="false"/>
			<property name="eclipselink.orm.throw.exceptions" value="true"/>
			<property name="eclipselink.logging.level" value="FINE"/>
			<property name="eclipselink.logging.logger" value="DefaultLogger"/>
			<property name="eclipselink.target-database" value="Oracle"/>
		</properties>
	</persistence-unit>
</persistence>

Here's a fragment of my mapping with lazy load configured:

    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "SUPPLIER_CONTRACT", joinColumns = @JoinColumn(name =
"CONTRACTID", referencedColumnName = "CONTRACTID"), inverseJoinColumns =
@JoinColumn(name = "SUPPLIERID", referencedColumnName = "SUPPLIERID"))
    private List<SupplierContract> supplierContracts = new
ArrayList<SupplierContract>();

And here's my test:

        Integer contractId = 101;
        Contract contract = null;
        EntityManager em =
Persistence.createEntityManagerFactory("JPASpike").createEntityManager();
        EntityTransaction tx = em.getTransaction();

        try
        {
            tx.begin();
            System.out.println("### find contract");
            contract = em.find(Contract.class, contractId);
            tx.commit();
            assertNotNull("Contract should be found with id: " + contractId,
contract);
            assertEquals("contract id", contractId, contract.getId());
        }
        catch (Exception e)
        {
            System.out.println("Persistence failed: " + e);
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // TODO this is failing with
IllegalStateException no xact is active
            fail("Persistence failed: " + e);
        }
        finally
        {
            System.out.println("### finally");
            if (em != null)
                em.close();
        }

        // Now, outside of the persistence context, make sure the collection
is not populated
        System.out.println("### verify collection not populated");
        System.out.println("### em is open: " + em.isOpen());
        assertTrue("collection should be empty after initial get",
contract.getSupplierContracts().isEmpty());

-- 
View this message in context: http://www.nabble.com/Lazy-loading-can-operate-when-persistence-context-is-closed--tp23990687p23990687.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top