I’m running into a difference between NONSTRICT_READ_WRITE and READ_WRITE CacheConcurrencyStrategy when writing “denormalized” collections… the idea being that I have a join table modeled as an entity but it also contains read only links to the tables it joins to.
My entities, roughly:
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
class Actor {
@Id
Integer id;
@Column
String name;
}
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
class Movie {
@Id
Integer id;
@Column
String title;
}
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
class Credit {
@Column
String roleName;
@ManyToOne(targetEntity = Movie.class, optional = true)
@JoinColumn(name = "movie_id", insertable = false, updatable = false)
@NotFound(action = NotFoundAction.IGNORE)
Movie movie;
@Column(name = "movie_id")
Long movieId;
@ManyToOne(targetEntity = Actor.class, optional = true)
@JoinColumn(name = "actor_id", insertable = false, updatable = false)
@NotFound(action = NotFoundAction.IGNORE)
Actor actor;
@Column(name = "actor_id")
Long actorId;
}
Second level object cache is enabled (with ehcache).
My application writes Movies and Actors… and sometime later, it links them together by writing Credit. When Credit is written, I only fill in the roleName, movieId, and actorId fields, I do not provide the Movie and Actor objects.
Using NONSTRICT_READ_WRITE caching, I am then able to read back that Credit object and it will contain the referenced Movie and Actor objects.
Using READ_WRITE caching, reading back the Credit will return a Credit with empty Movie and Actor fields. If I clear the hibernate cache, reading back that Credit then contains the Movie and Actor objects as expected. This is also the behavior with TRANSACTIONAL caching (but of course not with NONE caching).
So it would seem that hibernate is inserting Credit into the 2nd level cache with null Actor and Movie fields when using READ_WRITE cache. Is there a way to prevent this from happening and always read from the database to get back these joined fields? I’ve tried annotating just the fields with CacheConcurrencyStrategy.NONE, but this does not work.
I think you have probably stumbled across a hibernate bug because of your weird mapping (at least non standard mapping). There is no real reason for having two fields one with id and one with entity.
You can turn an id into an entity reference using
session.load– which just creates a proxy, doesn’t load the data from DB.If you get rid of the movieId and actorId field and remove the insertable / updatable false on the movie / actor field it should work the same irrespective of
READ_WRITEorNON_STRICT_READ_WRITEHibernate doesn’t save the complete objects in the second level cache. I stores it in a flattened form (hydrated – more like db tables) and reconstructs the object from that. So your assertion that it stored the object with null in the cache and not updating it is incorrect. There is something else going on.
The main difference between
READ_WRITEandNON_STRICT_READ_WRITEis that the entry in the cache will be locked while updating in case of READ_WRITE and won’t be locked in NON_STRICT_READ_WRITE. This will only impact if you are updating this entity in multiple threads concurrently.