使用ManyToMany关系时,Hibernate的二级缓存不会自动更新

时间:2014-05-07 03:50:30

标签: hibernate second-level-cache

我创建了2个类作为User&具有@ManyToMany关系的Profle如下:

课程资料

@Cache (usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"name"}))
public class Profile implements Serializable {

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity = User.class)
    @JoinTable(name = "User_Profile", joinColumns = { @JoinColumn(name = "profiles_id") }, inverseJoinColumns = { @JoinColumn(name = "user_id") })
    public Set<User> getUsers() {
       return m_users;
    }
}

和类用户

@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Entity
public class User implements Serializable {

    @Cache (usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "User_Profile", joinColumns = { @JoinColumn(name = "user_id") }, 
        inverseJoinColumns = { @JoinColumn(name = "profiles_id") })
public Set<Profile> getProfiles() {
    return m_profiles;
}

} 

现在,我更新个人资料的列表用户如下

public void saveProfile(Profile profile, List<User> users){ 

    profile.getUsers().clear();
// Make bulk users to profile

profile.getUsers().addAll(users);

    getSession().saveOrUpdate(profile);
} 

然后我调用getUser(userName),如下所示

public User getUser(String userName) {
   User user = createCriteria().add(Restrictions.eq("name", userName)).uniqueResult());

   // ISSUE HERE
   List<Profile> profile = user.getProfiles(); 
   // The cache still keep the old value of profiles in users. 
} 

问题是当我获得用户的个人资料时,它仍然保留用户的旧个人资料。但是,当我调用evict刷新缓存时,它工作正常。

有没有人和我一样面临同样的问题?在添加或删除新记录时,hibernate是否支持自动刷新缓存?

请帮忙!

0 个答案:

没有答案