I have the following problem:
I have a DataTable
that is populated with entities of type Login
that belongs to Usuario
as relationship.
A button named Add Login
calls the method addLogin()
of Login
and a line is added in the table with fields to popular Login.user
, Login.pass
, Login.mac
and Login.Plano
.
Plano
already has to be persisted previously in the database so that a list is displayed with selectOneMenu
.
What happens is that when you add more than one Login
, only the first one is populated with the reference of Plano
. The others will cause an error because it can not be null
.
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 resolve this problem?