提交的表单未完全保存在数据库中

时间:2014-09-28 18:27:38

标签: spring forms hibernate spring-mvc submit

在我目前的春季项目中,其中一个呈现的形式如下:

        <form id="Usuario" class="form" role="form" method="post" action="/caixa/Usuario/insert">
            <li class="list-group-item">
                <field-box>
                  <label>login</label>
                  <input type="text" name="login" id="login" class="form-control" />
                </field-box>
            </li>
            <li class="list-group-item">
                <field-box>
                  <label>senha</label>
                  <input type="password" name="senha" id="senha" class="form-control" />
                </field-box>
            </li>
            <li class="list-group-item">
                <field-box>
                  <label>nome</label>
                  <input type="text" name="nome" id="nome" class="form-control" />
                </field-box>
            </li>
            <li class="list-group-item">
                <field-box>
                  <label>sobrenome</label>
                  <input type="text" name="sobrenome" id="sobrenome" class="form-control" />
                </field-box>
            </li>
            <li class="list-group-item">
                <field-box>
                  <label>email</label>
                  <input type="email" name="email" id="email" class="form-control" />
                </field-box>
             </li>
            <li class="list-group-item">
                <field-box>
                  <label>role</label>
                  <select multiple="multiple" name="role.id" id="role" class="form-control option" data-classe="Role">
                  </select>
                </field-box>
            </li>

            <button type="submit" id="cadastrar" class="btn btn-primary">Cadastrar</button>

            <div class="alert alert-success alert-dismissible" id="yes" role="alert" style="display: none;">
                <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                Cadastro efetuado com sucesso
            </div>
            <div class="alert alert-danger alert-dismissible" id="not" role="alert" style="display: none;">
                <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <span class="text"></span>
            </div>
        </form>

通过此表单提交的数据与此类有关:

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

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

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

    @Column(name = "senha")
    @Input(type = "password")
    private String senha;

    @Column(name = "nome")
    @Input
    private String nome;

    @Column(name = "sobrenome")
    @Input
    private String sobrenome;

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

    @ManyToMany
    @JoinTable(name="role_members", joinColumns={@JoinColumn(name="fk_user")}, inverseJoinColumns={@JoinColumn(name="fk_role")})
    @LazyCollection(LazyCollectionOption.FALSE)
    @Select(classe = Role.class)
    private List<Role> role;
}

提交过程由此jquery代码处理:

    $(document).on("submit", "form.form", function (event) {
        event.preventDefault();
        var $form = $( this ), url = $form.attr( "action" );
        var posting = $.post( url, $(this).serialize() );
        posting.done(function( data ) {
            if(data == "") {
                $("#yes").show();
            } else {
                var $temp  = $('<div/>', {html:data});
                $("#not").find(".text").html( $temp.remove('head').html() );
                $("#not").show();
            }
            $("form.form").each(function(){
                this.reset();
            });
        });
    });

并通过我的控制器,服务和dao类中的这些方法:

@RequestMapping(value = "insert")
@ResponseBody
public void insert(@ModelAttribute("object") E object) throws Exception {
    try {
        serv.insert(object);
    } catch (Exception e) {
        throw e;
    }
}

public void insert(E object) {
    dao.persist(object);
}

@Transactional
public void persist(E object) {
    sessionFactory.getCurrentSession().persist(object);
}

但是当我运行应用程序并在浏览器中打开此视图并提交此表单时,数据将保存在数据库中,而不包含<select>字段中的数据。我检查了浏览器控制台(在firefox中),并验证所有字段都已提交给服务器,但字段role是唯一没有存储的字段。

任何人都可以看到这里的内容是什么?

1 个答案:

答案 0 :(得分:0)

我认为您在@JoinTable(name =“role_members”,joinColumns = {@ JoinColumn(name =“ fk_user ”)}中给出的列名中的问题,inverseJoinColumns = {@ JoinColumn(name) =“ fk_role ”)})

列名称fk_user不存在于您的实体类中。如果要使用其他名称,请使用referencedColumnName属性并提供实际字段名称。 例如。我们正在使用来自两个实体的id然后在更改后它将像

@JoinTable(name="role_members", joinColumns={@JoinColumn(name="fk_user", referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="fk_role",referencedColumnName="id")}) 

希望它会奏效。