Hibernate complains,
Caused by:
java.lang.UnsupportedOperationException: Can’t write to a readonly
object at
org.hibernate.cache.ReadOnlyCache.lock(ReadOnlyCache.java:68)
for class which has @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) but which is not modified. Class A has a many-to-many relationship with Class B defined via annotation,
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@MapKey(name = "abbreviation")
private Map<String, B> allBs;
I am creating a new object of Class A and associating it with an existing object of Class B but for some unknown reason Hibernate tries to modify the version and the lastmodified time of Class B.
If I remove the read_only annotation it works fine and for no reason the version and last modified date of B gets updated apart from an entry in the joining table a_b;
Is this something that Hibernate does – Write B even if only a new association is added and the object itself is not changed?
It appears that Hibernate tries to lock objects during certain operations which are permitted even for read-only-cacheable entities. I ran across this whilst trying to delete one (with the Infinispan cache). However, a lower-level part of hibernate for dealing with read-only caches throws an exception whenever any locking is attempted because (I presume) it considers that to signify an intention to modify the entity.
This appears to me to be incorrect behaviour on the part of Hibernate. Try commenting out the throwing of the exception in ReadOnlyCache (and in org.hibernate.cache.infinispan.entity.ReadOnlyCache.java, too), and returning null where necessary. Then recompile hibernate/replace those .class files in the jar. I can’t promise this won’t introduce a bug relating to concurrent cache access, although it does appear to work for me.