-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: L2 cache not working for associations
PostPosted: Wed Jun 13, 2012 1:08 pm 
Newbie

Joined: Mon Jan 30, 2012 7:04 pm
Posts: 4
I've just started using L2 caching and I've run into several issues that I need help understanding.
I'm using release 3.6

1) Entities are not cached if the Identifier Strategy is AUTO. Using SEQUENCE resolved this issue but I didn't see any mention in the docs.

2) Associations are not cached on persist but are cached after the first access.
I have two entities Activity and Task. Activity has a oneToMany association to Task (List<Task>). The association is marked with @Cache. When the Activity is persisted both the Activity and Task are cached but the association is not. Since the association is not cached, when Activity is queried it hits the cache then does a select to get the taskIds. It's only on the query that the association is cached.

The log message that the task is already cached is confusing as well. I would have expected the select to return the Ids and the engine to check the L2. The log message makes it sound like it's attempting to recache it. Maybe it's really saying the cached version matches the db.

Code:
@Entity
@DtoGenerate
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@org.hibernate.annotations.Entity(dynamicUpdate = true)
public class Activity implements Cloneable
{
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ACTIVITY_SEQ")
   @SequenceGenerator(name = "ACTIVITY_SEQ", sequenceName = "ACTIVITY_SEQ", allocationSize = 100)
   private long id;

   @Version
   private long version;


   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = Task.class, mappedBy = "activity")
   @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
   private List<Task> taskList;
}

@Entity
@DtoGenerate
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@org.hibernate.annotations.Entity(dynamicUpdate = true)
public class Task implements Cloneable
{
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TASK_SEQ")
   @GenericGenerator(name = "TASK_SEQ", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
      parameters =
         {
          @Parameter(name = "optimizer", value = "pooled"),
          @Parameter(name = "increment_size", value = "100"),
          @Parameter(name = "sequence_name", value = "TASK_SEQ ")
      })
   private long id;

   @Version
   private long version;

   @JoinColumn(name = "ACTIVITY_ID")
   @ManyToOne(fetch = FetchType.LAZY, cascade =
      { CascadeType.REFRESH, CascadeType.MERGE })
   private Activity activity;
}

[2012-06-13 11:45:05,986] [DEBUG] org.hibernate.SQL - call next value for ACTIVITY_SEQ
[2012-06-13 11:45:06,009] [DEBUG] org.hibernate.SQL - call next value for TASK_SEQ
[2012-06-13 11:45:06,010] [DEBUG] org.hibernate.SQL - call next value for TASK_SEQ
[2012-06-13 11:45:06,019] [DEBUG] org.hibernate.SQL - insert into Activity (....)
[2012-06-13 11:45:06,022] [DEBUG] org.hibernate.cache.TransactionalCache - inserting: com.att.bbnms.lightspeed.so.domain.Activity#1
[2012-06-13 11:45:06,027] [DEBUG] org.hibernate.SQL - insert into Task (....)
[2012-06-13 11:45:06,027] [DEBUG] org.hibernate.cache.TransactionalCache - inserting: com.att.bbnms.lightspeed.so.domain.Task#1
[2012-06-13 11:45:06,035] [DEBUG] org.hibernate.cache.TransactionalCache - cache lookup: com.att.bbnms.lightspeed.so.domain.Activity#1
[2012-06-13 11:45:06,035] [DEBUG] org.hibernate.cache.TransactionalCache - cache hit
[2012-06-13 11:45:06,039] [DEBUG] org.hibernate.cache.TransactionalCache - cache lookup: com.att.bbnms.lightspeed.so.domain.Activity.taskList#1
[2012-06-13 11:45:06,039] [DEBUG] org.hibernate.cache.TransactionalCache - cache miss
[2012-06-13 11:45:06,041] [DEBUG] org.hibernate.SQL - select tasklist0_.ACTIVITY_ID as ACTIVITY31_227_1_, tasklist0_.id as id1_, tasklist0_.id as id157_0_ from Task tasklist0_ where tasklist0_.ACTIVITY_ID=?
[2012-06-13 11:45:06,048] [DEBUG] org.hibernate.cache.TransactionalCache - item already cached: com.att.bbnms.lightspeed.so.domain.Task#1
[2012-06-13 11:45:06,048] [DEBUG] org.hibernate.cache.TransactionalCache - caching: com.att.bbnms.lightspeed.so.domain.Activity.taskList#1


Top
 Profile  
 
 Post subject: Re: L2 cache not working for associations
PostPosted: Wed Jun 20, 2012 2:22 pm 
Newbie

Joined: Mon Jan 30, 2012 7:04 pm
Posts: 4
I've done some more testing and I see the same behavior with different cache providers.
1) persist stores Object A and Object B
2) query on Object A hits on Object A. Object A to Object B association misses and issues sql query. Association is cached. Message stating Object B is already in cache.
3) query on Object A hits, association hits, Object B hits. No sql generated.

This seems very wasteful. Does anyone know if this is standard Hibernate L2 Cache behavior? or is there something I can do to force the association to be cached on the initial persist?

-Bill


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.