1

I tried googling this and found a possible duplicate of the question, but I cannot preview it: https://stackoverflow.com/questions/49344593/inverse-side-one-to-one-relationship-caching-not-working

Given two domain model classess:

@Entity
@Table(name = "object_a")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ObjectA extends AbstractEntity {

    private String  name;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "object_b_id")
    private ObjectB objectB;

    // getters and setters omitted
}

and

@Entity
@Table(name = "object_b")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ObjectB extends AbstractEntity {

    private String  name;

    @OneToOne(mappedBy = "objectB", fetch = FetchType.EAGER)
    private ObjectA objectA;

    // getters and setters omitted
}

I can see in the log that hibernate does not utilize caching for retrieving ObjectB#objectA field.

2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener  : Loading entity: [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener  : Attempting to resolve: [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener  : Converting second-level cache entry [CacheEntry(xy.abcde.test12.model.cachetest.ObjectA {objA, xy.abcde.test12.model.cachetest.ObjectB#1})] into entity : [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener  : Loading entity: [xy.abcde.test12.model.cachetest.ObjectB#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener  : Attempting to resolve: [xy.abcde.test12.model.cachetest.ObjectB#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener  : Converting second-level cache entry [CacheEntry(xy.abcde.test12.model.cachetest.ObjectB {objB, xy.abcde.test12.model.cachetest.ObjectA#1})] into entity : [xy.abcde.test12.model.cachetest.ObjectB#1]
2019-05-21 09:29:25.049 DEBUG 11808 --- [nio-8080-exec-1] org.hibernate.loader.Loader              : Loading entity: [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 DEBUG 11808 --- [nio-8080-exec-1] org.hibernate.SQL                        : select objecta0_.id as id1_61_1_, objecta0_.name as name2_61_1_, objecta0_.object_b_id as object_b3_61_1_, objectb1_.id as id1_62_0_, objectb1_.name as name2_62_0_ from object_a objecta0_ left outer join object_b objectb1_ on objecta0_.object_b_id=objectb1_.id where objecta0_.object_b_id=?

I found out through experimentation that this last entity load is in fact query for loading inverse side of relationship.

igobivo
  • 433
  • 1
  • 4
  • 17
  • Perhaps this is a bug? I opened an [issue](https://hibernate.atlassian.net/projects/HHH/issues/HHH-13422) to the hibernate team just in case... – igobivo Jun 10 '19 at 07:50
  • 1
    This is kind of old but I have just hit the same problem as you. I posted it here https://stackoverflow.com/questions/61238000/hibernate-second-level-cache-doesnt-work-for-onetoone-associations – Heits Apr 15 '20 at 21:33
  • 1
    Hibernate cannot eagerly fetch it from cache because, just like me, you are trying to load the side of the relation without the foreign key, so it is not present in the cache and apparently there's no way of doing so in Hibernate. This is very frustrating since its so usual to have this kind of relation. – Heits Apr 15 '20 at 21:35
  • 1
    Hi @Heits, thanks for your info. I don't remember this clearly, but I contacted Vlad about this, you can see the [Github conversation here](https://github.com/vladmihalcea/high-performance-java-persistence/pull/44#issuecomment-495182740). Vlad decidedly stated that this is a bug. There is also one old thread on [the old hibernate forum](https://forum.hibernate.org/viewtopic.php?f=1&t=1012333&start=0) that deals with this problem. – igobivo May 02 '20 at 21:18

0 Answers0