0
I’m getting this Exception while doing the remove, already broke my head looking for what and
Exception in thread "main" java.lang.Runtimeexception: Error Interpreting query [delete from Book Where id = 3]; this may indicate a semantic (user query) problem or a bug in the parser at application.dao.Daogenerico.removeById(Daogenerico.java:39) at application.TestClass.main(Testclass.java:30)
NOTE: The book with ID 3 exists, the operations of UPDATE, SAVE are working perfectly.
Follows the class of DAO:
public class DaoGenerico<T extends BaseEntity> {
private static EntityManager em = Connection.getConnection("integrator");
public T findById(Class<T> clazz, Long id) {
return em.find(clazz, id);
}
public void save_update(T entity) {
try {
em.getTransaction().begin();
if (entity.getId() == null)
em.persist(entity);
else
em.merge(entity);
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
}
}
public void removeById(Class<T> clazz, Long id) {
try {
Query query = em.createQuery("delete from " + clazz.getSimpleName() + " where id = " + id);
query.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
I have also tried to use directly with entitymanager:
public void save_update(T entity) {
try {
em.getTransaction().begin();
if (entity.getId() == null)
em.persist(entity);
else
em.merge(entity);
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
}
}
The entity:
package application.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tb_livro")
public class Livro implements Serializable, BaseEntity {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nome;
private String editora;
private Integer edicao;
private String autor;
public Livro() {
}
public Livro(Long id, String nome, String editora, Integer edicao, String autor) {
super();
this.id = id;
this.nome = nome;
this.editora = editora;
this.edicao = edicao;
this.autor = autor;
}
@Override
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEditora() {
return editora;
}
public void setEditora(String editora) {
this.editora = editora;
}
public Integer getEdicao() {
return edicao;
}
public void setEdicao(Integer edicao) {
this.edicao = edicao;
}
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Livro other = (Livro) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
The test class:
package application;
import java.util.List;
import application.dao.DaoGenerico;
import application.domain.Livro;
import javafx.collections.ObservableList;
public class TestClass {
public static void main(String[] args) {
DaoGenerico<Livro> repo = new DaoGenerico<Livro>();
Livro livro = new Livro(null, "livro 3", "ed", 3, "autor");
repo.save_update(livro);
List<Livro> allLivros = repo.getList(Livro.class);
for(Livro l : allLivros) {
System.out.println(l.getNome());
}
ObservableList<Livro> obLivro = repo.convertToObservable(allLivros);
System.out.println("Observable:");
for(Livro l2 : obLivro) {
System.out.println(l2.getNome());
}
repo.removeById(Livro.class, 3L);
}
}
The whole project is in git: https://github.com/ragnarloth2/project-integrator/