Reputation: 1372
I work with netbeans, and as I was headed toward creating a bean for the app-user's administration, I started wondering : which way would be more beneficial, persisting a DB table and applying an EJB on it or creating a bean that handles the connection with DB and does the tests "by hand" (something of a homegrown bean for CRUD operations on users ) ?
I acknowledge that the question is broad, but being a beginner, I had to ask for what way would be easier or safer for me in the futur.
Plus : any API on JSF that handles CRUD operations on a user table, with all the required tests (for example users with exact names and passwords) and such ?
Thanks.
EDIT : I noticed there are few examples of CRUD for JSF in Netbeans (working with 7.1.2), and I think one of them is addressed to CRUD on Users, but I don't know how much you would trust working with those example in a real life situation, kind of saying are they safe and refined enough ?
Upvotes: 1
Views: 103
Reputation: 27516
Nowadays you have two options for your backend - CDI and EJB. I guess you don't need things like transactions and RMI, so CDI will do the job for you (it's pretty similar to JSF but it's adding more features to it, JSFs Managed Beans will hopefully become obsolete soon).
Your UserBean visible to the JSF page
@Named
@RequestScoped //this depends on your use case
public UserBean {
@Inject
private UserDao dao;
public void addUser(User user) {
dao.addUser(user);
}
}
And your UserDao bean could like this
@RequestScoped
public UserDao {
@PersistencyContext
private EntityManager em;
public void addUser(User user) {
em.save(user);
}
}
Of course, this example is not perfect and it has it's downsides but you need to something to begin with and CDI and JSF is definitely the way. Later you can add the EJBs. Also see this tutorial which will give a basic overview of what CDI and JSF can do together.
Upvotes: 1