Vishal
Vishal

Reputation: 1209

Hibernate tries to update version number of read-only object

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?

Upvotes: 2

Views: 4131

Answers (4)

sendon1982
sendon1982

Reputation: 11244

That cache is Hibernate second level cache.You can add

<prop key="hibernate.cache.use_second_level_cache">false</prop>

under hibernate properties to fix that.

Upvotes: 0

Assem BARDI
Assem BARDI

Reputation: 9

i think that you dont have to put a cascade = CascadeType.ALL since your entity is READ-ONLY hibernate will perform a delete on a READ-ONLY try to put a CascadeType.NONE for a join with a READ-ONLY entity

Upvotes: 1

Harmeet Singh Taara
Harmeet Singh Taara

Reputation: 6611

i face the same exception , the reason behind is , when the entity is create and set the values in Entity. After this when the session.save() method is call , hibernate do some changes or modification with collections in entity , but my collections is read-only. the changes and modification what the hibernate do , i don't know , but when i change the usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE this work successfully.

Upvotes: 0

xelah
xelah

Reputation: 36

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.

Upvotes: 2

Related Questions