SpringData JPA Hibernate,尝试保存一个新的嵌套对象

时间:2022-09-11 17:00:37

I have a nested object like this

我有一个像这样的嵌套对象

public class Chart {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long chart_id;

   @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
   @JoinColumn(name="chart_id")
   private Set<ChartGroup> chartGroups;
        /* ..other attributes.. */
}

public class ChartGroup {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long group_id;
   private long chart_id;

       @OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
   @JoinColumn(name="group_id")
   private ChartYData chartYData;
         /* ..other attributes.. */
 }

 public class ChartYData {

   @Id
   private long group_id;

   private String yData;
  }

I have REST services where are used to fetch and save/update this data. After fetching the data for a specific chart I save all JSON data in a javascript object. User can delete or modify groups in a chart or he can add new groups. I am sending full chart object to the server to be saved. No call is made when you add a new group or delete an old group. The problem arises when user adds new group. In my JSON the values are like this SpringData JPA Hibernate,尝试保存一个新的嵌套对象

我有用于获取和保存/更新数据的REST服务。在获取特定图表的数据之后,我将所有JSON数据保存在一个javascript对象中。用户可以在图表中删除或修改组,也可以添加新的组。我正在向要保存的服务器发送完整的图表对象。当添加新组或删除旧组时,不会调用。当用户添加新组时出现问题。在我的JSON中,值是这样的。

When I try to update the CHART object, it gives me MySQLIntegrityConstraintViolationException:Cannot add or update a child row: a foreign key constraint fails (chartpro.chart_ydata, CONSTRAINT fk_ydata_to_group FOREIGN KEY (group_id) REFERENCES chart_group (group_id)

当我试图更新图表对象时,它会给我MySQLIntegrityConstraintViolationException:不能添加或更新子行:外键约束失败(chartpro)。约束fk_ydata_to_group外键(group_id)引用chart_group (group_id)

Is there a way to update my chart object as a whole? I mean the JPA should add ChartGroup and then add ChartYData with the newly created group_id

是否有方法更新我的图表对象作为一个整体?我的意思是JPA应该添加ChartGroup,然后使用新创建的group_id添加ChartYData

It gives me same error even when I send following JSON, (with group_id=null for new groups)

它会给我同样的错误,即使当我发送JSON之后(对于新组,group_id=null)

  ChartGroups : [{/* data for previously present Chart groups (works fine) */},{
    chartYData :
 { 
            ydata : "ydata",
            group_id : null
          } 
  chart_id:1
  group_id:null
 }]

1 个解决方案

#1


0  

I think the problem that you have to set the group_id in ChartYData entities manually as you are not sending group_id with json object, or handle it by javascript to add group_id to ChartYData before sending it.

我认为您必须手动设置ChartYData实体中的group_id,因为您没有使用json对象发送group_id,或者在发送之前通过javascript处理它向ChartYData添加group_id。

#1


0  

I think the problem that you have to set the group_id in ChartYData entities manually as you are not sending group_id with json object, or handle it by javascript to add group_id to ChartYData before sending it.

我认为您必须手动设置ChartYData实体中的group_id,因为您没有使用json对象发送group_id,或者在发送之前通过javascript处理它向ChartYData添加group_id。