i have a problem whit JPA.
my save method is marked @Transactional like this:
@Transactional(propagation = Propagation.REQUIRED)
public void push(long id) {
Parent parent = dao.findParentById(id);
setParentMeta(parent);
Country country = dao.findCountry(id);
setParentChild(parent);
User user = dao.findUser(id);
dao.update(parent);
}
public void setParentMeta(Parent parent){
parent.setTitle("toto");
parent.setdescription("some description");
}
public void setParentChild(Parent parent){
List<Child> childList = new ArrayList<>();
for (String date : dao.getList()) {
Child child = new Child();
ChildPK childPK = new ChildPK();
childPK.setProductId(parent.getId());
childPK.setDate(date);
child.setChildPK(childPK);
child.setParent(parent);
childList.add(child);
}
parent.setChildList(childList);
}
My parent entity :
public class Parent {
...
private long parentId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.LAZY, orphanRemoval = true)
private List<Child> childList;
...
}
My Child entity :
public class Child {
...
@EmbeddedId
protected ChildPK childPK;
@MapsId("parentId")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id", referencedColumnName = "parent_id", insertable = true, updatable = true)
private Parent parent;
...
}
@Embeddable
public class ChildPK {
....
@Basic(optional = false)
@Column(name = "parent_id")
private long parentId;
@Basic(optional = false)
@Column(name = "date")
@Temporal(TemporalType.TIMESTAMP)
private Date date;
....
}
my problem is when i delete @Transactional(propagation = Propagation.REQUIRED) from method "push" and put it in the dao all insert/update or rallback work perfectly :
@Transactional(propagation = Propagation.REQUIRED)
public E update(E entity) {
return entityManager.merge(entity);
}
but if i put this annotation in push methode and delete it from dao i get this exception:
Duplicate entry '3623238-2016-02-21 00:00:00' for key 'PRIMARY'
Error Code: 1062
Call: INSERT INTO child (date, parent_id) VALUES (?, ?)
bind => [2016-02-21 00:00:00.0, 3623238]
The problem here is eclipselink flush automacticly when child change. there are a why to flush at the end of push method ?
I found the problem. the transaction manger is not well configured here is the solution with code example