0
votes

I'm experiencing an issue with Hibernate collections caching (by EHCache).

So, this is the entity that owns the collection :

@Entity(name = "Message")
@Table(name = "t_message")
public class Message implements Comparable<Message>{
    @Id
    @Column(name = "message_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "message")
    @Sort(type = SortType.NATURAL)
    @JsonIgnore
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    private SortedSet<Event> events = new TreeSet<>();

    // ...
}

Then, in the other entity, there is a symetrical to the Message :

@Entity(name = "Event")
@Table(name = "t_event")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class Event implements Comparable<Event>, Comparator<Event> {
    @Id
    @Column(name = "event_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "event_message_id", referencedColumnName = "message_id")
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    private Message message;

    // ...
}

The problem : when I add an Event, the "events" field, the cache is not evicted. If I remove the @Cache anotation below this field, it works but of course with no cache.

I believed this has to be linked with the fact that when inserting a new event (by entity manipulations), we link it to a message from the event side so I added this line on hibernate configuration file :

<property name="hibernate.cache.auto_evict_collection_cache">true</property>

But it didn't solve the problem.

Additional informations :

  • Other ManyToOne collections caching works perfectly on the project.
  • There is no native queries in the project, only HQL and entities manipulations.
  • I'm using Hibernate 4.3.2 and EHCache 2.10.4.

Thanks for your advices.

1

1 Answers

0
votes

Ok, just figured out that a "cascade" parameter was missing in the Message's events field @OneToMany annotation :

@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinColumn(name = "event_message_id", referencedColumnName = "message_id")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
private Message message;

Solved !