从NetBeans的primefaces CRUD生成器插件中,我得到了 parent 表的以下代码:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_comments")
private Long idComments;
@OneToMany(mappedBy = "docDownloadComment")
private Collection<PmMain> pmMainCollection;
孩子是:
@JoinColumn(name = "doc_download_comment", referencedColumnName = "id_comments")
@ManyToOne
private Comments docDownloadComment;
每当我在id_comments
表中创建记录时,如何将生成的doc_donwload_comment
插入comments
?
答案 0 :(得分:0)
我们假设您坚持评论,并且希望将 PmMain 与
持续存在您必须保留评论对象。
然后坚持 PmMain 类的每个对象,将docDownloadComment设置为您已经过时的评论。
就像那样:
entityManager.persist(comment)
//then
comment = entityManager.merge(comment);
...
pmMain.setDocDownloadComment(comment);
entityManager.persist(pmMain);
//for all pmMains
您还可以在此关系上设置CascadeType,以自动保存新评论中的所有PmMain。有关详细信息,请查看here。
答案 1 :(得分:0)
<强>解强>
感谢Rjiuk和Billy Hope。
我想与那些使用 Primefaces CRUD Generator 的人分享我的解决方案。
<强>父强>:
@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);
}
儿童保持不变。
ParentController 添加:
@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);
}
并调用此方法,例如:
<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>