我是 java、jsf 和 hibernate 的新手,我一直在使用 jboss forge 为我的实体生成 crud 函数,但我不知道如何使它与 hibernate @embedable id 一起工作,这是我的代码。
@实体 @Table(名称=“estado”,模式=“公共”) 公共类 Estado 实现 java.io.Serializable { 私人 EstadoId id; 私人派斯派斯; 私人日期 fechaRegistro; 私有字符串描述; 私人短期身份; 私有集 municipios = new HashSet(0); 公共 Estado() { } public Estado(EstadoId id, Pais pais, Date fechaRegistro, short estatus) { 这个.id = id; this.pais = pais; this.fechaRegistro = fechaRegistro; this.estatus = 状态; } public Estado(EstadoId id, Pais pais, Date fechaRegistro, 字符串描述, short estatus, Set municipios) { 这个.id = id; this.pais = pais; this.fechaRegistro = fechaRegistro; this.descripcion = 描述; this.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”,可为空 = 假,长度 = 5)) }) }
接下来是可嵌入的 id
@可嵌入 公共类 EstadoId 实现 java.io.Serializable { 私有字符串 paisId; 私有字符串 estadoId; 公共 EstadoId() { } 公共 EstadoId(字符串 paisId,字符串 estadoId){ this.paisId = paisId; this.estadoId = estadoId; } }
现在是@ConversationScoped bean
导入 javax.annotation.Resource; 导入 javax.ejb.SessionContext; 导入 javax.ejb.Stateful; 导入 javax.enterprise.context.Conversation; 导入 javax.enterprise.context.ConversationScoped; 导入 javax.faces.application.FacesMessage; 导入 javax.faces.component.UIComponent; 导入 javax.faces.context.FacesContext; 导入 javax.faces.convert.Converter; 导入 javax.inject.Inject; 导入 javax.inject.Named; @命名 @有状态 @ConversationScoped 公共类 EstadoBean 实现 Serializable { 私有静态最终长序列版本UID = 1L; /* * 支持创建和检索 Estado 实体 */ 私人长ID; 公共长 getId() { 返回这个.id; } 公共无效 setId(长 id) { 这个.id = id; } 私人 Estado estado; 公共 Estado getEstado() { 返回这个.estado; } @注入 私人对话对话; @PersistenceContext(type = PersistenceContextType.TRANSACTION) 私有实体管理器实体管理器; 公共字符串创建() { this.conversation.begin(); 返回“创建?面孔重定向=真”; } 公共无效检索() { if (FacesContext.getCurrentInstance().isPostback()) { 返回; } if (this.conversation.isTransient()) { this.conversation.begin(); } 如果(this.id == null) { this.estado = this.example; } 别的 { this.estado = findById(getId()); } } 公共 Estado findById(长 id) { 返回 this.entityManager.find(Estado.class, id); } @资源 私人会话上下文会话上下文; 公共转换器 getConverter() { final EstadoBean ejbProxy = this.sessionContext.getBusinessObject(EstadoBean.class); 返回新的转换器() { @覆盖 public Object getAsObject(FacesContext 上下文,UIComponent 组件,字符串值) { 返回 ejbProxy.findById(Long.valueOf(value)); } @覆盖 public String getAsString(FacesContext 上下文,UIComponent 组件,对象值) { 如果(值 == 空) { 返回 ””; } return String.valueOf(((Estado) value).getId()); } }; } }
事实上,我真正的疑问是如何正确编码 getConverter 方法,因为 estado 实体 id 它不是字符串值,estado 实体的 id 类型是 estadoId 可嵌入类型,由两个字符串值组成,我的另一个问题是从哪里getConverter 方法获取字符串值参数?