【发布时间】:2018-04-26 12:38:24
【问题描述】:
我在将 Country 对象设置为 Pet 对象时遇到问题。
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@Entity
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
}
我的宠物班
@Data
@Entity
public class Pet {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne(fetch = FetchType.EAGER)
private Country country;
}
我尝试过这样做(国家是数据库中所有国家的列表,我将此作为属性添加到模型中):
<form th:object = "${pet}" th:action="@{/pet/}" method = "post">
<select class="form-control" th:field="*{country}" >
<option th:each="countryVal : ${countries}"
th:value="${countryVal}"
th:text="${countryVal.getName()}"
>val
</option>
</select>
</form>
但这不会将 country 对象保存到我的 Pet 对象中。 我必须这样做,我只将 id 保存到国家对象:
<form th:object = "${pet}" th:action="@{/pet/}" method = "post">
<select class="form-control" th:name="country.id" >
<option th:each="countryVal : ${countries}"
th:value="${countryVal.id}"
th:text="${countryVal.getName()}"
>val
</option>
</select>
</form>
有什么方法可以将我的国家/地区对象从列表保存到宠物对象中的国家/地区? 或者其他方式。 这个带有 country.id 的代码是否可以正确地将现有对象从数据库保存到我的 Pet 类中的对象?
【问题讨论】: