0

The following p:dialog creates a new comment using:

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

Where the save method is:

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

Everything works fine but I need to reload the page in order to visualize the comment id in PmMain (it gets inserted above where it says PmMain.setBuyComments(comments)). Any ideas?

EDIT

Adding information about setBuyComment:

For PmMain.setBuyComment I have:

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

And for comments.setBuyComment:

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

EDIT 2 The backing Bean from PmMain looks like this:

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

And the Comments bean looks like:

@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);
}
I Justo
  • 1
  • 4
  • Possible duplicate of [How to refresh the page after closing p:dialog](https://stackoverflow.com/questions/30107167/how-to-refresh-the-page-after-closing-pdialog) – Simon Martinelli Apr 25 '18 at 11:04
  • 1
    I'm afraid that it has nothing to do with that, I'm trying to let the DB know that something has been changed. In my opinion, this is related to the EntityManager refresh function. – I Justo Apr 25 '18 at 11:21
  • Don't you close the dialog after setBuyComment? – Simon Martinelli Apr 25 '18 at 11:25
  • Yes, but it won't update (even when setting `update="@all"`). It only updates when you refresh the page. – I Justo Apr 25 '18 at 11:40
  • How do you load the comments? In the method setBuyComment I don't see code that will update the collection that you probably use to list the comments. You have to update that list as well. Can you provide some more code? – Simon Martinelli Apr 25 '18 at 12:11
  • Just added some code from `setBuyComment`, thanks Simon for your time! – I Justo Apr 25 '18 at 12:30
  • So with this addition it works? If yes please add this code as the answer, that everyone can see that the question is answered. – Simon Martinelli Apr 25 '18 at 12:35
  • No, that code was already implemented. I was looking for the code to update the collection and the list. – I Justo Apr 25 '18 at 12:49
  • And how does your backing bean look like? I assume that you have difference reference and there is still the old data – Simon Martinelli Apr 25 '18 at 13:18

0 Answers0