2016-06-23 8 views
0

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?

Antwort

0

Dieser Fehler tritt auf, wenn Sie versuchen, Objekt zu erhalten, bereits vorhanden, auf der anderen Seite, @Transactional (Propagation = Propagation.REQUIRED) verwenden Sie die bereits geöffnete Transaktion und öffnen Sie eine neue, wenn niemand geöffnet ist, denke ich Problem mit findByParentId Methode, so versuchen Sie es zu "finden" und senden Sie es dann die Push Methode.

+0

Ich kann findByParentId außerhalb Push-Methode setzen –

+0

bitte klären, warum Sie das nicht tun können? – Ibrahim

+0

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 –

Verwandte Themen