如何使用JPA更新父子行

时间:2016-06-28 08:48:33

标签: java mysql hibernate jpa

我有一个servlet,我将所有数据重新添加到子表和父表,如

public net.sf.json.JSONArray saveQuery(QueryBuilderRequestHelper helper){
    logger.log(Level.INFO," ###############  Save Query ###############  ");
    HttpSession session = null;
    session = helper.getHttpServletRequest().getSession();
    int physicianId=Integer.parseInt(helper.getPhysicianId());
    String title=helper.getTitle();
    String discription=helper.getDiscription();
    String condition=helper.getcondition();
    String rules=helper.getRules();
    String fields=helper.getFields();
    String query = (String) session.getAttribute("query");
     List<QueryBuilderCondition> conditionList = new ArrayList<QueryBuilderCondition>();
    net.sf.json.JSONObject jObject=new net.sf.json.JSONObject();
    net.sf.json.JSONArray jArray = new net.sf.json.JSONArray();
    String result="";
     try {
        JSONArray jsonArr = new JSONArray(rules);
        QueryBuilder builder =new QueryBuilder();
        builder.setCondition(condition);
        builder.setCreationTime(new Date());
        builder.setDiscription(discription);
        builder.setOutputFields(fields);
        builder.setPhysician(new PhysicianJpaController().findPhysician(physicianId));
        builder.setQueryTitle(title);
        builder.setSqlQuery(query);
         for (int i = 0; i < jsonArr.length(); i++) {
             JSONObject jsonObj = jsonArr.getJSONObject(i);
             QueryBuilderCondition qbCondition=new QueryBuilderCondition();
             qbCondition.setFieldId(jsonObj.getString("id"));
               qbCondition.setOperator(jsonObj.getString("operator"));
               qbCondition.setValue(jsonObj.getString("value"));
               qbCondition.setQuerybuilderId(builder);
               conditionList.add(qbCondition);
         }

         builder.setQueryBuilderConditionCollection(conditionList);
         System.out.println("==========conditionList========="+conditionList.toString()+ "======SIZE is ============="+conditionList.size());

        result=new QueryBuilderJpaController().saveQuery(builder);
        if(result.equals("success")){
            jObject.put("result", result);

        }
    }catch(Exception ex){
            result="error";
            ex.printStackTrace();
            logger.log(Level.INFO,"===============saveQuery==============="+ex.getMessage());
        }
    jArray.add(jObject);
    return null;
}

从这里我叫我的jpa控件

public String saveQuery(QueryBuilder builder) {
    logger.log(Level.SEVERE, " ############### saveQuery############### ");
    String result = "";
    try {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();

        em.persist(builder);
        //em.persist(builderCondition);
        em.getTransaction().commit();

        result = "success";
    } catch (Exception ex) {
        result = "error";
        ex.printStackTrace();
        logger.log(Level.SEVERE, "=======================" + ex.getMessage());
    }
    return result;
}

不知道但是这段代码给了我一个错误:

InsertObjectQuery(com.medikm.entity.QueryBuilder[ id=null ])
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)

请让我知道我在这里做错了什么.pelase让我知道是否需要任何克制 这里是queryBuilder类

public class QueryBuilder implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "query_title")
private String queryTitle;
@Basic(optional = false)
@Lob
@Column(name = "sql_query")
private String sqlQuery;
@Basic(optional = false)
@Column(name = "condition")
private String condition;
@Basic(optional = false)
@Column(name = "output_fields")
private String outputFields;
@Column(name = "creation_time")
@Temporal(TemporalType.TIMESTAMP)
private Date creationTime;
@Column(name = "modification_time")
@Temporal(TemporalType.TIMESTAMP)
private Date modificationTime;
@Basic(optional = false)
@Lob
@Column(name = "discription")
private String discription;
@JoinColumn(name = "physician", referencedColumnName = "Physician_Id")
@ManyToOne(optional = false)
private Physician physician;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "querybuilderId")
private Collection<QueryBuilderCondition> queryBuilderConditionCollection;

public QueryBuilder() {
}

public QueryBuilder(Integer id) {
    this.id = id;
}

public QueryBuilder(Integer id, String queryTitle, String sqlQuery, String condition, String outputFields, String discription) {
    this.id = id;
    this.queryTitle = queryTitle;
    this.sqlQuery = sqlQuery;
    this.condition = condition;
    this.outputFields = outputFields;
    this.discription = discription;
}

public Integer getId() {
    return id;
}

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

public String getQueryTitle() {
    return queryTitle;
}

public void setQueryTitle(String queryTitle) {
    this.queryTitle = queryTitle;
}

public String getSqlQuery() {
    return sqlQuery;
}

public void setSqlQuery(String sqlQuery) {
    this.sqlQuery = sqlQuery;
}

public String getCondition() {
    return condition;
}

public void setCondition(String condition) {
    this.condition = condition;
}

public String getOutputFields() {
    return outputFields;
}

public void setOutputFields(String outputFields) {
    this.outputFields = outputFields;
}

public Date getCreationTime() {
    return creationTime;
}

public void setCreationTime(Date creationTime) {
    this.creationTime = creationTime;
}

public Date getModificationTime() {
    return modificationTime;
}

public void setModificationTime(Date modificationTime) {
    this.modificationTime = modificationTime;
}

public String getDiscription() {
    return discription;
}

public void setDiscription(String discription) {
    this.discription = discription;
}

public Physician getPhysician() {
    return physician;
}

public void setPhysician(Physician physician) {
    this.physician = physician;
}

@XmlTransient
public Collection<QueryBuilderCondition> getQueryBuilderConditionCollection() {
    return queryBuilderConditionCollection;
}

public void setQueryBuilderConditionCollection(Collection<QueryBuilderCondition> queryBuilderConditionCollection) {
    this.queryBuilderConditionCollection = queryBuilderConditionCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof QueryBuilder)) {
        return false;
    }
    QueryBuilder other = (QueryBuilder) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.medikm.entity.QueryBuilder[ id=" + id + " ]";
}
}

0 个答案:

没有答案