Re: Prettyfaces + Hibernate + Spring lazy initialization

Splash Forums PrettyFaces Users Prettyfaces + Hibernate + Spring lazy initialization Re: Prettyfaces + Hibernate + Spring lazy initialization

#22554

I can only for myself, but I’m typically using the following pattern.

The entry point is a list view containing all the entities. The Java class typically looks like this:

@Named
@RequestScoped
@URLMapping(id = "userList", pattern = "/admin/users", viewId = "/admin/user-list.jsf")
public class UserListBean {

private List<User> list;

@URLAction
public void init() {
list = userDao.list();
}

/* getter + setter */

}

The dataTable contains a column with an edit link. This link refers to the edit page like this:

<h:dataTable var="user" value="#{userListBean.list}">

<!-- .... -->

<h:column>
<pretty:link mappingId="editUser">
<f:param value="#{user.id}" />
Edit
</pretty:link>
</h:column>

</h:dataTable>

This renders a simple HTML link. The bean responsible for the edit page looks like this:

@Named
@RequestScoped
@URLMapping(id = "editUser", pattern = "/admin/user/#{userEditBean.id}", viewId = "/admin/user-edit.jsf")
public class UserEditBean {

private Long id;

private User user;

private String firstname;
private String lastname;

@URLAction
public void init() {

// load user to edit from the DB
user = userDao.getById( id );

// load values to edit
firstname = user.getFirstname();
lastname = user.getLastname();

}

public String save() {

// update database object
user.setFirstname(firstname);
user.setLastname(lastname);

// back to list view
return "pretty:userList";
}

/* getter + setter */

}

Simple, isn’t it? :)

I typically prefer to have separate fields in the JSF bean which hold the properties of the database object that are editable. I prefer that because you can post process these values (like to a String.trim()) and do complex validations that aren’t possible with JSF validators. But you could also bind the input components directly to the database entity.

Christian