2

私はJavaとjsfと休止状態の新人で、エンティティのcrud関数を生成するためにjboss forgeを使用していますが、休止状態の@embedable idで動作させる方法がわかりません。これが私のコードです。

    @実在物
    @Table(名前 = "エスタド"、スキーマ = "パブリック")
    public クラス Estado は java.io.Serializable を実装します
    {

       プライベート EstadoId id;
       プライベート パイス パイス;
       プライベート日付 fechaRegistro;
       プライベート文字列の説明。
       私的略称;
       プライベート セット municipios = 新しい HashSet(0);

       public Estado()
       {
       }

       public Estado(EstadoId id, Pais pais, Date fechaRegistro, short estatus)
       {
          this.id = id;
          this.pais = パイス;
          this.fechaRegistro = fechaRegistro;
          this.estatus = estatus;
       }

       public Estado(EstadoId id, Pais pais, Date fechaRegistro, String descripcion, short estatus, Set municipios)
       {
          this.id = id;
          this.pais = パイス;
          this.fechaRegistro = fechaRegistro;
          this.descripcion = 説明;
          this.estatus = estatus;
          this.municipios = municipios;
       }

       @EmbeddedId
       @AttributeOverrides( { @AttributeOverride(name = "paisId", column = @Column(name = "pais_id", nullable = false, length = 5)), @AttributeOverride(name = "estadoId", column = @Column(name = "estado_id"、nullable = false、長さ = 5)) })       

    }

次に埋め込み可能な ID

    @埋め込み可能
    public class EstadoId は java.io.Serializable を実装します {


         プライベート文字列 paisId;
         プライベート文字列 estadoId;

        public EstadoId() {
        }

        public EstadoId(String paisId, String estadoId) {
           this.paisId = paisId;
           this.estadoId = estadoId;
        }                    

    }

今 @ConversationScoped Bean


    import javax.annotation.Resource;
    javax.ejb.SessionContext をインポートします。
    import javax.ejb.Stateful;
    import javax.enterprise.context.Conversation;
    import javax.enterprise.context.ConversationScoped;
    import javax.faces.application.FacesMessage;
    import javax.faces.component.UIComponent;
    javax.faces.context.FacesContext をインポートします。
    import javax.faces.convert.Converter;
    import javax.inject.Inject;
    import javax.inject.Named;    

    @名前付き
    @ステートフル
    @ConversationScoped
    public class EstadoBean は Serializable を実装します
    {

       private static final long serialVersionUID = 1L;

       /*
        * Estado エンティティの作成と取得をサポート
        */

       プライベート ロング ID。

       public Long getId()
       {
          this.id を返します。
       }

       public void setId(ロング ID)
       {
          this.id = id;
       }

       プライベート エスタド エスタド;

       public Estado getEstado()
       {
          this.estado を返します。
       }

       @注入
       プライベートな会話;

       @PersistenceContext(type = PersistenceContextType.TRANSACTION)
       プライベート EntityManager entityManager;

       パブリック文字列 create()
       {

          this.conversation.begin();
          return "create?faces-redirect=true";
       }

       public void retrieve()
       {

          if (FacesContext.getCurrentInstance().isPostback())
          {
             戻る;
          }

          if (this.conversation.isTransient())
          {
             this.conversation.begin();
          }

          if (this.id == null)
          {
             this.estado = this.example;
          }
          そうしないと
          {
             this.estado = findById(getId());
          }
       }

       public Estado findById(Long id)
       {

          return this.entityManager.find(Estado.class, id);
       }


       @リソース
       プライベート SessionContext sessionContext;

       public コンバーター getConverter()
       {

          final EstadoBean ejbProxy = this.sessionContext.getBusinessObject(EstadoBean.class);

          新しいコンバーターを返す()
          {

             @オーバーライド
             public Object getAsObject(FacesContext コンテキスト、UIComponent コンポーネント、文字列値)
             {

                return ejbProxy.findById(Long.valueOf(value));
             }

             @オーバーライド
             public String getAsString (FacesContext コンテキスト、UIComponent コンポーネント、オブジェクト値)
             {

                if (値 == null)
                {
                   戻る "";
                }

                return String.valueOf(((Estado) value).getId());
             }
          };
       }

    }

実際、私の本当の疑問は、getConverter メソッドを適切にコーディングする方法についてです。なぜなら、estado エンティティ ID は文字列値ではなく、estado エンティティの ID タイプは、2 つの文字列値で構成される estadoId 埋め込み可能タイプだからです。 getConverter メソッドは、文字列値 param ? を取得します。

4

0 に答える 0