Thymeleaf和hibernate嵌套对象的形式

时间:2018-03-17 11:56:45

标签: hibernate spring-boot thymeleaf

我有一个引用某个国家/地区的用户。提取所有已知国家并将其注入变量${countries}(这些都正确显示)

 <form data-toggle="validator" autocomplete="off" th:action="@{/registration}"
                          th:object="${user}" method="post" class="form-signin"
                          role="form" id="form-registration">
      <select th:field="*{country}" class="form-control" required>
                <option
                                        th:each="country : ${countries}"
                                        th:text="${country.name}"
                                        th:value="${country.id}">
                </option>
      </select>

由于某些原因,我的用户没有在POST回调中引用所选国家/地区:

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult, HttpServletRequest request) {...

因此,看起来百里香的问题是将id解析为国家/地区对象。我拒绝为这个简单的用例编写一个包装器。有什么想法吗?

用户

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id", nullable = false)
private Country country;

...

国家

@Entity
@Table(name = "country")
public class Country {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "country_id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "iso_code")
private String isoCode;
@OneToMany(mappedBy="country")
private Set<User> user;

...

0 个答案:

没有答案