代码之家  ›  专栏  ›  技术社区  ›  I Justo

Primefaces CRUD生成器一对多插入父id JPA

  •  0
  • I Justo  · 技术社区  · 8 年前

    从用于NetBeans的primefaces CRUD生成器插件中,我得到以下代码 父母亲 表格:

    @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 桌子

    2 回复  |  直到 8 年前
        1
  •  0
  •   Emil Hotkowski    8 年前

    假设你坚持 议论 你想坚持下去 PmMain公司 用它

    靠你自己

    你必须坚持 议论 对象

    然后将 PmMain公司 类,您可以在其中将docDownloadComment设置为您之前访问过的注释。

    就像这样:

    entityManager.persist(comment)

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

    级联型。坚持

    您还可以在此关系上设置CascadeType,以便自动保存新注释中的所有PmMains。有关更多信息,请检查 here

        2
  •  0
  •   I Justo    8 年前

    解决方案

    感谢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);
    }
    

    小孩 保持不变。

    父控制器 添加以下内容:

    @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>