使用spring-form标签插入/更新具有非基本属性的实体

时间:2014-07-06 16:18:06

标签: java spring spring-mvc spring-form

在我的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>

当我的实体类只有原始属性(Integer,String等)时,这段代码工作正常。但我无法弄清楚如何对类似的事情做同样的事情:

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

0 个答案:

没有答案