Primefaces CRUD 生成器 one-to-many 插入 parent id JPA

Primefaces CRUD Generator one-to-many insert parent id JPA

从 NetBeans 的 primefaces CRUD 生成器插件中,我得到以下 parent table:

的代码
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_comments")
private Long idComments;

@OneToMany(mappedBy = "docDownloadComment")
private Collection<PmMain> pmMainCollection;

child 是:

@JoinColumn(name = "doc_download_comment", referencedColumnName = "id_comments")
@ManyToOne
private Comments docDownloadComment;

每当我在 comments table 中创建记录时,如何将生成的 id_comments 插入到 doc_donwload_comment 中?

假设您坚持使用 Comment 并且您希望使用它坚持 PmMain

靠自己

您必须保留您的 评论 对象。

然后保留 PmMain class 的每个对象,您将 docDownloadComment 设置为您之前保留的评论。

像那样:

entityManager.persist(comment)

//then
comment = entityManager.merge(comment);

...
pmMain.setDocDownloadComment(comment);
entityManager.persist(pmMain);
//for all pmMains

CascadeType.PERSIST

您还可以在此关系上设置 CascadeType,以自动保存新评论中的所有 PmMain。有关详细信息,请查看 here.

解决方案

感谢 Rjiuk 和 Billy Hope。

我想与那些使用 Primefaces CRUD Generator.

的人分享我的解决方案

Parent:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_comments")
private Long idComments;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "docDownloadComment")
private List<PmMain> pmMainCollection = new ArrayList<>();

[... Getters and Setters ...]

public void setDocDownloadComment(PmMain pmMain){
    pmMain.setDocDownloadComment(this);
    pmMainCollection.add(pmMain);
}

Child保持不变。

Parent控制器 添加:

@Inject
CommentsFacade commentsFacadeEJB;
@Inject
PmMainFacade pmMainFacadeEJB;
public void saveDocDownloadComment(long idPmMain, String commentText){

    PmMain pmMain = pmMainFacadeEJB.find(idPmMain);
    Comments comments = new Comments();

    pmMain.setDocDownloadComment(comments);
    comments.setDocDownloadComment(pmMain);

    comments.setCommentText(commentText);
    commentsFacadeEJB.edit(comments);

}

并从 primefaces 中调用此方法,例如:

<h:form id="ddCommentCreateForm">
    <h:panelGroup id="ddDisplay">

        <p:outputPanel id="ddCommentsPanel">

            <p:row>
                <p:column>
                    <p:inputTextarea id="commentText" value="#{commentsController.selected.commentText}" cols="100" rows="20" style="margin-bottom:10px"/>
                </p:column>
            </p:row>

        </p:outputPanel>

        <p:commandButton actionListener="#{commentsController.saveDocDownloadComment(pmMainController.selected.idPmMain, commentsController.selected.commentText)}" value="#{myBundle.Save}" update="ddDisplay,:PmMainListForm:datalist,:growl" oncomplete="handleSubmit(xhr,status,args,PF('ddDialog'));">
            <p:confirm header="#{myBundle.ConfirmationHeader}" message="#{myBundle.ConfirmEditMessage}" icon="ui-icon-alert"/>
        </p:commandButton>
        <p:commandButton value="#{myBundle.Cancel}" oncomplete="PF('ddDialog').hide()" update="nsDisplay" process="@this" immediate="true" resetValues="true"/>


    </h:panelGroup>

</h:form>