Solved and edited correctly:
I can not include an invoice with the customer id. In this case the customer may have multiple invoices assigned to it.
I am able to register the NF correctly, but does not show the customer number that this invoice is linked to, the BD appears id, nf number, date and value but does not appear the customer number to whom it belongs. / p>
The invoice number field appears in the customer record and the value is null
.
Follow the files:
Fiscal Note
@Entity
public class NotaFiscal implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String numero;
private Double valor;
@Temporal(TemporalType.DATE)
private Calendar data = Calendar.getInstance();
@ManyToOne
@JoinColumn(name="id_cliente")
private Cliente cliente;
public NotaFiscal() {
}
// include the getter and client setter in the notaFiscal file
Customer
@Entity
public class Cliente {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String sobrenome;
@Email
@NotBlank
@Column(unique=true)
private String email;
private String senha;
private String cpf;
private String cep;
private String endereco;
private String numero;
private String bairro;
private String cidade;
private String estado;
private String ddd;
private String celular;
@OneToMany(mappedBy="cliente")
private Set<NotaFiscal> notas;
public Cliente() {
}
NotaFiscalBean
@Model
public class NotaFiscalBean {
private NotaFiscal notaFiscal = new NotaFiscal();
@Inject
private NotaFiscalDAO notaFiscalDAO;
@Inject
private ClienteDAO clienteDAO;
private Cliente cliente = new Cliente();
private Long idCliente;
@Transactional
public void gravar() {
Cliente cliente = clienteDAO.buscaPorId(idCliente);
notaFiscal.setCliente(cliente );
notaFiscalDAO.adiciona(notaFiscal);
clienteDAO.save(cliente);
this.notaFiscal = new NotaFiscal();
this.setCliente(new Cliente());
idCliente = null;
}
HTML
<h:form>
<div class="form-group row">
<label for="nomecliente" class="col-sm-8 col-form-label">
Identificação do Cliente (digite o numero do cliente)</label>
<div class="col-sm-10"><br></br>
<h:inputText id="nomecliente" value="#
{notaFiscalBean.idCliente}" type="number" class="form-
control"
a:autofocus="true" />
</div>
</div>
<h:commandButton value="Cadastrar" type="submit" class="btn btn-warning"
action="#{notaFiscalBean.gravar}"></h:commandButton>
</h:form>
NotaFiscalDAO
public class NotaFiscalDAO implements Serializable {
private static final long serialVersionUID = 1L;
@PersistenceContext
private EntityManager manager;
public void adiciona(NotaFiscal nota) {
manager.persist(nota);
}
public List<NotaFiscal> listaTodos() {
CriteriaQuery<NotaFiscal> query =
manager.getCriteriaBuilder().createQuery(NotaFiscal.class);
query.select(query.from(NotaFiscal.class));
List<NotaFiscal> lista = manager.createQuery(query).getResultList();
return lista;
}
public int contaTodos() {
long result = (Long) manager.createQuery("select count(n) from
NotaFiscal n").getSingleResult();
return (int) result;
}
public List<NotaFiscal> listaTodosPaginada(int firstResult, int maxResults)
{
CriteriaQuery<NotaFiscal> query =
manager.getCriteriaBuilder().createQuery(NotaFiscal.class);
query.select(query.from(NotaFiscal.class));
List<NotaFiscal> lista =
manager.createQuery(query).setFirstResult(firstResult)
.setMaxResults(maxResults).getResultList();
return lista;
}
}
ClientData searching for client id in DB
public Cliente buscaPorId(Long id) {
Cliente cliente = manager.find(Cliente.class, id);
return cliente;
}