JPA JSF创建新记录不会更新页面

时间:2018-04-25 09:27:28

标签: jpa jsf reload

以下p:dialog使用以下内容创建新的comment

    <p:dialog id="buyDlg" widgetVar="buyDialog" modal="true" resizable="false" appendTo="@(body)"
              header="#{myBundle.CreateCommentsTitle}" closeOnEscape="true" width="auto" height="auto">

        <h:form id="buyCommentCreateForm">

            <h:panelGroup id="buyDisplay">

                <p:outputPanel id="buyCommentsPanel">

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

                </p:outputPanel>

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


            </h:panelGroup>

        </h:form>

    </p:dialog>

保存方法为:

public void setBuyComment(long idPmMain, String commentText){

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

    pmMain.setBuyComment(comments);
    comments.setBuyComment(pmMain);

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

一切正常但我需要重新加载页面,以便在PmMain中可视化评论ID(它会在PmMain.setBuyComments(comments)的上方插入)。有什么想法吗?

修改

添加有关setBuyComment的信息:

对于PmMain.setBuyComment我有:

public void setBuyComment(Comments buyComment) {
    this.buyComment = buyComment;
}

对于comments.setBuyComment

public void setBuyComment(PmMain pmMain){
    pmMain.setBuyComment(this);
    pmMainCollection24.add(pmMain);
}

编辑2 来自PmMain的支持Bean如下所示:

@Entity
@Table(name = "pm_main")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "PmMain.findAll", query = "SELECT p FROM PmMain p")
    , @NamedQuery(name = "PmMain.findByPropId", query = "SELECT p FROM PmMain p WHERE p.propId = :propId")
    , @NamedQuery(name = "PmMain.findByPropName", query = "SELECT p FROM PmMain p WHERE p.propName = :propName")
    , @NamedQuery(name = "PmMain.findByIdPmMain", query = "SELECT p FROM PmMain p WHERE p.idPmMain = :idPmMain")})
public class PmMain implements Serializable {

private static final long serialVersionUID = 1L;
@Size(max = 25)
@Column(name = "prop_id")
private String propId;
@Size(max = 125)
@Column(name = "prop_name")
private String propName;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_pm_main")
private Long idPmMain;
@JoinColumn(name = "buy_comment", referencedColumnName = "id_comments")
@ManyToOne
private Comments buyComment;

[... Getters and Setters ...]

Comments bean看起来像:

@Entity
@Table(name = "comments")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Comments.findAll", query = "SELECT c FROM Comments c")
    , @NamedQuery(name = "Comments.findByCommentText", query = "SELECT c FROM Comments c WHERE c.commentText = :commentText")
    , @NamedQuery(name = "Comments.findByIdComments", query = "SELECT c FROM Comments c WHERE c.idComments = :idComments")})
public class Comments implements Serializable {

private static final long serialVersionUID = 1L;
@Size(max = 2147483647)
@Column(name = "comment_text")
private String commentText;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_comments")
private Long idComments;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "buyComment")
private List<PmMain> pmMainCollection24 = new ArrayList<>();

[... Getters and Setters ...]

public void setBuyComment(PmMain pmMain){
    pmMain.setBuyComment(this);
    pmMainCollection24.add(pmMain);
}

0 个答案:

没有答案
相关问题