如何将jsf @ConversationScoped bean与hibernate组合主键一起使用?

即时通讯与java和jsf和休眠newbe,我一直在使用jboss伪造生成我的实体的crud功能,但我不知道如何使它与hibernate @embedable id一起工作,这里是我的代码。


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

    }

接下来是可嵌入的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;
        }                    

    }

现在是@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());
             }
          };
       }

    }

实际上,我真正的疑问是如何正确编码getConverter方法,因为estado实体id其不是字符串值,而estado实体的id类型是由两个字符串值组成的estadoId可嵌入类型,另一个问题来自哪里getConverter方法获取String值参数?

链接地址: http://www.djcxy.com/p/54073.html

上一篇: How to use jsf @ConversationScoped bean with an hibernate composite primary key?

下一篇: Hibernate error persists an entity in @PostPersist method