Hibernate添加新的子项并更新现有项

wyyhbhjk  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(239)

我有多个实体ChapterSectionOneToMany(没有连接表)。
SectionAttachmentOneToMany(带有连接表)。
我可以添加/更新/删除Sections,但不能删除附件,因为附件存在于另一个表中。我只需要管理连接表。
以下是我的实体:
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;

Section.java

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

@Column(name="SECTION_DESC")
private String sectionDesc;

@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "CHAPTER_ID", updatable = false)
private Chapter chapter;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name="SECTION_ATTACHMENT", 
    joinColumns={@JoinColumn(name="SECTION_ID")}, 
    inverseJoinColumns={@JoinColumn(name="ATTACHMENT_ID")})
private List<Attachment> attachments;

Attachment.java

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

@Column(name="ATTACHMENT_NAME")
private String attachmentName;

@ManyToOne(fetch = FetchType.LAZY)
@JoinTable(name="SECTION_ATTACHMENT", 
joinColumns={@JoinColumn(name="ATTACHMENT_ID")}, 
inverseJoinColumns={@JoinColumn(name="SECTION_ID")})
@JsonBackReference
private Section section;

ChapterResource.class

@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();
}

Dao.java

public void updateChapter(Session session, Chapter chapter) {
    session.update(chapter);
}

我正在创建新章节,使用后调用作为

{
        "sections":[
        {
            "sectionDesc":"first sectionDesc",
            "attachments":[{
                "attachmentId":1,
                "attachmentName":"attachment1.docx"
            }]
        }]
    }

调用GET章节时

{
    "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 increment during the insert
        "attachments":[{
            "attachmentId":10,
            "attachmentName":"sampleDoc.ppt"
        }]
    }]
}

当我对我的API进行PUT调用时,我没有收到任何错误或警告,但数据库中的记录没有像put body中那样发生变化。尝试了大量的跟踪和错误,但我无法找出哪里出错了。如果使用hibernate(5)可以实现,有人能帮助我吗?

643ylb08

643ylb081#

Hibernate更新应该用在我们知道只更新实体信息的地方。
尝试使用

merge()=〉首选

保存或更新()

更多信息here

相关问题