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

时间:2013-02-19 14:50:18

标签: hibernate jsf-2 jboss composite-primary-key jboss-forge

即时通讯使用java和jsf以及hibernate,我一直在使用jboss forge为我的实体生成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值param的位置?

0 个答案:

没有答案