2017-10-21 6 views
0

Je suis en train d'insérer des données à base de données Mon JSON d'entrée:Impossible d'insérer des données de l'enfant ainsi que des parents dans la table des enfants en utilisant JPA un à plusieurs cartographie

{ 
    "loop_ind": "TERM", 
    "callRedirectRicTgrpDetailsList": [{ 
     "ric": "RIC1", 
     "tgrp": "TGRP1", 
     "priority": 1 
    },{ 
     "ric": "RICX2", 
     "tgrp": "TGRP2", 
     "priority": 2 
    }] 
} 

et url J'envoie un plus de valeur est redirect_id comme « http://127.0.0.1:8080/myredirect/redirect/1080/ » ici est redirect_id 1080

Ainsi, le redirect_id et loop_ind sont communs pour les entités callRedirectRicTgrpDetailsList. Cela signifie que loop_ind et redirect_id sont liés aux entités parentes et que callRedirectRicTgrpDetailsList est une entité enfant.

Lorsque j'essaie d'insérer les détails ci-dessus, la 1ère liste est insérée avec succès [1080, TERM, RIC1, TGRP1,1]. Et la deuxième liste [1080, TERM, RIC2, TGRP2,2] n'insère pas. à cause de redirect_id est null.1080 est affecté pour la 1ère liste et la deuxième liste montre null.

schéma de base de données:

CREATE TABLE IF NOT EXISTS `callRedirect` (
`redirect_id` varchar(20) NOT NULL, 
`loop_ind` varchar(4) NOT NULL, 
PRIMARY KEY (`redirect_id`) 
); 

CREATE TABLE IF NOT EXISTS `callRedirect_ric_tgrp_details` (
`row_id` int(4) AUTO_INCREMENT, 
`redirect_id` varchar(20) NOT NULL, 
`ric` varchar(20) NOT NULL, 
`tgrp` varchar (20) NOT NULL, 
`priority` int NOT NULL, 
PRIMARY KEY (`row_id`) 
); 

En dao.java:

callRedirectList.setRedirect_id(redirect_id.trim()); //which is from URL 
entityManager.getTransaction().begin(); 
entityManager.merge(callRedirectList); 
entityManager.getTransaction().commit(); 

Callredirect.java:

import java.util.ArrayList; 
import java.util.List; 

import javax.persistence.CascadeType; 
import javax.persistence.Cacheable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.OneToMany; 
import javax.persistence.QueryHint; 
import javax.persistence.Table; 

import org.codehaus.jackson.annotate.JsonIgnore; 
import org.codehaus.jackson.annotate.JsonProperty; 
import org.eclipse.persistence.annotations.CascadeOnDelete; 
import org.eclipse.persistence.annotations.PrivateOwned; 



@Entity 
@Cacheable(false) 
@NamedQueries({ 
    @NamedQuery(name = "getCallRedirectById", query = "SELECT a FROM CallRedirect a WHERE a.redirect_id=:redirect_id", hints = @QueryHint(name = "eclipselink.refresh", value = "true")), 
    @NamedQuery(name = "getAllCallRedirect", query = "SELECT a FROM CallRedirect a order by a.redirect_id", hints = @QueryHint(name = "eclipselink.refresh", value = "true")) }) 

@Table(name = "CallRedirect") 
public class CallRedirect implements java.io.Serializable{ 


private static final long serialVersionUID = 1L; 

@Id 
@Column(name = "redirect_id") 
@JsonProperty("redirect_id") 
private String redirect_id; 

@Column(name = "loop_ind") 
@JsonProperty("loop_ind") 
private String loop_ind; 

@OneToMany(mappedBy = "callRedirect", cascade = {CascadeType.PERSIST,CascadeType.REMOVE}, fetch = FetchType.LAZY) 
@CascadeOnDelete 
/*@JoinColumn(name = "redirect_id") */ 
private List<CallRedirectRicTgrpDetails> callRedirectRicTgrpDetailsList; 


public CallRedirect(){ 
} 
public CallRedirect(String redirect_id, String loop_ind,List<CallRedirectRicTgrpDetails> callRedirectRicTgrpDetailsList){ 
    this.redirect_id = redirect_id; 
    this.loop_ind = loop_ind; 
    this.callRedirectRicTgrpDetailsList = callRedirectRicTgrpDetailsList; 
} 

public String getLoop_ind() 
{ 
    return loop_ind.trim(); 
} 

public void setLoop_ind(String loop_ind) 
{ 
    this.loop_ind = loop_ind.trim(); 
} 

public List<CallRedirectRicTgrpDetails> getCallRedirectRicTgrpDetailsList() 
{ 
    return this.callRedirectRicTgrpDetailsList; 
} 

public void setCallRedirectRicTgrpDetailsList(List<CallRedirectRicTgrpDetails> callRedirectRicTgrpDetails) 
{ 
    this.callRedirectRicTgrpDetailsList= callRedirectRicTgrpDetails; 
} 

/** 
* @return the redirectId 
*/ 
public String getRedirect_id() 
{ 
    return redirect_id.trim(); 
} 

/** 
* @param redirectId 
*   the redirectId to set 
*/ 
public void setRedirect_id(String redirect_id) 
{ 
    this.redirect_id = redirect_id.trim(); 
} 

@Override 
public String toString() 
{ 
    return "CallRedirect :: redirect_id: " + redirect_id + ", loop_ind : " 
      + loop_ind +", Ric_Tgrp Details : " + callRedirectRicTgrpDetailsList; 
} 

} 

CallRedirectRicTgrpDetails.java:

import javax.persistence.Cacheable; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.QueryHint; 
import javax.persistence.Table; 

import org.codehaus.jackson.annotate.JsonIgnore; 
import org.codehaus.jackson.annotate.JsonProperty; 


@Entity 
@Cacheable(false) 
@NamedQueries({ 
     @NamedQuery(name = "getCallRedirectDetailsById", query = "SELECT a FROM CallRedirectRicTgrpDetails a WHERE a.callRedirect.redirect_id=:redirect_id ORDER BY a.priority", hints = @QueryHint(name = "eclipselink.refresh", value = "true")), 
     @NamedQuery(name = "getAllCallRedirectDetails", query = "SELECT a FROM CallRedirectRicTgrpDetails a ORDER BY a.priority", hints = @QueryHint(name = "eclipselink.refresh", value = "true")) }) 

@Table(name = "CallRedirect_ric_tgrp_details") 
public class CallRedirectRicTgrpDetails implements java.io.Serializable{ 


    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "row_id") 
    private int id; 

    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL, CascadeType.REMOVE}) 
    //@JoinColumn(name = "redirect_id",insertable=false, updatable=false) 
    @JoinColumn(name = "redirect_id") 

    private CallRedirect callRedirect; 


    @Column(name = "ric") 
    @JsonProperty("ric") 
    private String ric; 

    @Column(name = "tgrp") 
    @JsonProperty("tgrp") 
    private String tgrp; 

    @Column(name = "priority") 
    @JsonProperty("priority") 
    private int priority; 
    public CallRedirectRicTgrpDetails(){ 

    } 
    public CallRedirectRicTgrpDetails(int priority, int id, String ric, String tgrp){ 
     this.priority = priority; 
     this.id = id; 
     this.ric = ric; 
     this.tgrp = tgrp; 
    } 
    @JsonIgnore 
    public CallRedirect getCallRedirect() 
    { 
     return this.callRedirect; 
    } 
    @JsonProperty 
    public void setCallRedirect(CallRedirect callRedirect) 
    { 
     this.callRedirect = callRedirect; 
     if (!this.callRedirect.getCallRedirectRicTgrpDetailsList().contains(this)) { 
      this.callRedirect.getCallRedirectRicTgrpDetailsList().add(this); 
     } 
    } 

    /** 
    * @return the ric 
    */ 
    public String getRic() 
    { 
     return ric.trim(); 
    } 

    /** 
    * @param ric 
    *   the ric to set 
    */ 
    public void setRic(String ric) 
    { 
     this.ric = ric.trim(); 
    } 

    /** 
    * @return the tgrp 
    */ 
    public String getTgrp() 
    { 
     return tgrp.trim(); 
    } 

    /** 
    * @param tgrp 
    *   the tgrp to set 
    */ 
    public void setTgrp(String tgrp) 
    { 
     this.tgrp = tgrp.trim(); 
    } 

    public int getId() 
    { 
     return id; 
    } 

    public void setId(int id) 
    { 
     this.id = id; 
    } 

    public int getPriority() 
    { 
     return priority; 
    } 

    public void setPriority(int priority) 
    { 
     this.priority = priority; 
    } 

    /*@Override 
    public String toString() 
    { 
     return "CallRedirectRicTgrpDetails ::RowId : " + id + ", RedirectId : " + callRedirect.getRedirect_id() + ", Ric : " + ric + ", Tgrp : " + tgrp 
       + ", Priority : " + priority; 
    }*/ 

} 
+0

Toutes les suggestions? – karthick

+0

json: '" ric ":" RICX2 "' vs 'RIC2', faute de frappe? éditer json si est. – pirho

Répondre

0

Il semble que vous ne puissiez pas accéder à tous les objets enfants et appeler setCallRedirect. Cascade magique ne gère pas que pour vous