Hibernate Data Cache : Objet.list not updated on add

Viewed 36

I have two objects that are cached by hibernate and related with a ManyToOne relationship : Company that have a list of Treatment.

When I save a new Treatment in my database related to a company it doesn't update the company list of treatment : when I try to get my company later it doesn't contain the new treatment saved in the database.

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Treatment implements Serializable {

    @ManyToOne(optional = false)
    @NotNull
    @JsonIgnoreProperties("treatments")
    private Company company;

    ...
}

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Company implements Serializable {


    @OneToMany(mappedBy = "company", fetch = FetchType.EAGER)
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Treatment> treatments = new HashSet<>();

    ...
}
1 Answers

Ok, so far I have found one thing that works: On save I evict the whole cache.

@Override
@CacheEvict(value = "com.bloombooking.app.domain.Company.treatments", allEntries = true)
public Treatment save(Treatment treatment) {
    log.debug("Request to save Treatment : {}", treatment);
    return treatmentRepository.save(treatment);
}

I can't find the proper way to evict a particular object from the cache. Don't hesitate if you have better answer.

Related