在Thymeleaf中使用'select'标签和实体

Fil*_*nov 5 java spring-mvc thymeleaf

我正在创建一个带有select标签的表单,如下所示:

<form th:object="${version}" method="post" class="form-horizontal">
    ...
    <div class="control-group" th:classappend="${#fields.hasErrors('product')} ? 'error'">
        <label class="control-label" for="product" th:text="#{version.product}">Product</label>
        <div class="controls">
            <select id="product" th:field="*{product}">
                <option value="" th:text="#{common.select.prompt}"></option>
                <option th:each="p : ${productList}" th:value="${p.id}"  th:text="${p.name}"></option>
            </select>
            <span class="help-inline" th:errors="*{product}"></span>
        </div>
    </div>
    ...
</form>
Run Code Online (Sandbox Code Playgroud)

DomainClassConverter当我提交表单时,Spring Data JPA有助于将所选内容自动转换id为实体Product的类.该product也应该是不为空(我用@NotNullproduct领域Version类.

我遇到的问题 - 当我回来编辑数据时,Product没有选中.

如果我修改了select这个(th:fieldth:errors):<-- p.s. is not a sad smile

<select id="product" th:field="*{product.id}">
    <option value="" th:text="#{common.select.prompt}"></option>
    <option th:each="p : ${productList}" th:value="${p.id}" th:text="${p.name}"></option>
</select>
<span class="help-inline" th:errors="*{product.id}"></span>
Run Code Online (Sandbox Code Playgroud)

然后当我回来编辑它时它会被选中,但验证器不起作用(product总是实例化,即使选择了id null).

它看起来像一个非常常见的场景(从列表中选择一个实体),但我找不到任何好看的例子.请分享秘密知识.

Fil*_*nov 3

解决了。问题的存在是因为我没有重写equals()hashCode()方法。