Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: Antw.: [eclipselink-users] question about readObject(Object object) method in Session class

Bozhou,
    After you construct a new Employee - it is not managed (the native UnitOfWork or EntityManager - if you were using JPA) and does not know about changes.  Normally e would need to be merged/persisted before or during your query so that the in-memory changes get flushed (persist in JPA) to the database and/or read back depending on your cache settings.

    I tried the following registered instantiation but still get a null object because it is only in memory
            Cell aCell = (Cell)session.acquireUnitOfWork().newInstance(Cell.class);
[EL Finest]: 2010-07-27 08:02:19.737--UnitOfWork(33165530)--Thread(Thread[main,5,main])--Register the object org.eclipse.persistence.example.jpa.server.business.Cell@13586438( id: null state: null left: null right: null parent: null references: null)

    I will try pure native code shortly, but the following mixed JPA/Native code functions correctly - returning null without the persist (unmanaged) and returning the same object with a persist

            Session session = ((UnitOfWork)((EntityManagerImpl)em).getUnitOfWork()).getActiveSession();
            em.getTransaction().begin();
            Cell aCell = new Cell();
            aCell.setId(BigInteger.valueOf((long)2));
            em.persist(aCell);
            Cell newCell = (Cell)session.readObject(aCell);

>unmanaged
//            em.persist(aCell);
[EL Finest]: 2010-07-27 07:57:05.734--UnitOfWork(3945427)--Thread(Thread[main,5,main])--Execute query ReadObjectQuery(referenceClass=Cell )
[EL Fine]: 2010-07-27 07:57:05.734--ServerSession(11800260)--Connection(31958869)--Thread(Thread[main,5,main])--SELECT ID, STATE, TSEQ, RIGHT_ID, ACELLATTRIBUTE_ID FROM EL_CELL WHERE (ID = ?)
    bind => [2]
aCell    Cell  (id=83)   
newCell    null   


>same object
[EL Finest]: 2010-07-27 07:55:44.686--UnitOfWork(18323943)--Thread(Thread[main,5,main])--PERSIST operation called on: org.eclipse.persistence.example.jpa.server.business.Cell@24475641( id: 2 state: null left: null right: null parent: null references: null).
[EL Finest]: 2010-07-27 07:55:55.311--UnitOfWork(18323943)--Thread(Thread[main,5,main])--Execute query ReadObjectQuery(referenceClass=Cell )
aCell    Cell  (id=83)   
newCell    Cell  (id=83)   


As you can see, in the persist 2nd case - no SQL is generated and the object is read from the memory cache, in the 1st example we don't find the object via an SQL select as expected.
This is in a transaction, with default caching behavior and logging on finest.
Native behavior should be the same, I will check it and advise.

    /michael
    www.eclipselink.org

bozhou tao wrote:
Hi Jaap,

The primary key for the Employee object is the employeeId and when I switch the persistence provider to Toplink, I can get the expected result.
Is there any other configuration I need to do?

thanks,
Bozhou


Date: Mon, 26 Jul 2010 23:36:23 -0700
From: jaap.spiering@xxxxxxxxxx
To: eclipselink-users@xxxxxxxxxxx
Subject: Antw.: [eclipselink-users] question about readObject(Object object) method in Session class

Hi Bozhou,

what is the primary key of your Employee object?
And do you have the log output for this bit of code? You should be able to see the SQL that was issued.

regards,

Jaap

----- Oorspronkelijk bericht -----
Van: bozhou.tao@xxxxxxxxxxx
Aan: eclipselink-users@xxxxxxxxxxx
Verzonden: Dinsdag 27 juli 2010 04:02:15 GMT +01:00 Amsterdam / Berlijn / Bern / Rome / Stockholm / Wenen
Onderwerp: [eclipselink-users] question about readObject(Object object) method in Session class

Hi,

I'm now using Eclipselink as the persistence provider of Toplink in my project and for some reason I need to use the readObject(Object object) method of the Session class.
According to the document:
java.lang.Object readObject(java.lang.Object object)
                            throws DatabaseException
PUBLIC: Use the example object to construct a read object query by the objects primary key. This will read the object from the database with the same primary key as the object or null if no object is found.
However, when I call this method in my code, the result is not as I expected. Take the code below for example:

Employee e = new Employee();
e.setEmployeeId(2);
Employee eCopy = session.readObject(e);
System.out.print(eCopy.getEmployeeId());

I think the result should be "2" but the result I get is "1".

Is it design! ed to be that?

Regards,
Bozhou



Hotmail: Trusted email with powerful SPAM protection. Sign up now.


Hotmail: Trusted email with Microsoft’s powerful SPAM protection. Sign up now.

_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top