I have some jTextfFields
where I need to send to DAO
its values, these values are arriving correctly in DAO
, however the return of NameQuery
is being null
. There is no error, only the return is null
, consequently it does not write to the bank.
What's strange (for me) is that after a lot of trying, I decided to test and found that if I set the values so they already start filled in jTextField
, the return of DAO
is not null
, but due return.
In the case below the "number" is the String
that came from jTextField
and was sent to DAO
, and null
is the return of NamedQuery
. p>
numero recebido: 5555
dao: null
In this case, manually set to start the sout
completed. The return is correct and writes to the bank.
numero recebido: 5555
dao: 5555
Below that I found it important to add so that someone can help in the question of why this behavior and help correct the errors that may be causing this, or even other errors. Thanks
jTextField
Car
@Entity
@DynamicUpdate(value = true)
@NamedQueries({
@NamedQuery(name = "Carro.findAll", query = "SELECT distinct c FROM Carro c"),
@NamedQuery(name = "Carro.findByCarroId", query = "SELECT c FROM Carro c WHERE c.id = :id"),
@NamedQuery(name = "Carro.findByCarroIdAndDescricao", query = "SELECT c FROM Carro c WHERE c.id = :id and c.numero = :numero"),
@NamedQuery(name = "Carro.findByCarroNumero", query = "SELECT c FROM Carro c WHERE c.numero = :numero")})
public class Carro implements EntidadeBase, Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idCarro")
private Integer id;
@Column(name = "numeroCarro")
private String numero;
Method in Entidade
to fetch the value of DAO
public Carro buscarPorNumero(String numero) {
System.out.println("NUMERO RECEBIDO: " + numero);
EntityManager em = getEm();
Carro carro = null;
try {
carro = (Carro) em.createNamedQuery("Carro.findByCarroNumero")
.setParameter("numero", numero)
.getSingleResult();
} catch (NoResultException | NonUniqueResultException nre) {
JOptionPane.showMessageDialog(null, nre.getMessage());
}
System.out.println("DAO: " + carro);
return carro;
}
Method to save, in jTextField
, I left only the code related to the specific issue to not pollute much.
public void salvar() {
CarroDao carroDao = new CarroDao();
Locall locall = new Locall();
Envio envio = new Envio();
List<Carro> carros = new ArrayList();
List<String> lista = new ArrayList();
if (jTextField1.getText().trim().equals("") == false) {
lista.add(jTextField1.getText());
System.out.println("PRIMEIRO:" + jTextField1.getText());
}
if (jTextField2.getText().trim().equals("") == false) {
lista.add(jTextField2.getText());
System.out.println("SEGUNDO:" + jTextField2.getText());
}
if (jTextField3.getText().trim().equals("") == false) {
lista.add(jTextField3.getText());
System.out.println("TERCEIRO:" + jTextField3.getText());
}
if (jTextField4.getText().trim().equals("") == false) {
lista.add(jTextField4.getText());
System.out.println("QUARTO:" + jTextField4.getText());
}
for (int i = 0; i < lista.size(); i++) {
carros.add((Carro) carroDao.buscarPorNumero(lista.get(i)));
System.out.println("Carro adicionado: " + lista.get(i));
}
envio.setCarros(carros);
try {
EnvioDao ci = new EnvioDao();
ci.save(envio);
JOptionPane.showMessageDialog(null, "Gravado !");
modelo.limpaLista();
preencherTabela();
} catch (Exception erro) {
JOptionPane.showMessageDialog(null, "Erro na Gravação:" + erro);
}
refresh();
clearSelection();
}
update1
if (jTextField1.getText().trim().equals("") == false) {
lista.add(jTextField1.getText().trim());
System.out.println("PRIMEIRO:" + jTextField1.getText());
}