Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF with hibernate without spring-is it the right choice  RSS feed

 
Sunil Dsouza
Greenhorn
Posts: 5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to develop a new web application of medium complexity.Right now i am somewhat comfortable with JSF and hibernate.I have never used JSF and hibernate together before.I just wanted to ask if it is good practice to use JSF(for both view layer and handling business logic) and hibernate(for persistence) without spring as a middle layer.The reason why i am asking this is i don't know anything about spring framework and my customer wants the development of the application to start as early as possible.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the old saying goes, "There's never time to do it right, but there's always time to do it over".

You do not need to use Spring with Hibernate and JSF. Especially you do not need to do so if you enjoy debugging obscure issues with transactions, persistent resource leaks, and love to code the same boilerplate code over and over again (of course, if you're being paid by the hour, you probably do!)

JSF really shouldn't contain business logic. It doesn't have any specific support for such things, although if the business functions are simple enough, they can be part of the backing bean action methods. For more complex business functions, you should put them in their own javabeans. Not only will it encourage Separation of Concerns, but the very act of separation will make it easier to write offline tests for the business functions.
 
Sunil Dsouza
Greenhorn
Posts: 5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim for your suggestion.Now i am seriously thinking of having either EJB or spring to implement buisness layer.Which one of the two (Spring, EJB ) do you think works better when used with JSF and hibernate.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EJB is primarily about persistence. Only session EJBs are appropriate for business logic, and because of their particular characteristics, about the only cases when using a session EJB over a plain JavaBean make sense is if you want them to run in a secured and/or transaction-aware mode.

Spring can be used to inject business logic POJOs into a system. You don't actually need it for static connections. Although having plug-replaceable components does assist in testing.

Having said all that, a common architecture I use is JPA/Spring based and has 3 layers:

1. Persistable Entities (Entity EJBs or equivalent), Also known as the ORM Object Model.

2. Table-level persistence classes. In other words, DAOs.

3. Business-level persistence classes (services).

The third layer (persistence services) in my particular case often has persistence-related business logic in it. Persistence-independent business logic I do elsewhere (in POJOs). The persistence service classes are transaction-bound methods that deal with the CRUD functionality of sets of related persistence objects, delegating the per-table CRUD operations to the DAOs (within the same transaction).
 
Akaine Harga
Ranch Hand
Posts: 99
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on the application type you're working on. My experience says that in most of small-medium sized administrative applications JSF-PersistenceFramework-only solution actually is quite a good choice. Yes, it's not kosher as most purists say, but do you really need a middle layer when the only logic you have is CRUD based? Middle layer is the correct choice when you have heavy business logic o an architecture different from DD design pattern. In all other cases you could do the following:

1. Design you app using Domain Driven Design approach. This will result in an Entity based database and a bunch of Entity classes mirroring your DB on Java level.
2. On JSF level just use Entity beans' local instances mapping the form fields with Entity beans' fields.
3. Use EJB injection in managed beans to access persistence methods.
4. The only other thing your managed beans would contain besides entity beans would be view control variables (flags, temporal fields, etc.).
5. Try to keep one managed bean per view.
6. All additional extensive/utility logic put in some local utility classes.

So in the end you'll get a bunch of managed beans full of small action methods that would show/hide dialog windows, read/save/update/delete entities.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!