1
I have a selectOneMenu where I am populating with the database data but the same is coming with the memory address (see image), and not the value I want.
I tried to solve using the example of this video https://youtu.be/3XGwUTPJ9x8?list=PL_GwGUsBlNyfI0W3ggfffhBdJUqB4981Z in addition to similar questions asked on forums and other research sources, but without success and do not know where I am missing.
Below follows the code of my selectOneMenu and convert.
My choice:
<h:outputLabel value="Veiculo: " />
<p:selectOneMenu id="selectVeiculo" filter="true"
value="#{manutencaoBean.manutencao.veiculo}"
converter="veiculoConverter">
<f:selectItems
value="#{manutencaoBean.veiculo}" var="veiculo"
itemValue="#{veiculo}" itemLabel="#{veiculoBean.veiculo.placa}"/>
</p:selectOneMenu>
My convert:
@FacesConverter(value="veiculoConverter", forClass = Veiculo.class)
public class VeiculoConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && value.trim().length() > 0) {
Integer codigo = Integer.valueOf(value);
try {
VeiculoRN veiculoRN = new VeiculoRN();
return veiculoRN.carregar(codigo);
} catch (Exception e) {
throw new ConverterException("Não foi possível encontrar a categoria de código "
+ value + ". " + e.getMessage());
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null) {
Veiculo veiculo = (Veiculo) value;
return veiculo.getCodigo_veiculo().toString(); }
return "";
}
}
Vehicle Class:
@Entity
public class Veiculo implements Serializable {
private static final long serialVersionUID = -5574236691480093866L;
@Id
@GeneratedValue
private Integer codigo_veiculo;
private String placa;
@OneToMany(mappedBy="veiculo", fetch=FetchType.LAZY)
private List<Manutencao> manutencao;
//gets sets e hashcode e equals
Maintenance class:
@Entity
public class Manutencao implements Serializable {
private static final long serialVersionUID = 9031330110535169652L;
@Id
@GeneratedValue
private Integer cod_manutencao;
@ManyToOne
@JoinColumn(name = "codigo_veiculo")
private Veiculo veiculo;
@OneToMany(mappedBy="manutencao", fetch=FetchType.LAZY)
private List<ItensManutencao> itensManutencao;
//gets sets e hashcode e equals
Class Manutencaobean:
@ManagedBean(name = "manutencaoBean")
@RequestScoped
public class ManutencaoBean {
private Manutencao manutencao = new Manutencao();
private List<Manutencao> lista;
private List<Veiculo> veiculo;
private List<ItensManutencao> itensManutencao;
public String novo() {
this.manutencao = new Manutencao();
return "/publico/manutencao";
}
public String salvar() {
ManutencaoRN manutencaoRN = new ManutencaoRN();
manutencaoRN.salvar(this.manutencao);
return null;
}
public List<Manutencao> getLista() {
if (this.lista == null) {
ManutencaoRN manutencaoRN = new ManutencaoRN();
this.lista = manutencaoRN.listar();
}
return this.lista;
}
public List<Veiculo> getVeiculo() {
this.veiculo = new ArrayList<Veiculo>();
VeiculoRN veiculoRN = new VeiculoRN();
this.veiculo = veiculoRN.listar();
return veiculo;
}
public List<ItensManutencao> getItensManutencao() {
this.itensManutencao = new ArrayList<ItensManutencao>();
ItensManutencaoRN itensManutencaoRN = new ItensManutencaoRN();
this.itensManutencao = itensManutencaoRN.listar();
return itensManutencao;
}
//gets sets
Class Itensmanutencao:
@Entity
public class ItensManutencao implements Serializable {
private static final long serialVersionUID = -8496201153212695359L;
@Id
@GeneratedValue
private Integer cod_item_manutencao;
private String descricao;
private Integer quantidade;
private Float valorUnitario;
private Float total;
@ManyToOne
@JoinColumn(name = "cod_manutencao")
private Manutencao manutencao;
// gets sets hashcode e equals
Itensmanutencaobean class:
@ManagedBean(name = "itensManutencaoBean")
@RequestScoped
public class ItensManutencaoBean {
private ItensManutencao itensManutencao = new ItensManutencao();
private Manutencao manutencao = new Manutencao();
private List<ItensManutencao> lista;
public String novo() {
this.itensManutencao = new ItensManutencao();
return "/publico/itensmanutencao";
}
public String salvar() {
ManutencaoRN manutencaoRN = new ManutencaoRN();
manutencaoRN.salvar(this.manutencao);
ItensManutencaoRN itensManutencaoRN = new ItensManutencaoRN();
itensManutencaoRN.salvar(this.itensManutencao);
return null;
}
public String excluir() {
ManutencaoRN manutencaoRN = new ManutencaoRN();
manutencaoRN.salvar(this.manutencao);
ItensManutencaoRN itensManutencaoRN = new ItensManutencaoRN();
itensManutencaoRN.excluir(this.itensManutencao);
this.lista = null;
return null;
}
public List<ItensManutencao> getLista() {
if (this.lista == null) {
ItensManutencaoRN itensManutencaoRN = new ItensManutencaoRN();
this.lista = itensManutencaoRN.listar();
}
return this.lista;
}
//gets e sets
What I’m trying to do now is to add the maintenance items (Itensmanutencao class) and other information (top of the image, which corresponds to the Maintenance class).
When trying to add an item, the same is not saving, I believe that as there is a relationship between the tables, the table Manutencao still does not have a cod_manutencao.
I don’t know how I should add the items related to this maintenance form and associate these items that have been added with this maintenance.
Put your vehicle entity in question
– Erick Luz
@Erickluz I added the classes Vehicle, Maintenance and Maintenance
– Jorge