Primefaces积垢生成器一对多插入父id JPA
从NetBeans的primefaces CRUD生成器插件中,我得到了父表的以下代码:Primefaces积垢生成器一对多插入父id JPA,jpa,one-to-many,cascade,Jpa,One To Many,Cascade,从NetBeans的primefaces CRUD生成器插件中,我得到了父表的以下代码: @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id_comments") private Long idComments; @OneToMany(mappedBy = "docDownloadComment") private Collection<Pm
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_comments")
private Long idComments;
@OneToMany(mappedBy = "docDownloadComment")
private Collection<PmMain> pmMainCollection;
每当我在
comments
表中创建记录时,如何将生成的id\u comments
插入doc\u donwload\u comment
中?假设您坚持comment,并且希望坚持PmMain
你自己
您必须保留您的注释对象
然后将PmMain类中设置docDownloadComment的每个对象持久化为您以前访问过的注释
就像这样:
entityManager.persist(注释)
级联型
您还可以在此关系上设置CascadeType,以便在新注释中自动保存所有pmmain。有关更多信息,请查看。解决方案 多亏了Rjiuk和Billy Hope 我想与使用Primefaces积垢发生器的人分享我的解决方案 家长:
@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);
}
从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>
PrimeFaces“crud”生成器来自Netbeans或IntelliJ,但不来自PrimeFaces。上面的代码中没有与PF相关的内容。首先尝试进行单元测试……您应该向@OneToMany注释中添加一个cascade=CascadeType.ALL,mappedBy=“docdownloadcoment”表示与另一端相关的属性,因此当您向集合中添加对象时,id\u注释将被更新
@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);
}
@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>