selectOneMenu with convert does not work

Asked

Viewed 83 times

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 "";
}
}

Image with the error shown: Campo da tela de cadastro com erro

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

Tela de cadastro de manutenção e itens de manutenção

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

  • @Erickluz I added the classes Vehicle, Maintenance and Maintenance

2 answers

0


you are using the variable manutencaoBean.veiculo as source list and output value. Try using the manutencaoBean.manutencao.veiculo for the output (example below).

<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="#{veiculo.placa}" />
</p:selectOneMenu>

  • Hello Marcus, I made the change suggested and now is giving the error java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity.

  • How you’re saving the entity?

  • I believe this error is occurring because I created a class called Itensmanutencao and the Manutencao class has a Onetomany relationship with that class. The items are not saving in the bank, and I believe it is because I do not know how to get the Maintenance code at the time of saving. I will edit the question by placing the image and these other classes for a better understanding.

  • Marcus, I edited the question by adding the Itensmanutencao class and what I added to the Manutencao and Manutencaobean class, and tried to explain what I’m trying to do.

  • From what I understand, you are creating a maintenance and have several maintenance items. I think you should not have 2 Beans managing this. Only the Manutencaobean would take care of everything. If you want the maintenance items to be persisted along with the maintenance, I suggest changing the Maintenance: @OneToMany(mappedBy="manutencao", fetch=FetchType.LAZY, cascade = CascadeType.ALL) private List<ItensManutencao> itensManutencao;

  • Marcus, I don’t understand what I should do, as only Manutencaobean should take care of it. What changes should I make? Attributes that are in Itensmaintenance must be in Maintenance?

  • I understood that this is a maintenance form. Each maintenance has several maintenance items. In this case, you have only one form to edit the Maintenance entity, along with its items. I think the most indicated is that bean control only this entity and the maintenance items are inserted via Scade, in a One-To-Many relationship. I suggest you check this link https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/

Show 2 more comments

0

I was able to solve the problem of displaying the value from the database. I did so in my selectOneMenu:

<h:outputLabel value="Veiculo: " />
	<p:selectOneMenu id="selectVeiculo" filter="true"
		value="#{manutencaoBean.veiculo}" converter="veiculoConverter">
		<f:selectItems value="#{manutencaoBean.veiculo}" var="veiculo"
			itemValue="#{veiculo}" itemLabel="#{veiculo.placa}" />
	</p:selectOneMenu>

But now I have another problem. Trying to save is giving the error:

javax.el.Elexception: /publico/manutencao.xhtml @11,66 value="#{manutencaoBean.veiculo}": Cannot Convert [br.com.oficina.veiculo.Veiculo@f 23f4348] of type [class br.com.oficina.veiculo.Veiculo] to [interface java.util.List]

I looked for ways to try to solve this other problem and the solutions I tried haven’t solved this problem yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.