Home > Mobile >  How to join unidirectional entities from the target side?
How to join unidirectional entities from the target side?

Time:10-26

I have Car and CarType entities involved in a unidirectional relationship as follows:

public class Car {

  @Id
  private Long id;

  @ManyToOne
  private CarType type;
  
  //other fields / getters / setters
}

public class CarType {

   @Id
   private Long id;

   private String code;

  //other fields / getters / setters

}

This unidirectional relationship has worked well to meet the requirements. But now I have a new requirement to get the set of CarTypes for given set of Car ids.

How can I do this without using a native query and without creating a bidirectional relationship?

CodePudding user response:

This should work

@Query("select c.type from Car c where c.id in (:ids)")
List<CarType> findCarTypesFor(List<Long> ids)
  • Related