So erhalten Sie Fremdschlüssel mit Hibernate Annotations, Struts 2 und JSP

Ich habe es geschafft, eine Webanwendung mit Hibernate (Annotations) und Struts 2 in einem MVC-Muster zu erstellen. Ich habe die JSP-Seite festgelegt, auf der ein Formular vom Benutzer ausgefüllt werden soll. Anschließend wird es in meiner Action-Klasse verarbeitet, die das DAO aufruft und ein POJO für das übergibtuser Tabelle. Ich habe auch einen Tisch mit gefülltstates und ich muss seine einstellenID in demuser Tabelle als Fremdschlüssel. Ich habe es getan, indem ich eine Variable erstellt habe, die neu iststate POJO und mit derstate Name, der aus meinem JSP-Formular in einer Abfrage kommt, um das zu erhaltenstate Informationen aus der Datenbank. Das Problem ist, dass ich gelernt habe, dass ich in der Aktionsklasse keine Abfragesprache (HQL) verwenden kann, da ich die MVC- und Struts 2-Struktur beschädigen würde. Ist das richtig? Wenn ja, wie kann ich das senden?state Name durch die Aktionsklasse, wenn das DAO erwartet, dass es einestate POJO, nicht nur dasstate Name;

Hier ist das jsp Formular:

   <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>

hier ist meine action klasse, folgende struts 2 konfiguration:

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";
    }

// GETs und SETs

Hier ist mein POJO für den Benutzer:

@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...

Antworten auf die Frage(1)

Ihre Antwort auf die Frage