Hibernate添加新子项并更新存在

时间:2018-05-01 00:59:01

标签: java hibernate parent-child hibernate-mapping resteasy

我有多个实体 ChapterSection OneToMany(没有联接表)。 SectionAttachment OneToMany(带连接表) 我可以添加/更新/删除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 increament during the insert
        "attachments":[{
            "attachmentId":10,
            "attachmentName":"sampleDoc.ppt"
        }]
    }]
}

当我对我的API进行PUT调用时,我没有收到任何错误或警告,但是db中的记录没有像在put体中那样被更改。 尝试了很多线索和错误,但我无法弄清楚我哪里出错了。 如果使用hibernate(5)可以实现这一点,那么任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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

尝试使用

merge() =&gt;优选

<强> saveOrUpdate()方法

更多信息here