How to use jsf @ConversationScoped bean with an hibernate composite primary key?
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 ?
链接地址: http://www.djcxy.com/p/54074.html上一篇: 删除共享首选项