[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] Caching and Relational Objects
|
If you use EntityManager.find() it will get a cache hit. If you use
EntityManager.createQuery().getSingleResult(), by default it will query the
database, unless you set the Query hint,
"eclipselink.cache-usage"="CheckCacheByPrimaryKey".
>> * What is the expected/accepted usage pattern with regards to cache, is
>> the use case of creating objects in memory with only primary key fields
>> invalid/incorrect?
It is invalid to create objects that are existing, especially with only an
id. You can you the EntityManager.getReference() method to get an object
reference.
>> * Does eclipselink make use of the hashcode/equals implemented in domain
>> objects (TimesheetPeriod) for its caching. I am guessing not as eclipse
>> link seems to use primary key equality (via ClassDescriptor instances)
>> and not the hashcode/equals combination for its cache (based on the code
>> I stepped
through).
No. EclipseLink never uses the object's hashCode() or equals() in its
caching, or in general.
Bhaskar Maddala wrote:
>
>>> The cache will cache anything you read, so if you read it again (by
> primary key) you avoid accessing the database, and (by query) avoid
> rebuilding the object and its relationships.
>
> If I understood this right, there is no way to prevent the database query
> (as in the case of using get) when using the "Query" objects, the cache
> only
> avoid the translation of tabular data to objects, correct?
>
> On the other questions, I must have done a real bad job of explaining ..
> :)
> Hopefully this will help (I am using eclipselink as my EJB3 persistence
> framework to a relational datastore)
>
> Problem 1:
> I get NullPointerException in my application due to entities that
> eclipselink returns when I retrieve (get or Query) them. The entities
> returned from eclipselink are objects it has cached (I figured this out by
> stepping into the eclipselink code). The entities in the cache have null
> values for attributes/fields which I know are constrained by the database
> schema to be not null, leading me to not do a null check and ultimately to
> a
> NPE
>
> I tried to figure out how the entities got into the cache (with a bad
> state)
> and tracked it down to the saving of another entity having a relation with
> the one in bad state
>
> I have a One-To-Many relationship between TSDATES - RSRHOUR, represented
> earlier by TimesheetPeriod and ResourceHour respectively. Saving (in this
> case it is a create or presist from your response) ResourceHour resulted
> in
> TimesheetPeriod being added to the eclipselink cache.
>
> My code that resulted in TimesheetPeriod being added to the cache (Note :
> Timesheet period instance is already in the database, when creating
> ResourceHour, I just need to provide a relational object reference as
> RSRCHOUR has a non null foreign key constraint)
>
> TimesheetPeriod tp = new TimesheetPeriod();
> tp.setTimesheetPeriodId(Integer.valueOf(timesheetTask.getTsId())); // only
> setting the key field, ignoring other fields, as I am only using this for
> foreign key reference
>
> ResourceHour hour = new ResourceHour();
> hour.setTimesheetPeriod(tp); // sets the TimesheetPeriod on ResourceHour
> m_hoursDao.save(hour); // Create/Persist the resource hour
>
> Starting with an empty cache for TimesheetPeriod prior to the persist call
> above, I end up with the following cache state after the persist
>
> UnitOfWorkIdentityMap for: TimesheetPeriod
> Key: [10] Identity Hash Code: 20015579 Object:
> TimesheetPeriod[TimesheetPeriodId: 10 StartDate: null EndDate: null
>
> Now when I load the TimesheetPeriod with TimesheetPeriodId (primary key
> @Id)
> 10, eclipselink returns the object in cache, with StartDate and EndDate
> null. My solution to fixing this was to replace the 2 lines of code
>
> TimesheetPeriod tp = new TimesheetPeriod();
> tp.setTimesheetPeriodId(Integer.valueOf(timesheetTask.getTsId())); // only
> setting the key field, ignoring other fields, as I am only using this for
> foreign key reference
>
> with
>
> TimesheetPeriod tp
> =m_timesheetPeriodDao.get(Integer.valueOf(timesheetTask.getTsId()));
>
> this results in the object in the database being loaded into the cache.
> The
> rest of the code remains the same.
>
> UnitOfWorkIdentityMap for: TimesheetPeriod
> Key: [10] Identity Hash Code: 20015579 Object:
> TimesheetPeriod[TimesheetPeriodId: 10 StartDate: 01/01/09 EndDate:
> 01/31/09
>
>
> Now back to the questions
> * What is the expected/accepted usage pattern with regards to cache, is
> the
> use case of creating objects in memory with only primary key fields
> invalid/incorrect?
> Should I be using
>
> TimesheetPeriod tp = new TimesheetPeriod();
> tp.setTimesheetPeriodId(Integer.valueOf(timesheetTask.getTsId()));
> ResourceHour hour = new ResourceHour();
> hour.setTimesheetPeriod(tp);
> m_hoursDao.save(hour);
>
> or
>
> TimesheetPeriod tp
> =m_timesheetPeriodDao.get(Integer.valueOf(timesheetTask.getTsId()));
> ResourceHour hour = new ResourceHour();
> hour.setTimesheetPeriod(tp);
> m_hoursDao.save(hour);
>
> or something else.
>
> * Does eclipselink make use of the hashcode/equals implemented in domain
> objects (TimesheetPeriod) for its caching. I am guessing not as eclipse
> link
> seems to use primary key equality (via ClassDescriptor instances) and not
> the hashcode/equals combination for its cache (based on the code I stepped
> through).
>
> Thank you for the reply.
>
> -Bhaskar
>
>
-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland
http://www.eclipse.org/eclipselink/
EclipseLink , http://www.oracle.com/technology/products/ias/toplink/
TopLink
Wiki: http://wiki.eclipse.org/EclipseLink EclipseLink ,
http://wiki.oracle.com/page/TopLink TopLink
Forums: http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink ,
http://www.nabble.com/EclipseLink-f26430.html EclipseLink
Book: http://en.wikibooks.org/wiki/Java_Persistence Java Persistence
--
View this message in context: http://www.nabble.com/Caching-and-Relational-Objects-tp21835126p21915901.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.