views:

50

answers:

2

I'm new to JSF and am wondering if I got things right. Let's say I have a simple CMS that makes it possible to write pages.

First, I define a JPA entity called Page:

@Entity
public class Page {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column
  private Long id;

  @Column private String title;

  @Column private String content;

  // getters & setters ...

}

Then I would like in a view to create Page-s. For that, it looks like I need a page bean of some sort. For now I handled things like this:

@Model
public class PageBean {

  private Page page = new Page();

    public String getTitle() {
      return page.getTitle();
    }

    public void setTitle(String title) {
      page.setTitle(title);
    }

    // rest of properties & getters & setters ...

    public void save() {
      // persist using EntityManager
    }
 }

My question is the following one: given that my JPA entity model and the model I want to use in the views are most of the time exactly the same, is there a way of avoiding to have to create a batch of getters & setters in the PageBean?

I read somewhere that you should not use a same bean as JPA entity and JSF model bean (because JSF does repeated calls to getters that may affect JPA), yet I do wonder if there is not a simpler way that would help avoiding this kind of code duplication. Especially when you've got an application with a large model and in many instances do not require anything special in the view beans, it looks like this can get quite cumbersome.

+1  A: 

It's not code duplication. The are no algorithms duplicated. The business logic is still in one place.

What your bean is doing is just connecting the View to the Domain model. This is good, it's part of the MVC pattern.

If you were using your JPA entity as your backing bean, you would be breaking the MVC pattern. For example, if one day instead of displaying a plain String you would need to add a Date to this String because the view requires so (i.e. interface requirements), are you going to write this view logic inside the JPA class? That does not make sense, mixing domain model and view model.

On the other hand, why the view has to know about how the domain is implemented? What if the domain values format change? (For example you save a timestamp String instead a date class in de Database for performance reasons). All you would need to do is just rewrite the method in the backing bean, it would take the timestamp and adapt it to a Date so everything would work as it was before. Just one change outside the JPA class. If you had it in the JPA class you would end up maintaining both logics in just one class (interface logic and domain logic).

What if you want to develop a new view (for example for mobile version)? Are you gonna add even more code to the JPA class? It would be better to keep the JPA as it was and create another Bean (that extends a common bean for both views) for the mobile version.

If after all this, you still want to not to write the getters and setters, you can do

#{myBean.page.title}

all you need is a getPage() inside the backing bean.

pakore
The `#{myBean.page.title}` is exactly what he need and would already be sufficient as whole answer at its own. Most JSF/EL-starters aren't aware of the ability to "deeplink" getters and thus think that it's necessary to flatten/hammer the entities in the managed bean.
BalusC
I know what he wanted from the begining. But I think that you should be able to deeplink once you are aware of what you are doing and its implications. That's why I wrote the "disclaimer" part before the answer. I use a lot of deeplinking within the backing beans, but when it comes to access the fields in the domain model, I think about it twice.
pakore
A: 

[...] given that my JPA entity model and the model I want to use in the views are most of the time exactly the same, is there a way of avoiding to have to create a batch of getters & setters in the PageBean?

I don't see the point of using a wrapper around an Entity and adding such a layer is indeed duplication. Just use the entity from your JSF page. Yes, this introduce some sort of coupling between the view and the domain but, in general, modifying the database usually means adding or removing fields on the view. In other words, I don't buy the "decoupling" argument and I've written enough extra layers, mapping code, boilerplate code, etc to favor the simple approach when possible.

I read somewhere that you should not use a same bean as JPA entity and JSF model bean (because JSF does repeated calls to getters that may affect JPA)

I'd be interested if you could provide a reference but a wrapper class (delegating calls to the entity) is not going to change anything if there is a problem somewhere.

Just in case, some additional resources:

Pascal Thivent