Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

want to search with the city from address table.

Entity: Hotel

@JsonManagedReference
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "hotel")
    private List<Address> address;

Entity: Address

@ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "hotel_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonBackReference
    private Hotel hotel;

controller:

@GetMapping("/forSearch")
    public String searchHotelPage(Model model, Address address){
        model.addAttribute("address", address);
        return "search-hotel";
    }

    @GetMapping("/search-hotel-city/{city}")
    public String searchHotel(@RequestParam("city") String city, Model model){
        List<Address> addresses = addressService.searchAddressByCity(city);
        System.out.println("this is address: "+ addresses);
        model.addAttribute("address",addresses );
        return "search-hotel";
    }

service:

public List<Address> searchAddressByCity(String city) {
        return addressRepository.findAll();
    }

search and list view:

<form th:action="@{/search-hotel-city/city?city=${address.city}}" method="get">
            <div class="form-group mb-2">
                <input type="text" class="form-control" name="city" id="city" placeholder="Search "/>
                <input type="submit" value="search" class="btn btn-primary">   
            </div>
        </form>

        <hr/>

        <table class="table">
            <thead>
            <tr>
                <th scope="col">Id</th>
                <th scope="col">roadNumber</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>
        </table>

Error:

Hibernate: select address0_.hotel_id as hotel_id5_0_0_, address0_.id as id1_0_0_, address0_.id as id1_0_1_, address0_.city as city2_0_1_, address0_.country as country3_0_1_, address0_.hotel_id as hotel_id5_0_1_, address0_.road_number as road_num4_0_1_ from address address0_ where address0_.hotel_id=?
2019-07-28 12:58:28.184 ERROR 9836 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause

java.lang.StackOverflowError: null
    at java.lang.Exception.<init>(Exception.java:102) ~[na:1.8.0_212]
    at java.lang.ReflectiveOperationException.<init>(ReflectiveOperationException.java:89) ~[na:1.8.0_212]

I want to search for the city. but when I searching StackOverflowError is occurring. I have tried but unable to solve this problem. please help me. thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
268 views
Welcome To Ask or Share your Answers For Others

1 Answer

Remove System.out.println("this is address: "+ addresses); from searchHotel. It may lead to infinite calls which result in StackOverflowError.

Possible Reasons:

  • The toString() method for most List implementations iterates through the List elements and calls toString() on them. 1
  • using Lombok, with your automatics, generated hashCode was used by collection so running infinite calls 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...