# Entities not saved in L2C

**URL:** <https://discourse.hibernate.org/t/entities-not-saved-in-l2c/8747>\
**Category:** Hibernate ORM\
**Created:** [December 24, 2023, 4:37pm UTC](https://discourse.hibernate.org/t/entities-not-saved-in-l2c/8747 "2023-12-24T16:37:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shamil](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@shamil](https://discourse.hibernate.org/u/shamil)\
**Post date:** [December 24, 2023, 4:37pm UTC](https://discourse.hibernate.org/t/entities-not-saved-in-l2c/8747/1 "2023-12-24T16:37:05Z")

</div>

I’ve configured Hibernate in Spring with the following:

```auto
  jpa:
    generate-ddl: true
    properties:
      jakarta:
        missing_cache_strategy: create
        persistence:
          sharedCache:
            mode: ENABLE_SELECTIVE
        cache:
          provider: org.redisson.jcache.JCachingProvider
      hibernate:
        generate_statistics: true
        ddl-auto: update
        cache:
          use_second_level_cache: true
          use_query_cache: true
          region:
            factory_class: org.redisson.hibernate.RedissonRegionFactory
          redisson:
            fallback: true
            config: redisson/redisson-dev.yaml

```

And cacheable entities are annotated as such:

```auto
@Entity
@Data
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "entities")
@Cacheable
public class ComponentType {}

```

However, I am seeing issues putting the entities in cache:

```auto
2023-12-24T15:32:43.058Z DEBUG 46344 --- [nio-8080-exec-4] o.hibernate.orm.results.loading.entity : (ENTITYRESULTINITIALIZER) Adding entityInstance to second-level cache: net.shamil.dcc.entities.ComponentType(168864263290750)#0EKRCJTA5ZW3H
2023-12-24T15:32:43.058Z DEBUG 46344 --- [nio-8080-exec-4] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`entities` (AccessType[read-write])] : key[net.shamil.dcc.entities.ComponentType#0EKRCJTA5ZW3H] -> value[CacheEntry(net.shamil.dcc.entities.ComponentType)]
2023-12-24T15:32:43.059Z DEBUG 46344 --- [nio-8080-exec-4] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6977256641576960`, version=`1`] : txTimestamp=`6977257320656896`, newVersion=`1`
2023-12-24T15:32:43.059Z DEBUG 46344 --- [nio-8080-exec-4] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (entities), key=`net.shamil.dcc.entities.ComponentType#0EKRCJTA5ZW3H`, value=`CacheEntry(net.shamil.dcc.entities.ComponentType)`] failed due to being non-writable
20

```

Looking at the logs, I believe it’s because `version=null`:

```auto
public abstract class AbstractReadWriteAccess {

class Item {
  public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versionComparator) {
			if ( log.isDebugEnabled() ) {
				log.debugf(
						"Checking writeability of read-write cache item [timestamp=`%s`, version=`%s`] : txTimestamp=`%s`, newVersion=`%s`",
						timestamp,
						version,
						txTimestamp,
						newVersion
				);
			}

			//noinspection unchecked
			return version != null && versionComparator.compare( version, newVersion ) < 0;
		}
   }
}

```

My version in the database is 1 and it’s comparing to 1, such that the output of `versionComparator.compare( version, newVersion )` is `0`, hence it doesn’t write to cache…

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 4, 2024, 10:58am UTC](https://discourse.hibernate.org/t/entities-not-saved-in-l2c/8747/2 "2024-01-04T10:58:08Z")

</div>

The entity is already in the second level cache with the same version, so why should it put the object into the cache again? Skipping the cache put action in the first place is a possible optimization though.
