0

在我的 Spring 应用程序中,用于在数据库中插入/更新实体的表单具有类似于以下的结构:

<jsp:include page="../../common/cadastrar.jsp">
    <jsp:param name="entity" value="Usuario"/>

    <jsp:param name="arguments" value="login"/>
    <jsp:param name="arguments" value="senha"/>
    <jsp:param name="arguments" value="first_name"/>
    <jsp:param name="arguments" value="last_name"/>
    <jsp:param name="arguments" value="email"/>
</jsp:include>

重定向到这个共享的 jsp 页面(所有视图通用):

<c:url value="${param.entity}/cadastra" var="cadastra"/>
<form:form class="form" role="form" method="post" action="${cadastra}">

    <table>
        <c:forEach var="item" items="${paramValues.arguments}">
        <c:choose>
            <c:when test="${item == 'senha'}">
                <tr>
                    <td><form:label path="${item}">${item}</form:label></td>
                    <td><form:input path="${item}" type="password"/></td>
                </tr>
            </c:when>
            <c:otherwise>
                <tr>
                    <td><form:label path="${item}">${item}</form:label></td>
                    <td><form:input path="${item}"/></td>
                </tr>
            </c:otherwise>
        </c:choose>
        </c:forEach>
        <tr>
            <td colspan="2">
                <button type="submit" class="btn btn-lg btn-primary">Cadastrar</button>
            </td>
        </tr>
    </table>

</form:form>

当我的实体类只有原始属性(整数、字符串等)时,此代码可以正常工作。但我不知道如何为这样的类做同样的事情:

@Entity
@Table(name="cliente")
public class Cliente extends Entidade {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @OneToOne
    @JoinColumn(name="fk_usuario")
    private Usuario usuario;

    @Column(name="documento")
    private String documento;

    @Column(name="cpf")
    private String cpf;

    @Column(name="cnpj")
    private String cnpj;

    @ManyToMany
    @JoinTable(name="endereco_entrega", joinColumns={@JoinColumn(name="fk_cliente")}, inverseJoinColumns={@JoinColumn(name="fk_endereco")})
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Endereco> endereco;

    @ManyToMany
    @JoinTable(name="pedido_cliente", joinColumns={@JoinColumn(name="fk_cliente")}, inverseJoinColumns={@JoinColumn(name="fk_pedido")})
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Pedido> pedido;
}

其中一些属性是其他实体。任何人都可以为我指明方向吗?

更新 1

Usuario.java

@Entity
@Table(name="usuario")
public class Usuario extends Entidade {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "login")
    private String login;

    @Column(name = "senha")
    private String senha;

    @Column(name="first_name")
    private String first_name;

    @Column(name="last_name")
    private String last_name;

    @Column(name="email")
    private String email;

    @ManyToMany
    @JoinTable(name="role_members", joinColumns={@JoinColumn(name="fk_user")}, inverseJoinColumns={@JoinColumn(name="fk_role")})
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Role> Autorizacao = new ArrayList<Role>();
}

Endereco.java

@Entity
@Table(name="endereco")
public class Endereco {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name="logadouro")
    private String logradouro;

    @Column(name="numero")
    private String numero;

    @Column(name="complemento")
    private String complemento;

    @Column(name="bairro")
    private String bairro;

    @Column(name="cidade")
    private String cidade;

    @Column(name="estado")
    private String estado;

    @Column(name="cep")
    private String cep;
}

Pedido.java

@Entity
@Table(name="pedido")
public class Pedido {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @ManyToMany
    @JoinTable(name="produtos_do_pedido", joinColumns={@JoinColumn(name="fk_pedido")}, inverseJoinColumns={@JoinColumn(name="fk_produto")})
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Produto> produto;

    @ManyToMany
    @JoinTable(name="cobranca_do_pedido", joinColumns={@JoinColumn(name="fk_pedido")}, inverseJoinColumns={@JoinColumn(name="fk_cobranca")})
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Cobranca> cobranca;
}
4

0 回答 0