2

im a newbe with java and jsf and hibernate and i´ve been using jboss forge to generate the crud functions for my entities but i dont know how to make it work with hibernate @embedable id, here is my code.


    @Entity
    @Table(name = "estado", schema = "public")
    public class Estado implements java.io.Serializable
    {

       private EstadoId id;
       private Pais pais;
       private Date fechaRegistro;
       private String descripcion;
       private short estatus;
       private Set municipios = new HashSet(0);

       public Estado()
       {
       }

       public Estado(EstadoId id, Pais pais, Date fechaRegistro, short estatus)
       {
          this.id = id;
          this.pais = 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 = pais;
          this.fechaRegistro = fechaRegistro;
          this.descripcion = 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, length = 5)) })       

    }

next the embedable id


    @Embeddable
    public class EstadoId  implements java.io.Serializable {


         private String paisId;
         private String estadoId;

        public EstadoId() {
        }

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

    }

now the @ConversationScoped bean



    import javax.annotation.Resource;
    import 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;
    import javax.faces.context.FacesContext;
    import javax.faces.convert.Converter;
    import javax.inject.Inject;
    import javax.inject.Named;    

    @Named
    @Stateful
    @ConversationScoped
    public class EstadoBean implements Serializable
    {

       private static final long serialVersionUID = 1L;

       /*
        * Support creating and retrieving Estado entities
        */

       private Long id;

       public Long getId()
       {
          return this.id;
       }

       public void setId(Long id)
       {
          this.id = id;
       }

       private Estado estado;

       public Estado getEstado()
       {
          return this.estado;
       }

       @Inject
       private Conversation conversation;

       @PersistenceContext(type = PersistenceContextType.TRANSACTION)
       private EntityManager entityManager;

       public String create()
       {

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

       public void retrieve()
       {

          if (FacesContext.getCurrentInstance().isPostback())
          {
             return;
          }

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

          if (this.id == null)
          {
             this.estado = this.example;
          }
          else
          {
             this.estado = findById(getId());
          }
       }

       public Estado findById(Long id)
       {

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


       @Resource
       private SessionContext sessionContext;

       public Converter getConverter()
       {

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

          return new Converter()
          {

             @Override
             public Object getAsObject(FacesContext context, UIComponent component, String value)
             {

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

             @Override
             public String getAsString(FacesContext context, UIComponent component, Object value)
             {

                if (value == null)
                {
                   return "";
                }

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

    }

In fact my real doubt is about how to properly code the getConverter method because the estado entity id its not an string value, the id type for estado entity is estadoId embedable type who is composed by two string values, another question i have is from where the getConverter method gets the String value param ?

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
metalvarez
  • 606
  • 1
  • 8
  • 14
  • please post only the **relevant** parts of the code, nobody is going to read the whole 1000 lines of it. What I mean is - no imports, no comments, no setters/getters, no ctors and in general - no code that is not being used in the current context. – kostja Feb 19 '13 at 14:53
  • First, you should separate EJB beans and CDI/JSF managed beans. See http://stackoverflow.com/questions/4684112/how-do-cdi-and-ejb-compare-interact/4705840#4705840 – Johny T Koshy Feb 19 '13 at 15:23
  • Finally go through the following tutorial to learn how to use Converter in JSF. http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/ – Johny T Koshy Feb 19 '13 at 15:39

0 Answers0