EntityGraph 和 LEFT JOIN FETCH 查询的 Hibernate L2 缓存问题
Hibernate L2 cache issues with EntityGraph and LEFT JOIN FETCH queries
我使用 hibernate 5.3.14 和 hazelcast 3.11.5 作为二级缓存提供程序和 spring 启动 2.1.11。
我用关系定义了 3 个实体:
- 一个订单有多个订单项
- 一个订单有多个自定义字段
为实体、关联和查询启用 L2 缓存。
@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 {
我有一个仓库:
@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();
有3个问题:
repo 方法findById
不从缓存中加载主要实体、订单、关系、customFields,由加载的实体图指示
repo 方法 findAllActiveWithOrderItems
的缓存查询结果似乎没有关系,orderItems,由 FETCH JOIN
加载
缓存的查询结果
对于 repo 方法 findAllActiveWithOrderItems
似乎没有由 EntityGraph、customFields
加载的关系
是否有任何已知的休眠问题或解决方法来解决这些问题?
这是一个已知问题,我认为 Hibernate 6.0 会修复它,但我不记得是否有过此问题的票证。
我使用 hibernate 5.3.14 和 hazelcast 3.11.5 作为二级缓存提供程序和 spring 启动 2.1.11。
我用关系定义了 3 个实体:
- 一个订单有多个订单项
- 一个订单有多个自定义字段 为实体、关联和查询启用 L2 缓存。
@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 {
我有一个仓库:
@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();
有3个问题:
repo 方法
findById
不从缓存中加载主要实体、订单、关系、customFields,由加载的实体图指示repo 方法
加载findAllActiveWithOrderItems
的缓存查询结果似乎没有关系,orderItems,由 FETCH JOIN缓存的查询结果 对于 repo 方法
加载的关系findAllActiveWithOrderItems
似乎没有由 EntityGraph、customFields
是否有任何已知的休眠问题或解决方法来解决这些问题?
这是一个已知问题,我认为 Hibernate 6.0 会修复它,但我不记得是否有过此问题的票证。