Avoiding duplication of model code with JSF and JPA

I am new to JSF and I wonder if I understood correctly. Say I have a simple CMS that allows you to write pages.

First, I define a JPA object named 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 to create a page. For this, it seems to me that I need a bean page. At the moment, I did such things:

@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 this: given that my JPA entity model and the model I want to use in the views are exactly the same in most cases, is there a way to avoid creating a batch of getters and setters in PageBean?

- , bean JPA JSF- bean ( JSF , JPA), , . , beans, , .

+3
2

[...], , JPA , , , PageBean?

Entity, . JSF. , , , , . , "", , , .., , .

- , bean JPA JSF- bean ( JSF , JPA)

, , - ( ) , - .

, :

+4

. . - .

bean - View to Domain. , MVC.

JPA- bean, MVC. , String Date String, (.. ), JPA ? , .

, , ? , ? (, String ). , , bean, , , . JPA. JPA, ( ).

, (, )? JPA? JPA bean ( bean ) .

,

#{myBean.page.title}

, , getPage() bean.

+2

Source: https://habr.com/ru/post/1754366/


All Articles