Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy my @ApplicationScope CDI bean not updated?
    primarykey
    data
    text
    <p>In my application, I have an <code>@ApplicationScoped</code> CDI bean to store some information from the database:</p> <pre><code>@Named @ApplicationScoped public class MrBean { @EJB private SoyaBean soyaBean; private List&lt;Toys&gt; myToys; @PostConstruct public void prepareMrBean() { this.myToys = soyaBean.getToys(); } public void updateToys() { this.myToys = soyaBean.getToys(); } } </code></pre> <p>I also have a AddToy.xhtml page which would simply add a toy to the database. The backing bean is as following:</p> <pre><code>@Named @RequestScoped public class MrsBean { @EJB private SoyaBean soyaBean; @Inject private MrBean mrBean; public void addToy() { this.soyaBean.addToy(); this.mrBean.updateToys(); } } </code></pre> <p>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 <code>mrBean.updateToys()</code>, the list of toys in mrBean is not updated at all. I have another ViewToys.xhtml with a <code>@RequestScoped</code> backing bean to view the list of toys and I didn't see the list get updated.</p> <p>I'd be very grateful if someone could give me an advice on how to tackle this problem.</p> <p><strong>UPDATE</strong>: This is my SoyaBean implementation:</p> <pre><code>@Stateless public class SoyaBeanImpl implements SoyaBean { @PersistenceContext() private EntityManager em; @Override public List&lt;Toys&gt; getToys() { Query q = em.createQuery("SELECT T from Toys T"); return (List&lt;Toys&gt;) q.getResultList(); } @Override public void addToy() { Toys newToy = new Toys(); em.persist(newToy); } } </code></pre> <p><strong>UPDATE 2</strong> 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. </p> <p>Best regards,</p> <p>James Tran </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload