Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 休眠添加新的子项并更新现有项_Java_Hibernate_Parent Child_Hibernate Mapping_Resteasy - Fatal编程技术网

Java 休眠添加新的子项并更新现有项

Java 休眠添加新的子项并更新现有项,java,hibernate,parent-child,hibernate-mapping,resteasy,Java,Hibernate,Parent Child,Hibernate Mapping,Resteasy,我有多个实体 章节至章节OneToMany(无联接表)。 部分至附件OneToMany(带联接表) 我可以添加/更新/删除部分,但无法删除附件,因为附件存在于另一个表中。我只需要管理联接表。这里是我的实体 Chapter.java @Id @Column(name = "CHAPTER_ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer chapterId; @OneToMany(cascade=Ca

我有多个实体
章节
章节
OneToMany
(无联接表)。
部分
附件
OneToMany
(带联接表) 我可以添加/更新/删除
部分
,但无法删除附件,因为附件存在于另一个表中。我只需要管理联接表。这里是我的实体

Chapter.java

@Id
@Column(name = "CHAPTER_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer chapterId;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "chapter")
private List<Section> sections;
第二章资源类

@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/chapter/{chapterId}")
public Response updateChapter(@PathParam("chapterId") int chapterId, Chapter chapter) {
    return chapterService.updateChapter(chapterId, chapter);
}
ChapterServiceImpl.class

@Override
public Response updateChapter(int chapterId, Chapter chapter) {
    Session session = null;
    try {
        session = HibernateUtil.getSession();
        dao.updateChapter(session, chapter);
        HibernateUtil.closeSession(session);
    } finally {
        HibernateUtil.closeSession(session);
    }
    return Response.ok().build();
}
java

public void updateChapter(Session session, Chapter chapter) {
    session.update(chapter);
}
我正在创建一个新的章节,使用post call作为

 {
        "sections":[
        {
            "sectionDesc":"first sectionDesc",
            "attachments":[{
                "attachmentId":1,
                "attachmentName":"attachment1.docx"
            }]
        }]
    }
什么时候打电话给我

{
    "chapterId":1,
    "sections":[
    {
        "sectionDesc":"first sectionDesc",
        "sectionId":1,
        "attachments":[{
            "attachmentId":1,
            "attachmentName":"attachment1.docx"
        }]
    }]
}
现在我必须使用PUT更新,如下所示

{
    "chapterId":1,
    "sections":[
    { //updating the existing attachment name
        "sectionDesc":"first sectionDesc",
        "sectionId":1, // this indicates existing section
        "attachments":[{
            "attachmentId":1,
            "attachmentName":"test.pdf" // change of attachmentName should not delete the section, but has to update the 
        }]
    },{ //adding new section here
        "sectionDesc":"new sectionDesc",
        // "sectionId":2, it should be auto increament during the insert
        "attachments":[{
            "attachmentId":10,
            "attachmentName":"sampleDoc.ppt"
        }]
    }]
}
当我对我的API进行PUT调用时,我没有收到任何错误或警告,但是数据库中的记录没有像PUT正文中那样被更改。 我尝试了很多尝试和错误,但我不知道我错在哪里。
如果使用
hibernate(5)
可以实现这一点,有谁能帮助我吗?

如果我们知道只更新实体信息,那么应该使用hibernate更新

试用

合并()=>首选

saveOrUpdate()


更多信息

试着调试方法“updateChapter”,看看这一章是否正确完成。是的<
updateChapter
中的code>chapter与JSON中预期的一样好。我还可以尝试什么?您可以在将JSON转换为实体的地方添加代码吗?我添加了rest
资源
,以及
服务
impl,其中它会自动将
JSON
转换为
章节
实体。尝试了这两种方法,结果仍然相同。新的变化没有反映在数据库中。
{
    "chapterId":1,
    "sections":[
    {
        "sectionDesc":"first sectionDesc",
        "sectionId":1,
        "attachments":[{
            "attachmentId":1,
            "attachmentName":"attachment1.docx"
        }]
    }]
}
{
    "chapterId":1,
    "sections":[
    { //updating the existing attachment name
        "sectionDesc":"first sectionDesc",
        "sectionId":1, // this indicates existing section
        "attachments":[{
            "attachmentId":1,
            "attachmentName":"test.pdf" // change of attachmentName should not delete the section, but has to update the 
        }]
    },{ //adding new section here
        "sectionDesc":"new sectionDesc",
        // "sectionId":2, it should be auto increament during the insert
        "attachments":[{
            "attachmentId":10,
            "attachmentName":"sampleDoc.ppt"
        }]
    }]
}