javahibernatehibernate-cache

Hibernate L2 cache issues with EntityGraph and LEFT JOIN FETCH queries


I'm using hibernate 5.3.14 with hazelcast 3.11.5 as L2 cache provider and spring boot 2.1.11.

I have 3 entities defined with relations:

@Entity
@Table(name = "orders")
@org.hibernate.annotations.Cache(usage =CacheConcurrencyStrategy.READ_WRITE)
public class Order extends AbstractBaseEntity implements Orderable {

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @LazyCollection(LazyCollectionOption.TRUE)
    @Fetch(FetchMode.SELECT)
    private List<OrderItem> orderItems;

@MappedSuperclass
public abstract class AbstractBaseEntity

    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @JoinColumn(name = "parent_rid")
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<CustomField> customFields = new ArrayList<>();

@Entity
@Table(name = "custom_fields")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class CustomField implements Serializable {

@Entity
@Table(name = "order_items")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class OrderItem extends AbstractBaseEntity implements Orderable {

I have one repository:

@Repository
public interface OrderRepository extends JpaRepository<Order, String> {

    @EntityGraph(attributePaths = "customFields")
    Optional<Order> findById(String rid);

    @QueryHints(value = {@QueryHint(name = "org.hibernate.cacheable", value = "true")})
    @EntityGraph(attributePaths = "customFields")
    @Query("select o from Order left join fetch o.orderItems where o.status = 'ACTIVE' ")
    List<Order> findAllActiveWithOrderItems();

There are 3 problems:

  1. repo method findById doesn't load from the cache the main entity, order, with the relation, customFields, indicated by entity graph loaded

  2. cached query results for repo method findAllActiveWithOrderItems does not seem to have the relations, orderItems, loaded by the FETCH JOIN

  3. cached query results for repo method findAllActiveWithOrderItems does not seem to have the relations loaded by the the EntityGraph, customFields

Are there any known hibernate tickets or workarounds to fix those?


Solution

  • That's a known issue and I think Hibernate 6.0 will fix it, but I don't remember if there ever was a ticket for this.