【发布时间】:2019-07-22 08:46:09
【问题描述】:
我是春季靴子的新手。我有酒店类和地址类。酒店和地址之间的关系是一对多的。我想用酒店 id 搜索地址。
酒店等级:
@OneToMany(cascade = CascadeType.ALL)
private List<Address> address;
地址类:
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "hotel_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Hotel hotel;
控制器:
@GetMapping("/search/{hotelId}")
public String searchAddressHotel(@PathVariable("hotelId") Long hotelId, Model model){
List<Address> addresses= addressService.findAddressByHotelId(hotelId);
model.addAttribute("addresses", addresses);
return "address-list";
}
查看:
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Road Number</th>
<th scope="col">City</th>
<th scope="col">Country</th>
</tr>
</thead>
<tbody>
<tr th:each="address: ${addresses}">
<th scope="row" th:text="${address.id}"></th>
<td th:text="${address.roadNumber}"></td>
<td th:text="${address.city}"></td>
<td th:text="${address.country}"></td>
</tr>
</tbody>
存储库:
@Repository
public interface AddressRepository extends JpaRepository<Address, Long> {
List<Address>findAddressByHotel(Long hotelId);
}
当我在浏览器中使用此链接 (localhost:8080/search/1) 进行搜索时,显示此异常:
2019-07-22 14:33:09.340 ERROR 12216 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [1] did not match expected type [com.shaon.SpringBootLogin.model.Hotel (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [com.shaon.SpringBootLogin.model.Hotel (n/a)]] with root cause
java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [com.shaon.SpringBootLogin.model.Hotel (n/a)]
为什么酒店 ID 与地址表中的酒店 ID 列不匹配。请帮帮我。
【问题讨论】:
-
你能看看我的回答吗,希望对你有帮助!我给出了一个有效的答案,您无需选择父母再次选择孩子,直接可以根据需要获取具有父母ID的孩子。
-
hmm,添加
spring rest repositories将为您完成工作,您甚至不必编写查询 xd -
谢谢大家。我已经解决了我的问题。实际上问题出在存储库中。我对自定义方法有一点问题。现在一切正常。
-
@mdshaon 你能对我的回答提供反馈吗?谢谢!
标签: java spring spring-boot spring-mvc spring-data-jpa