Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

java - 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:

  • one order has many order items
  • one order has many custom fields L2 cache is enabled for entities, associations and queries.
@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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...