Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Lazy is not lazy(What am I missing?)
Lazy is not lazy [message #555579] Fri, 27 August 2010 09:07 Go to next message
No real name is currently offline No real nameFriend
Messages: 3
Registered: August 2010
Junior Member
Hi,

I am using static weaving and testing with the following code.
However, I'm not able getting the lazy fetching really lazy although I'm detaching the object after retrieving it.
What am I doing wrong?

Thanks a lot. I'm stuck...

edit: EclipseLink version is 2.1.0.

edit2: persistence.xml attached, too. Weaving is triggered through Ant and seems to work (Jar grows ~ 2 KB).

package de.ormtest;

import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Foo {
    private long id;
    private String name;
    private String descr;
    private List<Bar> list;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
	return id;
    }

    public void setId(long id) {
	this.id = id;
    }

    @Basic
    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    @Basic
    public String getDescr() {
	return descr;
    }

    public void setDescr(String descr) {
	this.descr = descr;
    }

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OrderColumn(name = "JPA_ORDERING")
    public List<Bar> getList() {
	return list;
    }

    public void setList(List<Bar> list) {
	this.list = list;
    }
}


package de.ormtest;

import javax.persistence.Basic;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Bar {
    private long id;
    private String name;
    private String descr = "hallo";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
	return id;
    }

    public void setId(long id) {
	this.id = id;
    }

    @Basic
    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    @Basic
    public String getDescr() {
	return descr;
    }

    public void setDescr(String descr) {
	this.descr = descr;
    }
}


package de.ormtest;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

public class Main {
    @PersistenceUnit(unitName = "ormtest")
    private static EntityManagerFactory emf = Persistence
	    .createEntityManagerFactory("ormtest");

    @PersistenceContext(unitName = "ormtest")
    private static EntityManager em = emf.createEntityManager();

    public static void main(String[] args) {
	{
	    Foo foo = new Foo();
	    foo.setName("fooName");
	    List<Bar> bars = new LinkedList<Bar>();
	    for (int j = 0; j < 300; j++) {
		Bar bar = new Bar();
		bar.setName("barName" + j);
		bars.add(bar);
	    }
	    foo.setList(bars);

	    EntityTransaction et = em.getTransaction();
	    et.begin();
	    em.persist(foo);
	    et.commit();
	}

	Foo result = em.find(Foo.class, 1L);
	em.detach(result);
	em.close();

	System.out.println(result);
	// Here I'm getting the wohle list, although the object is detached
	System.out.println(result.getList());
	System.out.println(result.getList().size());
    }
}


<?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="ormtest">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

		<class>de.ormtest.Foo</class>
		<class>de.ormtest.Bar</class>

		<properties>
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
			<!-- ;create=true -->
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/ormtest" />

			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="javax.persistence.jdbc.password" value="root" />

			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
			<property name="eclipselink.ddl-generation.output-mode"
				value="database" />
			<property name="eclipselink.weaving" value="static" />
		</properties>
	</persistence-unit>
</persistence>

[Updated on: Fri, 27 August 2010 12:14]

Report message to a moderator

Re: Lazy is not lazy [message #555933 is a reply to message #555579] Mon, 30 August 2010 09:40 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 3
Registered: August 2010
Junior Member
I also tried dynamic weaving - no difference.

I also inserted a "Thread.sleep(x)" after retrieving "Foo" from the database and shut the DB down during sleep().
-> Whole list was accessible, EclipseLink did not even notice that the DB is not available.

So the indirection does not work - but why?
I read all documentation dedicated to lazy loading and indirection but I just don't get it. Sad
Re: Lazy is not lazy [message #556077 is a reply to message #555579] Mon, 30 August 2010 17:46 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

First of all, weaving is only required for LAZY OneToOne and ManyToOne. A LAZY OneToMany will work even without weaving.

Second LAZY avoids the database access. You are not seeing any database access?

You are using the same EntityManager so will get the same object that you inserted, which has its list already instantiated.

If you created a new EntityManager then you would get a non instantiated list back, but if you accessed it, then it would instantiate, so you will always get the same result. The only way you would get a different result is if you serialized the object, then you would get an exception that you were accessing a dead relation.

Also it will not access the database even in a new EntityManger as the object is still in the cache.

Not sure on what you goal is? What do you want to occur? What are you trying to do?



James : Wiki : Book : Blog : Twitter
Previous Topic:Calculated Primary Key
Next Topic: NullPointerException in QueryKeyExpression.getQueryKeyOrNull
Goto Forum:
  


Current Time: Tue Apr 16 05:59:21 GMT 2024

Powered by FUDForum. Page generated in 0.29576 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top