Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use jsf @ConversationScoped bean with an hibernate composite primary key?
    primarykey
    data
    text
    <p>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.</p> <pre> @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)) }) } </pre> <p>next the embedable id</p> <pre> @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; } } </pre> <p>now the @ConversationScoped bean</p> <pre> 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()); } }; } } </pre> <p>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 ? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload