Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are mixing the responsibilities of EJBs and JSF managed beans. The <code>faces-config.xml</code> registers only JSF artifacts, such as managed beans and not EJBs. Your registration in <code>faces-config.xml</code></p> <pre><code>&lt;managed-bean&gt; &lt;managed-bean-name&gt;personManager&lt;/managed-bean-name&gt; &lt;managed-bean-class&gt;ejb.PersonManager&lt;/managed-bean-class&gt; &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt; &lt;/managed-bean&gt; </code></pre> <p>does basically exactly the same as </p> <pre><code>@ManagedBean @SessionScoped public class PersonManager { // ... } </code></pre> <p>In other words, you're registering the class as a JSF managed bean which is available in views by <code>#{personManager}</code>. This does not represent the same instance as is managed by the EJB container. You can and should <strong>not</strong> use <code>faces-config.xml</code> to register EJBs. There you use the annotations from the <code>javax.ejb</code> package for, such as <code>@Stateless</code> and <code>@Stateful</code>. That's all you need to register an EJB.</p> <p>Actually, registering JSF managed beans in <code>faces-config.xml</code> is an old JSF 1.x way which has in JSF 2.x been replaced by the new <code>@ManagedBean</code> annotation.</p> <hr> <p><strong>Update</strong> the proper approach would be:</p> <p>View (the Facelets file):</p> <pre><code>&lt;h:form&gt; &lt;h:inputText value="#{personManager.person.name}" /&gt; ... &lt;h:commandButton value="Save" action="#{personManager.save}" /&gt; &lt;h:messages /&gt; &lt;/h:form&gt; </code></pre> <p>Controller (the JSF managed bean):</p> <pre><code>@ManagedBean @ViewScoped public class PersonManager implements Serializable { private Person person; @EJB private PersonService personService; @PostConstruct public void init() { person = new Person(); } public void save() { personService.create(person); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Person successfully created, new ID is " + person.getId())); } // ... } </code></pre> <p>Model (the JPA entity):</p> <pre><code>@Entity public class Person implements Serializable { @Id private Long id; private String name; // ... } </code></pre> <p>Service (the stateless EJB):</p> <pre><code>@Stateless public class PersonService { @PersistenceContext private EntityManager em; public void create(Person person) { em.persist(person); } // ... } </code></pre>
 

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