![](/img/trans.png)
[英]EntityGraph or Join Fetch with Inheritance in JPA/Hibernate
[英]Hibernate L2 cache issues with EntityGraph and LEFT JOIN FETCH queries
我使用 hibernate 5.3.14 和 hazelcast 3.11.5 作为二级缓存提供程序和 spring 引导 2.1.11。
我有 3 个用关系定义的实体:
@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
的缓存查询结果似乎没有由 FETCH JOIN 加载的关系 orderItems
repo 方法findAllActiveWithOrderItems
的缓存查询结果似乎没有由 EntityGraph、customFields 加载的关系
是否有任何已知的 hibernate 票或解决方法来解决这些问题?
这是一个已知问题,我认为 Hibernate 6.0 会修复它,但我不记得是否有过此问题的票证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.