findAll UUID MongoRepository

时间:2017-12-28 16:13:47

标签: java spring mongodb spring-mongo spring-mongodb

我试图使用mongo-spring-boot获得findAllByUUID,但没有运气。 我有什么:

public interface CarMatchRepository extends MongoRepository<CarMatchEntity, String> {
    List<CarMatchEntity> findAllByCarID(Iterable<UUID> ids);
    CarMatchEntity findByCarID(UUID carID);
}

函数调用:

public void addCarsToCollection(String id, List<UUID> carId) {
   List<CarMatchEntity> entities = carMatchRepository.findAllByCarID(carId); <--- empty
}

如果我调用findByCarID(),它会正确检索单个对象(如果存在),但使用Iterable,查询不会失败,但它永远不会返回任何对象。我在这里做错了什么,还是我走错了路?

谢谢!

修改

@Document(collection = "car_index")
public class CarMatchEntity implements Serializable {

    @Id
    private String id;

    private UUID carID;

    //partner data
    private UUID partnerID;
    private String partnerThumbURL;
    private String partnerName;
    private Date partnerMembershipSince;

    // car location
    private List<Double> location;
    private String district;
    private String city;

    // car data
    private CarType carType;
    private String carBrand;
    private String carModel;
    private String carPlate;
    private List<CarFeature> carFeatures;

    private String carAddress;

    private String description;
    private BigDecimal hourFare;
    private BigDecimal dayFare;
    private BigDecimal weekFare;
    private BigDecimal dailyPrice;
    private BigDecimal suggestedHourlyPrice;
    private BigDecimal suggestedDailyPrice;
    private BigDecimal suggestedWeeklyPrice;
    private String carThumbURL;
    private Map<String, CarPhotos> carPhotosURL;
    private CarAvailability availability;
    private CarStatus carStatus;
    private String carYear;
    private FuelType fuelType;

    @Transient
    private DayOfWeek prohibitedDay;

    private String carYearModel;

    @Transient
    private double partnerRating = 5.0;
    private CarTransmission carTransmission;
    private CarColor carColor;
    private String odometer;
    private Integer manufactureYear;
    private String fipeCode;
    private String renavam;
    private String chassi;
    private InsuranceCompany insuranceCompany;
    private List<CarSpecialFeature> carSpecialFeatures;
    private BigDecimal deductible;
    private Boolean superCar;

    public CarMatchEntity() {

    }

2 个答案:

答案 0 :(得分:1)

尝试使用基于JSON的查询和SpEL表达式

@Query("{carID: { $in: ?0 } })")
List<CarMatchEntity> findAllByCarIds(List<UUID> ids);

答案 1 :(得分:0)

使用

List<CarMatchEntity> findAllByCarIDIn(Iterable<UUID> ids);

而不是

List<CarMatchEntity> findAllByCarID(Iterable<UUID> ids);

<强>更新: 您是否尝试显式声明JPQL查询而不是依赖于Spring Data查询生成机制?

@Query("select e from CarMatchEntity e where e.carID in (:ids)")
List<CarMatchEntity> findAllByCarID(@Param("ids") Iterable<UUID> ids);

更新2: 我要尝试的另一个解决方案是将ids方法中的参数findAllByCarIDIn声明为Collection<UUID>而不是Iterable<UUID>