【问题标题】:want to access child properties with parent class id想要访问具有父类 ID 的子属性
【发布时间】: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


【解决方案1】:

在您的 AddressRepository 中,您按酒店搜索地址,因此您需要将 Hotel 对象传递到您的存储库。如果要按酒店 ID 搜索,则应创建 findAddressesByHotelId 方法。试试这两种可能性:

List<Address>findAddressByHotel(Hotel hotel);

List<Address>findAddressByHotelId(Long hotelId);

【讨论】:

    【解决方案2】:

    您可以先选择HotelhotelId,然后从中获取地址,但如果您不需要它可能不需要选择父级。您可以按照以下方式重新设计您的实体,以便使用父键轻松获取子实体;

    @Entity
    public class Address {
    
        @Embeddable
        public static class Pk implements Serializable {
    
            @MapsId
            @Column(name = "hotel_id")
            private String hotelId;
    
            @Column
            private String addressId;
    
            // getter & setters
        }
    
        @EmbeddedId
        private Pk pk;
    
        @JsonBackReference
        @MapsId("hotelId")
        @ManyToOne(fetch = LAZY)
        @JoinColumn(name = "hotel_id", referencedColumnName = "hotel_id", nullable = false)
        private Hotel hotel;
    
        // getters & setters
    }
    

    & 在Address.Pk 中使用此hotelId 以使用父键进行选择;

    @Repository
    public interface AddressRepository extends JpaRepository<Address, Long> {
    
        List<Address> findByPk_HotelId(Long hotelId);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多