I have a JPQL with @NamedQuery
that does not return errors, but also doesn´t update Database.
Here is the Entity.
@Entity
@Table(name = "restricao_decomp")
@NamedQueries({
@NamedQuery(name = RestricaoDecomp.UPDATE_BY_ID, query = "UPDATE RestricaoDecomp a SET a.nome = ?1, "
"a.comentar = ?2, a.revisada = ?3 where id = ?4"),
})
public class RestricaoDecomp {
public static final String UPDATE_BY_ID = "RestricaoDecomp.updateById";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column(name = "nome")
String nome;
@Column(name = "comentar")
String comentar;
@Column(name = "data")
LocalDate data;
@Column(name = "revisada")
Integer revisada;
public int getId() {
return id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getComentar() {
return comentar;
}
public void setComentar(String comentar) {
this.comentar = comentar;
}
public LocalDate getData() {
return data;
}
public void setData(LocalDate data) {
this.data = data;
}
public Integer getRevisada() {
return revisada;
}
public void setRevisada(Integer revisada) {
this.revisada = revisada;
}
}
This is the DAO:
@Stateless
public class RestricaoDecompDAO extends AbstractDAO<RestricaoDecomp, Integer> {
private static final long serialVersionUID = -5926717750595575580L;
public void updateById(String nome, String comentar, Integer revisada, Integer id) {
executeNamedQuery(RestricaoDecomp.UPDATE_BY_ID, nome, comentar, revisada, id);
}
}
And this is how it is called in the Service
... (The parameters are altered in the object. The debug shows it.)
restricaoDecompDAO.updateById(restricao.getNome(), restricao.getComentar(), 1, restricao.getId());
But... When the method updateById
is called, nothing happens... And I get no errors... It just doesn´t update, and doesn´t return any errors... What the hell is happening?
PS: I'm not using Spring, just JPA... It is driving me crazy! No errors, no update... PLEASE HELP! =) Thanks!
PS: I have other @NamedQueries
(not with UPDATE, but SELECT), that works just fine....
CodePudding user response:
I suggest this :
@Stateless
public class RestricaoDecompDAO extends AbstractDAO<RestricaoDecomp, Integer> {
@PersistenceContext
private EntityManager em;
public void updateById(String nome, String comentar, Integer revisada, Integer id) {
if(id == null) return;
var o = em.find(RestricaoDecomp.class, id);
if(o == null) return;
o.setNome(nome);
o.setComentar(comentar);
o.setRevisada(revisada);
}
}
CodePudding user response:
I managed to get it working. This is what I did:
public void update(RestricaoDecomp rd, RestricaoDecompDAO dao) {
if (rd == null)
return;
RestricaoDecomp rdDB = dao.findById(rd.getId());
if (rdDB == null)
return;
rdDB.setComentar(rd.getComentar());
rdDB.setRevisada(1);
rdDB.setNome(rd.getNome());
dao.update(rdDB);
}
The Object in first method param contains the changes I wanted to persist, and the dao is an object which is @Inject
in the Service.
The AbstractDAO
extending class has an update method, that finally worked (But if I called the update from Abstract right from the service, it didn´t worked either.
The AbstractDAO implemented the @PersistenceContext
and EntityManager
I believe that somehow it was a problem with the UPDATE statement of the Query, since the SELECTs were working just fine.
Well, that's it! =)