Hanno Opperman
Hanno Opperman

Reputation: 143

Correct way of using CRUD in JSF

I need advice on a few design principles regarding CRUD operations in my JSF project.

A very simple example:

I have a basic screen with a form that get submitted. In my bean I declare a database connection in my method and a string object which I populate with my script. I modify the string to get the data that have been submitted in the form. This is the way I was taught do it, but I'm suspecting it's not based on solid principles.

So I decided to start using prepared statements. Seems a bit better, but still not perfect in my mind.

My question is: instead of writing a new script for each CRUD method, is it better to perhaps create stored procedures instead, in my mind it looks like much neater code and perhaps has better readability.

Or is there an entirely different way of doing things? The only concerns I have is a very fragile OLTP database.

Upvotes: 0

Views: 253

Answers (2)

Leo
Leo

Reputation: 6570

I don't like the idea of using stored procedures because they're hard to port and usually also hard to debug.

I've been working for years with something like this

  1. JSF -> xhtml + @ViewScoped managed bean to accomodate the values
  2. Stateless EJB for transactional methods called from managed beans
  3. Entity DAOs, called from EJBs, reusing basic CRUD methods with generics. I think JPA here is great, specially when they use metamodel type-safe criteria (http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html)

Nowadays, it´s been easier to work with lightweight JavaEE stacks such as apache TomEE than using prepared statements.

Upvotes: 2

user3222718
user3222718

Reputation: 242

Your JSF,s should always redirect to a servlet which calls a service method, where you write all your business logic and call your Data Access Object to execute required sql query. U should never use your bean for database connection... You should use DataSource for your data base Connection. And yes a simple preparedStatement is enough to do. You should convert all your strings in your servlet only and then pass it to the next layer with the help of your bean which has your setters and getters for all your form fields.. And your DAo contains all the CRUD operations.

Upvotes: 2

Related Questions