-
Type:
Bug
-
Status: Closed
-
Resolution: Fixed
-
Affects Version/s: 5.2.3, 6.0.5 GA
-
Fix Version/s: --Sprint - SP, 6.0.6 GA, 6.1.0 CE RC1
-
Component/s: Core Infrastructure > Service Builder
-
Labels:None
-
Branch Version/s:6.0.x, 5.2.x
-
Backported to Branch:Committed
I encountered a problem with the way service builder generated code caches entities
>> Problem description
My service.xml looks like this
<entity name="CalEventInvitation" local-service="true" remote-service="true">
...
<column name="calEventId" type="long" />
<finder name="CalEventCopyId" return-type="CalEventInvitation">
<finder-column name="calEventCopyId" />
</finder>
</entity>
Then i save an invitation like this
long invitationId = CounterLocalServiceUtil.increment(CalEventInvitation.class.getName());
CalEventInvitation invitation = CalEventInvitationLocalServiceUtil.createCalEventInvitation(invitationId);
invitation.setGroupId(groupId);
CalEventInvitationLocalServiceUtil.updateCalEventInvitation(invitation); // does a merge
I do not set the calEventCopyId
I now query for a CalEventInvitation with the calEventCopyId property set to 123
CalEventInvitationUtil.findByCalEventCopyId(123L);
This correctly returns null
I then set the calEventCopyId and update like this :
invitation.setCalEventCopyId(123L);
CalEventInvitationLocalServiceUtil.updateCalEventInvitation(invitation);
And query for
CalEventInvitationUtil.findByCalEventCopyId(123L);
and it will return null where it should return the invitation i just created
>> Why does this happen ?
The first time i call CalEventInvitationUtil.findByCalEventCopyId(123L); it will cache the result,
and when i update the value, this cache is not invalidated
In the generated updateImpl method in the generated CalEventInvitationPersistenceImpl class we find the following code
if (!isNew &&
(calEventInvitation.getCalEventCopyId() != calEventInvitationModelImpl.getOriginalCalEventCopyId())) {
FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CALEVENTCOPYID,
new Object[]
);
}
So the cache is only flushed when it is detected that the calEventCopyId property was changed
Why does it not detect the change ?
originalCalEventCopyId is set in the generated setCalEventCopyId method in CalEventInvitationModelImpl.
This method looks like this
public void setCalEventCopyId(long calEventCopyId) {
_calEventCopyId = calEventCopyId;
if (!_setOriginalCalEventCopyId)
{ _setOriginalCalEventCopyId = true; _originalCalEventCopyId = calEventCopyId; }}
As you can see it updates the originalCalEventCopyId property if it has not been set before
In my case it never was set, so when i update the invitation for the second time originalCalEventCopyId
and calEventCopyId will be the same and the cache wont be invalidated
>> Possible solution
Set all original values to the current value on update