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