Ich habe ein Problem mit JPA.JPA/Spring Doppeleintrag für Schlüssel 'PRIMARY'
meine speichern Methode markiert @Transactional wie folgt aus:
@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);
}
Meine Muttergesellschaft:
public class Parent {
...
private long parentId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.LAZY, orphanRemoval = true)
private List<Child> childList;
...
}
My Child Einheit:
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;
....
}
mein Problem ist, wenn ich löschen @ Transaktional (Propagation = Propagation.REQUIRED) aus der Methode "push" und in den DAO einfügen/update oder rallback wo rk perfekt:
@Transactional(propagation = Propagation.REQUIRED)
public E update(E entity) {
return entityManager.merge(entity);
}
aber wenn ich diese Anmerkung in Push-Methode setzen und löschen Sie es aus dao bekomme ich diese Ausnahme:
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]
Das Problem hier ist Eclipse bündig automacticly wenn Kind ändern. gibt es einen Grund, am Ende der Push-Methode zu spülen?
Ich kann findByParentId außerhalb Push-Methode setzen –
bitte klären, warum Sie das nicht tun können? – Ibrahim
da ändert sich nichts, es gibt auch einige finden Sie unten, das Problem hier ist, wenn ich alle löschen und direkt zu aktualisieren, dass die Arbeit löschen. und für meine Behandlung muss ich tun, bevor Sie –