为什么我的@ApplicationScope CDI bean没有更新? [英] Why my @ApplicationScope CDI bean not updated?
问题描述
在我的应用程序中,我有一个@ApplicationScoped
CDI bean,用于存储数据库中的一些信息:
In my application, I have an @ApplicationScoped
CDI bean to store some information from the database:
@Named
@ApplicationScoped
public class MrBean {
@EJB
private SoyaBean soyaBean;
private List<Toys> myToys;
@PostConstruct
public void prepareMrBean() {
this.myToys = soyaBean.getToys();
}
public void updateToys() {
this.myToys = soyaBean.getToys();
}
}
我也有一个AddToy.xhtml页面,可以简单地将一个玩具添加到数据库中.支持bean如下:
I also have a AddToy.xhtml page which would simply add a toy to the database. The backing bean is as following:
@Named
@RequestScoped
public class MrsBean {
@EJB
private SoyaBean soyaBean;
@Inject
private MrBean mrBean;
public void addToy() {
this.soyaBean.addToy();
this.mrBean.updateToys();
}
}
由于数据库中添加了一个新玩具,因此我想更新mrBean中的玩具列表.但是,即使mrsBean称为mrBean.updateToys()
,mrBean中的玩具列表也不会被更新.我还有另一个ViewToys.xhtml,它带有一个@RequestScoped
备用bean来查看玩具列表,但我没有看到该列表得到更新.
Since there is a new toy added to the database, I wanted to update the list of toys in mrBean. However, even though mrsBean called mrBean.updateToys()
, the list of toys in mrBean is not updated at all. I have another ViewToys.xhtml with a @RequestScoped
backing bean to view the list of toys and I didn't see the list get updated.
如果有人能给我有关如何解决此问题的建议,我将不胜感激.
I'd be very grateful if someone could give me an advice on how to tackle this problem.
更新:这是我的SoyaBean实现:
UPDATE: This is my SoyaBean implementation:
@Stateless
public class SoyaBeanImpl implements SoyaBean {
@PersistenceContext()
private EntityManager em;
@Override
public List<Toys> getToys() {
Query q = em.createQuery("SELECT T from Toys T");
return (List<Toys>) q.getResultList();
}
@Override
public void addToy() {
Toys newToy = new Toys();
em.persist(newToy);
}
}
更新2 如果有人能向我展示如何以除烦恼之外的其他方式实现同一目标,我也将非常感谢.
UPDATE 2 I'd also really appreciate if someone could show me how I can achieve the same goal in any ways other than my troubling way.
最诚挚的问候,
詹姆斯·特兰
推荐答案
问题是所有SQL查询的结果都已缓存.因此,即使我尝试刷新玩具列表,也只能收到旧结果.我通过将文件persistence.xml
中的选项Shared Cache Mode
设置为None
解决了此问题.
The problem was that the results of all SQL queries were cached. Hence, even though I tried to refresh the list of toys, it only received the old result. I solved this problem by setting the option Shared Cache Mode
in the file persistence.xml
to None
.
我认为完全不使用缓存不是一个好的选择.因此,如果有人可以告诉我如何在无需关闭缓存的情况下达到相同的效果,我将不胜感激.
I believe not using cache at all is not a good option. Hence, I'd be very grateful if someone could show me how I can achieve the same result without having to turn off cache.
这篇关于为什么我的@ApplicationScope CDI bean没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!