Hibernate @oneToOne mapping is unable to use L2 cache when mapping returns no Object
11:31 08 Dec 2025

I have 2 JPA classes with bi-directional onetoOne mapping :

@Entity
public class A {
...

// bi-directional one-to-one association to B
    @OneToOne(cascade = CascadeType.ALL, optional = true, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "btemp")
    private B b;

...

public B getB() {
    return this.B;
}

public A setSubscriptionBatchnotify(B b) {
    if (b == null) {
        if (this.b != null) {
            this.b.setA(null);
        }
    }
    else {
        b.setA(this);
    }
    this.b = b;
    return this;
}

}


@Entity
public class B {
...

// bi-directional one-to-one association to A
    @OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private A a;

...

    public A getA() {
        return this.resourceSubscription;
    }

    public void setA(A a) {
        this.a = a;
    }

}

I have enable entity cache using Redis and Redisson with Hibernate. I want find B using A but when I see the queries generated by hibernate I can see that queries are hitting generated instead of getting cache hits. I found out that it is happening because the relation mapping is returning no object and thus no object is being stored in the cache related to the object B, thus every time the relation is used a fresh query is executed to DB. I have also tried :

@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
// bi-directional one-to-one association to B
    @OneToOne(cascade = CascadeType.ALL, optional = true, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "btemp")
    private B b;

So that the relationship is cached but I still see that query in hibernate logs. Is there a way to fix this?

java hibernate caching one-to-one redisson