-1
I’m having trouble loading a user’s data into the form.
Man selectOneMenu is loaded(correctly) with a list of guys however the TYPE object entered in the User record is not being selected during editing.
Summary: I have a user class and each user can be of various types. ( Admgeral/ Admgerencia / Admcompras / Userbasico / etc. ) I can register new users without problems but I’m having difficulty editing an existing record due to the "Selectonemenu" component that is not selecting the value saved in the database.
I made a reduced version of the problem to facilitate and assist others.
Java User Class.
@Entity
public class Usuario{
@Id
@GeneratedValue(generator="user_id_seq")
@SequenceGenerator(name="user_id_seq",sequenceName="user_id_seq",allocationSize=1)
private Integer id;
private String nome;
@OneToOne
private Tipo tipo
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Tipo getTipo() {
return this.tipo;
}
public void setTipo(Tipo tipo) {
this.tipo = tipo;
}
public String getNome(){
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
Class Type.java
@Entity
public class Tipo{
@GeneratedValue(generator="tipo_id_seq")
@SequenceGenerator(name="tipo_id_seq",sequenceName="tipo_id_seq",allocationSize=1)
private Integer id;
private String nome;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome(){
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
View User.xhtml
<h:form id="form_user">
<p:panelGrid columns="2">
<p:outputLabel value="ID" />
<p:inputText disabled="true" value="#{usuarioBean.usuario.id}" />
<p:outputLabel value="Nome" />
<p:inputText value="#{usuarioBean.usuario.nome}" />
<p:outputLabel value="ID" />
<p:selectOneMenu value="#{usuarioBean.usuario.tipo}" required="true">
<f:selectItem itemLabel="Select" />
<f:selectItems value="#{tipoBean.lista}" var="c" itemValue="#{c}" itemLabel="#{c.nome}" />
<f:converter converterId="entityConverter"/>
</p:selectOneMenu>
<p:commandButton value="New" icon="fa fa-plus" update="form_user" action="#{usuarioBean.newRecord}" />
<p:commandButton value="Save" icon="fa fa-save" update="form_user" action="#{usuarioBean.save}"/>
</p:panelGrid>
<p:dataTable var="user" value="#{usuarioBean.lista}">
<h:outputText value="#{user.id}"/>
<p:commandButton title="Edit" update="form_user" icon="fa fa-fw fa-edit" process="@this">
<f:setPropertyActionListener target="#{usuarioBean.usuario}" value="#{user}" />
</p:commandButton>
<p:commandButton action="#{usuarioBean.removeRecord}" update="form_user" icon="fa fa-fw fa-trash" immediate="true">
<f:setPropertyActionListener target="#{usuarioBean.usuario}" value="#{user}" />
</p:commandButton>
</p:dataTable>
</h:form>
Convert Entityconverter
@FacesConverter(value="entityConverter")
public class EntityConverter implements Converter {
public Object getAsObject(FacesContext ctx, UIComponent component,
String value) {
if (value != null) {
return component.getAttributes().get(value);
}
return null;
}
public String getAsString(FacesContext ctx, UIComponent component,
Object obj) {
if (obj != null && !"".equals(obj)) {
String id;
try {
id = this.getId(getClazz(ctx, component), obj);
if (id == null){
id = "";
}
id = id.trim();
component.getAttributes().put(id, getClazz(ctx, component).cast(obj));
return id;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
private Class<?> getClazz(FacesContext facesContext, UIComponent component) {
return component.getValueExpression("value").getType(
facesContext.getELContext());
}
public String getId(Class<?> clazz, Object obj) throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
for (Field field : clazz.getDeclaredFields()) {
if ((field.getAnnotation(Id.class)) != null) {
Field privateField = clazz.getDeclaredField(field.getName());
privateField.setAccessible(true);
if (privateField.get(clazz.cast(obj)) != null) {
return (String)field.getType()
.cast(privateField.get(clazz.cast(obj))).toString();
} else {
return null;
}
}
}
return null;
}
}
Completion:
1 - My Selectonemenu is being populated with database data correctly
2 - After I started using the generic converter(Entityconverter) I can save the user record in the database.
3 - When I open the record for editing it does not load the type saved in the database in Selectonemenu.
What I’m doing wrong?