users@glassfish.java.net

Data retrieval after EM closed, bug?

From: <glassfish_at_javadesktop.org>
Date: Wed, 27 Aug 2008 17:38:58 PDT

Hello.

I think TopLink is retrieving data after EM has been closed in the case
below, and I'm not sure if this behavior is correct.

[b]CASE[/b]: (Steps 1 to 4 and follows)
[b]
STEP 1[/b].- I'm using a recursive table (most probably the same applies for
non-recursive tables):

create table "SA"."RECURSIVA" (
PK int primary key not null,
DATO varchar(10),
PADRE int references RECURSIVA
);

INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (0,'Raiz',null);
INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (1,'n1',0);
INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (2,'n2',1);
INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (3,'n3',2);
INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (4,'n4',3);
INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (5,'n5',4);
INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (6,'n6',5);
INSERT INTO "SA"."RECURSIVA" (PK,DATO,PADRE) VALUES (7,'n7',6);

[b]STEP 2[/b].- This is the entity (please [u][b]note[/b][/u] the LAZY fetch type):

@Entity
@Table(name = "RECURSIVA")
public class Recursiva implements Serializable {
@Id
@Column(name = "PK", nullable = false)
private Integer pk;
@Column(name = "DATO")
private String dato;
@OneToMany(mappedBy = "padre")
private Collection<Recursiva> recursivaCollection;
@JoinColumn(name = "PADRE", referencedColumnName = "PK")
@ManyToOne(fetch=FetchType.LAZY)
private Recursiva padre;
....
[b]
STEP 3[/b].- This is the data retrieval code (please [u][b]note[/b][/u] EntityManager is closed
before accessing data):

EntityManagerFactory emf =
Persistence.createEntityManagerFactory("mijpa");;
EntityManager em = emf.createEntityManager();
Recursiva rc = null;

rc = em.find(Recursiva.class, 7);
em.close();

while (rc != null) {
System.out.println(rc.getDato());
rc = rc.getPadre();
}

emf.close();

[b]STEP 4[/b].- Results:

n7
n6
n5
n4
n3
n2
n1
Raiz

[b]QUESTIONS[/b]: If em is closed right after the leaf entity is retrieved:

A. How is it possible that all of the leaf ancestors up to the root are
retrieved?
B. Is toplink accessing the database after em is closed or simply the
full hierarchy is eagerly loaded at the very beginning?
C.[b] [u]Is this correct, or is it a bug?[/u][/b]

NOTE: openjpa prints n7 and n6, as I would have expected.

I would appreciate your comments, specially if you think this is a bug,
in order to report it.

Thanks.
Antonio.

Tested with:

    * Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))
    * derby 10.4.1.3
    * java hotspot 1.6.0_06-b02
    * ubuntu 8.04.1 2.6.24-19-generic
[Message sent by forum member 'antodasana' (antodasana)]

http://forums.java.net/jive/thread.jspa?messageID=295783