javaspringspring-bootjpa

Spring mvc + Spring Jpa + tyhmleaf StackOverflowError


I'm working with spring boot and thymeleaf as template engine, when i tried to use Spring JPA method findAllByPropertyTitleContainning, i had StackOverflowError

Property.class

package com.example.estateagencyspring.models;

import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Data
public class Property {

    @Id
    private String id;
    private String propertyTitle;
    private String propertyDescription;
    private String location;
    private String type;
    private String status;
    private String area;
    private String beds;
    private String baths;
    private String garage;
    private String defaultPicture;
    private String price;
    @Lob
    private String googleSrc;


    @OneToMany(mappedBy="property")
    private List<Picture> pictures = new ArrayList<>();

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "property_amenity",
            joinColumns = @JoinColumn(name = "property_id"),
            inverseJoinColumns = @JoinColumn(name = "amenity_id")
    )
    private List<Amenity> amenities = new ArrayList<>();
}

this code of Amenity class, it has @ManyToMany relationship with Property class Amenity.class

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.ArrayList;
import java.util.List;

@Entity
@Data
public class Amenity {

    @Id
    private String id;
    private String name;

    @ManyToMany(mappedBy = "amenities", fetch = FetchType.LAZY)
    private List<Property> properties = new ArrayList<>();
}

PropertyRepository.class

public interface PropertyRepository extends JpaRepository<Property, String> {

    Optional<Property> findByPropertyTitle(String title);

    List<Property> findAllByPropertyTitleContaining(String title);

}

PropertyService.class

...
public List<Property> searchByTitle(String title){
        System.out.println(propertyRepository.findAllByPropertyTitleContaining(title));
        return propertyRepository.findAllByPropertyTitleContaining(title);
    }

Solution

  • I changed @Data to @Setter and @Getter.