I ran into a removal problem with JPA and this is my code:
public deleteLine(int idLine) {
Line line = em.find(Line.class,idLine);
Header header = line.getHeader();
this.deleteLine(header,line);
}
public boolean deleteLine(Header header, Line line) {
try {
line.setIdArticle(null);
line.setDetail(DELETED_TAG);
line.setQuantity(0.0f);
em.merge(line);
header.getLineCollection().remove(line);
em.remove(line);
em.flush();
}
catch (Exception ex) {
LOG.log(Level.SEVERE, null, ex);
}
return true;
}
When I call deleteLine(), I end the database row with idArticle nulldetails equal to DELETED_TAGconstant and quantityequal 0. But the string still exists, despite em.remove.
I try to add line.setHeader(null)before deleting, but I get ConstraintViolationExceptionit because the header field cannot be null.
Obviously, I am doing something wrong, but I cannot understand that.
Here is the entity code:
public class Header implements Serializable {
[...]
@OneToMany(cascade = CascadeType.ALL, mappedBy = "header")
private Collection<Line> lineCollection;
[...]
}
public class Line implements Serializable {
[...]
@JoinColumn(name = "header", referencedColumnName = "header")
@ManyToOne(optional = false)
private Header header;
[...]
}
I ran tests by deleting a row using JPQL ( DELETE FROM Linea WHERE idLinea=?) and it effectively deletes the row. But then, when the JPA performs a commit, the line appears again due to the INSERTexecution of the JPA.
, ? , , , , , , ?