JPA Entity as JSF Bean?

You could do so. It’s technically possible. But it does (design)functionally not make any sense. You’re basically tight-coupling the model with the controller. Usually the JPA entity (model) is a property of a JSF managed bean (controller). This keeps the code DRY. You don’t want to duplicate the same properties over all place, let alone annotations on those such as bean validation constraints.

E.g.

@ManagedBean
@ViewScoped
public class Register {

    private User user;

    @EJB
    private UserService service;

    @PostConstruct
    public void init() { 
        user = new User();
    }

    public void submit() {
        service.create(user);
    }

    public User getUser() {
        return user;
    }

}

with this Facelets page (view):

<h:form>
    <h:inputText value="#{register.user.email}" />
    <h:inputSecret value="#{register.user.password}" />
    <h:inputText value="#{register.user.firstname}" />
    <h:inputText value="#{register.user.lastname}" />
    ...
    <h:commandButton value="Register" action="#{register.submit}" />
</h:form>

See also:

Leave a Comment