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.