user590444
user590444

Reputation: 4332

Strange hibernate cache behaviour

I use ehcache and hibernate 3.6.7 Final. This a pseudo code sample that reveals problem with caching.

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class A{    

    long id;    

    @OneToMany(mappedBy = "aId", targetEntity = B.class, fetch = FetchType.LAZY)
    @Fetch(value = FetchMode.JOIN)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    protected Set<B> fieldB;
}    


@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class B { 

    long id;

    long bId; 
}

1) First time when I load entity A from hibernate it does not read fieldB. And this is ok - cause FetchType.LAZY is set.

2) Second time when I load entity A I see sql queries retrieving entity A JOIN entity B.

3)If remove @Fetch(value = FetchMode.JOIN) point 2 will not be performed.

So the question is this bug or feature? And how can I avoid such latent things.

Upvotes: 1

Views: 190

Answers (1)

kentonue
kentonue

Reputation: 91

You have two conflicting fetches, you definitely do not want to specify the fetch on the column and the @Fetch annotation as it will provide unpredicatable behavior.

Upvotes: 1

Related Questions