'Hibernate/JPA - How to find all records, that match the pairs of given parameters, when they are not the primary key?

Consider a class Car

@Entity
class Car{
    @Id
    private UUID id; // primary key- this is the only thing that can't change.
    
    private String name;
    
    private String brand;
    
    private string model;
    
    @Data
    public static class Key{
        private String brand;
        private String model;
    }
}

There is a List.of(new Key("brand1","model1"), new Key("brand2","model2")). How is it possible to find all those given cars by those keys?

Is it doable with only JPA methods? Do I need to write native query? Can I modify something (besides primary key) in order to do it correctly?

I went to the direction of @EmbeddedId and then use the JPA method findAllById(Collection ..) which would be the naïve solution, but it's impossible since it must be the primary key in order to work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source