简体   繁体   中英

How can I write a repository method for this situation?

I know this isn't the best way to define a car class, but I got curious and I ended up writing this:

@Entity
@Table(name = "CAR")
public class Car {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String manufacturer;
    private String model;

    @ElementCollection
    @CollectionTable(
        name="ACCESSORY",
        joinColumns=@JoinColumn(name="OWNER_ID")
    )
    @Column(name="ACCESSORIES")
    private List<String> accessories;

    //getters and setters
}

and its repository

@Repository
public interface CarRepository extends JpaRepository<Car, Long> {   
}

Now, considering that I want to create a repository method that finds all the cars that meet a given list of accessories, how can I do it? I was thinking about a method like findById() or something. By the way, feel free if you want to answer in a way that "Accessories" is an entity.

Disclaimer: Yes, I tried the method List<Car> findByAccessoriesIn(List<String> as);but it brings any car that has at least one of the elements inside the list. I want all the cars that have all items inside the list.

“finds all the cars that meet a given list of accessories“

If i understood correctly then - findByAccessoriesIn(List yourStrList) should work in this case.

A similar thread is here: Spring CrudRepository findByInventoryIds(List<Long> inventoryIdList) - equivalent to IN clause

For List in jpa we use “in” where “i” is in caps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM