首页 > 解决方案 > 使用 @Cacheable 时出现 EntityNotFoundException

问题描述

在我的应用程序中,我有多个实体,例如:

@Entity
@Data
@Builder
@ToString(of = {"id", "code", "nameContentType", "observations"})
@EqualsAndHashCode(exclude = "room")
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "desk")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Desk implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @Column(name = "code")
    private String code;

    @Lob
    @Column(name = "name")
    private byte[] name;

    @Column(name = "name_content_type")
    private String nameContentType;

    @Column(name = "observations")
    private String observations;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(unique = true)
    private Coordinates coordinates;

    @OneToMany(mappedBy = "desk")
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private Set<Reservation> reservations = new HashSet<>();

    @ManyToOne
    @JoinColumn(name = "room_id", insertable = false, updatable = false)
    @JsonIgnoreProperties(value = "desks", allowSetters = true)
    private Room room;

  
}

用集合表示的所有关系都用 缓存@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

当我删除一些相关实体的记录时,我得到:

javax.persistence.EntityNotFoundException: Unable to find com.**.domain.Reservation with id ***

我不知道是否需要对缓存设置进行任何额外调整或如何调试问题

标签: spring-boothibernateredis

解决方案


你确定你更新了 Hibernate 到最新版本 5.4.32.Final 吗?如果是这样,并且您仍然有问题,请在问题跟踪器(https://hibernate.atlassian.net)中创建一个带有测试用例( https://github.com/hibernate/hibernate-test-case- )的问题重现问题的模板/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)。


推荐阅读