使用JPA一对多映射

时间:2017-10-21 06:36:52

标签: java json jpa eclipselink

我正在尝试将数据插入数据库 我的输入json:

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

并在url中我再发送一个值为redirect_id 比如“http://127.0.0.1:8080/myredirect/redirect/1080/” 这里redirect_id是1080

因此redirect_id和loop_ind对于callRedirectRicTgrpDetailsList实体是通用的。这意味着与父实体相关的loop_ind和redirect_id以及callRedirectRicTgrpDetailsList是子实体。

当我尝试插入上述细节时,第一个列表成功插入[1080,TERM,RIC1,TGRP1,1]。并且第二个列表[1080,TERM,RIC2,TGRP2,2]未插入。因为redirect_id为第一个列表分配了空.1080,第二个列表显示为空。

数据库架构:

    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`)
);

在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;
    }*/

}

1 个答案:

答案 0 :(得分:0)

看起来您可能无法转到所有子对象并调用setCallRedirect。 Cascade magic不能为你处理