Welcome to OStack Knowledge Sharing 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...