如何将外键添加到JSF页面?

时间:2017-03-20 20:46:21

标签: jsf

我正在尝试创建一个显示帖子的jsf页面,用户可以在其上写评论。所以在注释表中有一个引用该帖子的外键。 我的问题如何在评论“Avis”实体中添加属性Idea“Idee”?

这是index.xhtml

 <p:outputPanel>
            <h:panelGrid columns="2" cellpadding="5">
                <h:outputText value="#{msg['idee.titre']}" />
                <h:outputText value="#{in.titre}" style="font-weight: bold"/>

                <h:outputText value="#{msg['idee.description']}" />
                <h:outputText value="#{in.description}" style="font-weight: bold"/>

                <h:outputText value="#{msg['idee.theme']}" />
                <h:outputText value="#{in.theme}" style="font-weight: bold"/>
                 <h:outputText value="#{msg['idee.type']}" />
                <h:outputText value="#{in.type}" style="font-weight: bold"/>

                <h:inputText value="#{avisBean.avis.commentaire}" />
                <p:commandButton value="commenter" icon="ui-icon-check" action="#{avisBean.addAvis}" >

                </p:commandButton>
            </h:panelGrid>
        </p:outputPanel>

这是Avis.java

 @Entity
@Table(name = "avis")
public class Avis implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column
    private String commentaire;
    @Column
    private int score;

    @Column
    private Boolean signaler;

    @Column
    private Boolean satisfaction_utilisateur;

    @ManyToOne
    @JoinColumn(name = "idee_id")
    private Idee idee;


    @ManyToOne
    @JoinColumn(name="user_id")
    private Utilisateur user_avis;

这是Idee.java

   @Entity
    @Table(name = "idee")
    public class Idee implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        @Column
        private String titre;
        @Column
        private String description;

        @Column
        private String theme;

        @Column
        private String type;

        @OneToMany(mappedBy = "idee")
        private Collection<Avis> avis;
        @ManyToOne
        @JoinColumn(name="user_id")
        private Utilisateur user;

1 个答案:

答案 0 :(得分:0)

如果您在一个页面中显示多个Ideas“Idees”,那么您可以做的是将“idee”对象传递给#{avisBean.addAvis}方法: 在您的xhtml页面中:

<p:commandButton value="commenter" icon="ui-icon-check" action="#{avisBean.addAvis(in)}" >

并在你的bean中这样:

public String addAvis(Idee idee){
    //add your avis to your idee here
}