Datatable does not populate object? JSF

Asked

Viewed 236 times

2

I have the following problem:

I have a DataTable which is populated with entities of the type Login that belongs to Usuario as a relationship.

A button called Add Login calls the method addLogin() of Login and a row is added in the table with fields for popular Login.user , Login.pass, Login.mac and Login.Plano. Plano already has to be persisted previously in the database for a list to be displayed with selectOneMenu.

What happens is when you add more than one Login, only the first is populated with the reference of Plano the others accuse an error because the same cannot be null.

Erro quando tenta persistir

Login

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "user"))
public class Login implements Autenticavel{

@Id
@GeneratedValue
private Long id;

@ManyToOne
@NotNull
private Usuario usuario;

@ManyToOne
@NotNull
private Plano plano;

@NotNull
private String user;

@NotNull
private String pass;

private String mac;

//GETTERS E SETTERS
}

User

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "rg"))
public class Usuario implements AbstractEntity{

@Id
@GeneratedValue
private Long id;

@NotNull
@Size(min = 1, max = 150)
@Pattern(regexp = "[^0-9]*", message = "Não deve conter números")
private String nome;

@NotNull
private String cpfCnpj;

@NotNull
private String rg;

@Temporal(TemporalType.DATE)
private Date dataNascimento;

@OneToOne(cascade = CascadeType.ALL)
private Contato contato;

@OneToOne(cascade = CascadeType.ALL)
private Endereco endereco;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "usuario_id")
private List<Login> login;

@Enumerated
private TipoPessoa tipoPessoa;

@Enumerated
private TipoPrevilegio previlegio;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "usuario_id")
private List<Cobranca> cobranca;

// GETTERS E SETTERS

public void setLogin(List<Login> login) {
    this.login = login;
}

public void addLogin(Login login){
    if(this.login == null)
        this.login = new ArrayList<Login>();
    this.login.add(login);
}

public void addLogin(){
    this.addLogin(new Login(this)); 
}

public void removeLogin(Login login){
    if(this.login.contains(login))
        this.login.remove(login);
}
}

View

<ui:composition xmlns="http://www.w3.org/1999/xhtml"  
            xmlns:h="http://java.sun.com/jsf/html"  
            xmlns:f="http://java.sun.com/jsf/core"  
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:p="http://primefaces.org/ui" 
            template="WEB-INF/templates/default/main.xhtml"
            xmlns:b="http://bootsfaces.net/ui">
<ui:define name="content">                        
    <h1 class="page-header "> <i class="fa fa-tachometer"></i> Dashboard</h1>
    <ol class="breadcrumb">
        <li class="active">Inicio</li>
    </ol>
    <h:form>
    <b:messages styleClass="messages"
           errorClass="invalid" infoClass="valid"
           warnClass="warning" globalOnly="true"/>
    <h:panelGrid columns="3" columnClasses="titleCell">
           <h:dataTable id="tabelaLogin" value="#{testController.usuario.login}" var="l"
              styleClass="table table-striped table-bordered">
              <h:column>
                 <!-- column header -->
                 <f:facet name="header">Usuário</f:facet>
                 <!-- row record -->
                 <b:inputText id="user" value="#{l.user}" />
              </h:column>
              <h:column>
                 <f:facet name="header">Senha</f:facet>
                 <b:inputText id="pass" value="#{l.pass}" />
              </h:column>
              <h:column>
                 <f:facet name="header">MAC</f:facet>
                 <b:inputText id="mac" value="#{l.mac}" />
              </h:column>
              <h:column>
                 <f:facet name="header">Plano</f:facet>
                 <b:selectOneMenu id="plano" name="plano" value="#{l.plano}" converter="planoConverter">
                    <f:selectItems value="#{planoController.findAll()}" var="pl" itemLabel="#{pl.nome}" itemValue="#{pl}"/>
                 </b:selectOneMenu>
                 <h:message for="plano" errorClass="invalid" />
              </h:column>
              <h:column>
                 <f:facet name="header">#</f:facet>
                 <b:commandButton action="#{testController.usuario.removeLogin(l)}" value="Apagar" look="danger" />
              </h:column>
           </h:dataTable>
           <b:commandButton value="Add Login" actionListener="#{testController.usuario.addLogin()}"  update="tabelaLogin" look="success" />
           <b:commandButton value="Salvar" actionListener="#{testController.salvar()}"  update="tabelaLogin" look="success" />
        </h:panelGrid>
        </h:form>
</ui:define>

How can I solve this problem?

2 answers

3

Problem solved! I discovered that the problem was in the Bootsfaces library, by changing the <b:selectOneMenu /> for <h:selectOneMenu /> everything worked perfectly!

  • 1

    A tip, bootsFaces has many problems, take care of that library.

  • Thank you! I’ll stay tuned.

2

Brother, have you ever tried to adopt an Id? //example @Id @Generatedvalue(Strategy=Generationtype.IDENTITY) // in case you are using a mysql BD.

ps. vc could add your persistence.xml file to improve understanding of its configuration.

  • I defined Strategy, but that didn’t solve it. I believe that to solve this problem, I will have to for each object of type Login, make the fields appear dynamically with jsf.

Browser other questions tagged

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