How to search through array in Spring Boot CrudRepository

℡╲_俬逩灬. 提交于 2019-12-01 08:18:10

Ideally, You should declare cars as a separate Entity like this

@Entity
public class Person {
  @Id
  private String name;

  private List<Car> cars;

  // Constructor, getters and setters
}

If not you should change Array to List at the least. change

private String[] cars;

to

@ElementCollection
private List<String> cars;

Then You have to write a Query like this

@Query("select p from Person p WHERE :car in elements(p.cars)")
List<Person> getAllByCars...(@Param("car") String car)

I'm guessing at how you are currently storing the cars information and suggesting a possible solution:

@Entity
public class Car {
  @Id
  private String name;

  @Column
  private String person_name;
}

public interface CarRepository extends JpaRepository<Car, String> {
    //Result will have all cars with the person_name identifying the Person @Entity
    List<Car> findByName(String name);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!