当前位置: 首页 > 知识库问答 >
问题:

如何使用Hibernate注释、Struts2和JSP获取外键

逑禄
2023-03-14
   <s:form action="cadVoluntario" method="post">
        <s:textfield name="dtCadastro" label="Data de Cadastro"/>
        <s:textfield name="nomeCivil" label="Nome Civil"/>
        <s:textfield name="nomeSocial" label="Nome Social"/>
        <s:textfield name="cpf" label="CPF"/>
        <s:textfield name="rg" label="RG"/>
        <s:textfield name="orgExp" label="Órgão Expedidor"/>
        <s:textfield name="dtNascimento" label="Data de Nascimento"/>
        <s:textfield name="celular" label="Celular"/>
        <s:textfield name="telComercial" label="Telefone Comercial"/>
        <s:textfield name="telResidencial" label="Telefone Residencial"/>
        <s:textfield name="endereco" label="Endereço"/>
        <s:textfield name="bairro" label="Bairro"/>
        <s:textfield name="cidade" label="Cidade"/>
        <s:textfield name="estado" label="Estado"/>
        <s:submit/>
    </s:form>
public class CadVoluntarioAction {

private String dtCadastro;
private String nomeCivil;
private String nomeSocial;
private String cpf;
private String rg;
private String orgExp;
private String dtNascimento;
private String celular;
private String telComercial;
private String telResidencial;
private String endereco;
private String bairro;
private String cidade;
private String estado;

    public String add() throws Exception {

        VoluntarioPojo pojoVol = new VoluntarioPojo();
        CadVoluntarioDAO daoVol = new CadVoluntarioDAO();

        pojoVol.setDtCadastro(getDtCadastro());
        pojoVol.setNomeCivil(getNomeCivil());
        pojoVol.setNomeSocial(getNomeSocial());
        pojoVol.setCpf(getCpf());
        pojoVol.setRg(getRg());
        pojoVol.setOrgExp(getOrgExp());
        pojoVol.setDtNascimento(getDtNascimento());
        pojoVol.setCelular(getCelular());
        pojoVol.setTelComercial(getTelComercial());
        pojoVol.setTelResidencial(getTelResidencial());
        pojoVol.setEndereco(getEndereco());
        pojoVol.setBairro(getBairro());
        pojoVol.setCidade(getCidade());
        pojoVol.setEstadoPojo(getEstado());

        try {
            daoVol.salvarVoluntario(pojoVol);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "sucesso";
    }

//获取和设置

以下是我给用户的POJO:

@Entity
@Table (name="voluntario")
@Inheritance(strategy = InheritanceType.JOINED)

public class VoluntarioPojo implements Serializable{   
    private Long idVoluntario;
    private String dtCadastro;
    private String cpf;
    private String rg;
    private String orgExp;
    private String nomeCivil;
    private String nomeSocial;
    private String dtNascimento;
    private String areaAtuacao;
    private String celular;
    private String telResidencial;
    private String telComercial;
    private String endereco;
    private String cidade;
    private String bairro;
    private String cep;
    private String email;
    private String senha;
    private String perfil;
    private EstadoPojo estadoPojo;
    private IdentidadeGeneroPojo identidadeGeneroPojo;
    private OrientacaoSexualPojo orientacaoSexualPojo;

    public VoluntarioPojo () 
    {
    }

    @Id
    @SequenceGenerator(name="vol_seq", sequenceName="voluntario_volid_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "vol_seq")
    @Column(updatable = true, name = "volid", nullable = false)
    public Long getIdVoluntario() {
        return idVoluntario;
    }
    public void setIdVoluntario(Long idVoluntario) {

        this.idVoluntario = idVoluntario;
    }

    @ManyToOne 
    @JoinColumn(name = "estadosestid")
    public EstadoPojo getEstadoPojo ()
    {   
        return this.estadoPojo;
    }
    public void setEstadoPojo (EstadoPojo estadoPojo)
    {
        this.estadoPojo = estadoPojo;
    }

    @ManyToOne 
    @JoinColumn(name ="orisexualoriid")
    public OrientacaoSexualPojo getOrientacaoSexualPojo()
    {   
        return this.orientacaoSexualPojo;
    }
    public void setOrientacaoSexualPojo(OrientacaoSexualPojo orientacaoSexualPojo)
    {
        this.orientacaoSexualPojo = orientacaoSexualPojo;
    }

    @ManyToOne
    @JoinColumn(name ="idegeneroideid")
    public IdentidadeGeneroPojo getIdentidadeGeneroPojo()
    {   
        return this.identidadeGeneroPojo;
    }
    public void setIdentidadeGeneroPojo (IdentidadeGeneroPojo identidadeGeneroPojo)
    {
        this.identidadeGeneroPojo = identidadeGeneroPojo;
    }

    @Column(updatable = true, name = "volcpf", nullable = false, length = 11)
    public String getCpf() {
        return this.cpf;
    }
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

//All the other required Colunms...

共有1个答案

东郭瀚玥
2023-03-14

你需要的是一个转换器。一个xwork转换器。你就是这样做的。假设您状态对象

public class State implements Serializable
{
     private String stateName;
     private Long stateId;
     //getters and setters
}

现在做这个。在action类中,将state作为属性添加

public class CadVoluntarioAction extends ActionSupport{
     private State myState;
     //getters and setters
}

在jsp中添加mystate

public abstract class MyStrutsTypeConverter extends StrutsTypeConverter
{
    public Object convertFromString(Map map, String strings[], Class type)
    {
        String value = strings[0];
        if(value != null && value.equals("state_string_from_jsp"))
        {
          //Use your dao to retrieve your state object and return the state object
        }
        return null;
    }

public String convertToString(Map map, Object o)
{
        if(o instanceof State)
        {
            State data = (State)o;
            //return the  state string
        }
        return "";
}
}
full_package_name_of_your_state_class.State=full_package_name_of_your_converter_class.MyStrutsTypeConverter
 类似资料:
  • 我正在尝试使用Hibernate注释为我的数据库表编写一个模型类。 我有两个表,每个表都有一个主键用户和问题。 问题桌。 并且我还有一个表UserAnswer,它有userId和questionId作为来自上面两个表的外键。 但我无法找到如何在UserAnswer表中引用这些约束。 我怎样才能做到这一点呢?

  • 主要内容:创建主页:,创建视图:,创建动作:,配置文件:,Struts 2的注释类型:正如前面提到的,Struts提供了两种形式的配置。传统的方式是使用对所有配置struts.xml文件。到目前为止,我们已经看到了这样的例子很多。 Struts配置的另一种方法是使用Java5注释功能。使用Struts 注解,我们可以实现零配置。 要开始在你的项目中使用注释,确保WebContent/WEB-INF/lib文件夹中的jar文件包括以下:   struts2-convention-pl

  • 本文向大家介绍Oracle 获取表注释和列注释,包括了Oracle 获取表注释和列注释的使用技巧和注意事项,需要的朋友参考一下 全部表 表的注释 列的注释 相应的还有dba_col_comments,all_col_comments,这两个比user_col_comments多了ower列 以上所述是小编给大家介绍的Oracle 获取表注释和列注释,希望对大家有所帮助,如果大家有任何疑问请给我留言

  • 考虑下面的代码: 使用用于注释处理的最新JDK8 API,如何访问注释列表(@JSON,@freezed) 我尝试过很多技巧,比如

  • 如果我读对了,这个问题,这个问题,这个问题表明我需要实现一个类,但我不确定我应该用哪个子类来获取行数据,或者我应该如何去做。 编辑:我还想获得定义文本框中矩形的点的外观数据。尽管这可能是一个不同的问题,但它似乎与这个问题密切相关:检索定义注释外观流的非文本图形数据。

  • 主要内容:HTML 注释,带有 JSP 表达式的注释,隐藏注释,脚本程序(Scriptlet)中的注释说到注释,相信大家肯定都不陌生,它是对程序代码的解释和说明。注释可以提高代码的可读性,让他人能够更加轻松地了解代码,从而提高团队合作开发的效率。 在 JSP 中可以使用以下 4 种注释: HTML 注释 带有 JSP 表达式的注释 隐藏注释 脚本程序(Scriptlet)中的注释 在 JSP 规范中,它们都属于 JSP 中的注释,且语法规则和运行效果都各不相同。本节我们将对 JSP 中的各种注释