Пишу критерий запроса со сравнением 2 раза. При запуске службы возникло исключение:
2020-06-19 19:12:08.688 - WARN 27740 [ault-executor-0] o.h.e.j.s.SqlExceptionHelper : SQL Error: 402, SQLState: S0001
2020-06-19 19:12:08.688 -ERROR 27740 [ault-executor-0] o.h.e.j.s.SqlExceptionHelper : The data types time and datetime are incompatible in the greater than or equal to operator.
Вот определение моей сущности:
@Entity
@Getter
@Setter
@SuperBuilder
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@SelectBeforeUpdate
@TimeFlowValidation
@Table(name = "[PARKING_LOT]")
public final class ParkingLotEntity extends BaseEntity {
@ManyToOne(optional = false)
@JoinColumn(name = "[PARKING_LOT_TYPE_ID]", referencedColumnName = "[ID]", nullable = false, updatable = false)
private ParkingLotTypeEntity parkingLotTypeEntity;
@Column(name = "[LATITUDE]", nullable = false)
private Double latitude;
@Column(name = "[LONGITUDE]", nullable = false)
private Double longitude;
@Column(name = "[OPENING_HOUR]", nullable = false)
private Time openingHour;
@Column(name = "[CLOSING_HOUR]", nullable = false)
private Time closingHour;
@ColumnDefault("true")
@Column(name = "[IS_AVAILABLE]")
private Boolean isAvailable;
...
Вот модель поколения JPA:
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(ParkingLotEntity.class)
public abstract class ParkingLotEntity_ extends com.bht.saigonparking.common.base.BaseEntity_ {
public static volatile SingularAttribute<ParkingLotEntity, ParkingLotTypeEntity> parkingLotTypeEntity;
public static volatile SingularAttribute<ParkingLotEntity, Time> openingHour;
public static volatile SingularAttribute<ParkingLotEntity, Boolean> isAvailable;
public static volatile SetAttribute<ParkingLotEntity, ParkingLotUnitEntity> parkingLotUnitEntitySet;
public static volatile SingularAttribute<ParkingLotEntity, ParkingLotLimitEntity> parkingLotLimitEntity;
public static volatile SingularAttribute<ParkingLotEntity, Double> latitude;
public static volatile SingularAttribute<ParkingLotEntity, Time> closingHour;
public static volatile SingularAttribute<ParkingLotEntity, ParkingLotInformationEntity> parkingLotInformationEntity;
public static volatile SetAttribute<ParkingLotEntity, ParkingLotRatingEntity> parkingLotRatingEntitySet;
public static volatile SingularAttribute<ParkingLotEntity, Double> longitude;
public static final String PARKING_LOT_TYPE_ENTITY = "parkingLotTypeEntity";
public static final String OPENING_HOUR = "openingHour";
public static final String IS_AVAILABLE = "isAvailable";
public static final String PARKING_LOT_UNIT_ENTITY_SET = "parkingLotUnitEntitySet";
public static final String PARKING_LOT_LIMIT_ENTITY = "parkingLotLimitEntity";
public static final String LATITUDE = "latitude";
public static final String CLOSING_HOUR = "closingHour";
public static final String PARKING_LOT_INFORMATION_ENTITY = "parkingLotInformationEntity";
public static final String PARKING_LOT_RATING_ENTITY_SET = "parkingLotRatingEntitySet";
public static final String LONGITUDE = "longitude";
}
Вот моя функция:
@Override
public Long countAll(boolean isAvailable) {
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
Root<ParkingLotEntity> root = query.from(ParkingLotEntity.class);
return entityManager.createQuery(query
.select(criteriaBuilder.count(root))
.where(isAvailable
? criteriaBuilder.and(
criteriaBuilder.equal(root.get(ParkingLotEntity_.isAvailable), true),
criteriaBuilder.greaterThanOrEqualTo(root.get(ParkingLotEntity_.openingHour), criteriaBuilder.currentTime()),
criteriaBuilder.lessThanOrEqualTo(root.get(ParkingLotEntity_.closingHour), criteriaBuilder.currentTime()))
: criteriaBuilder.or(
criteriaBuilder.equal(root.get(ParkingLotEntity_.isAvailable), false),
criteriaBuilder.lessThan(root.get(ParkingLotEntity_.openingHour), criteriaBuilder.currentTime()),
criteriaBuilder.greaterThan(root.get(ParkingLotEntity_.closingHour), criteriaBuilder.currentTime()))))
.getSingleResult();
}
Думаю, проблема в сравнении с lessThan, большеThan, lessThanOrEqual, большеThanOrEqual.
Я следую инструкциям по stackoverflow, который добавляет «sendTimeAsDateTime = false» в строку подключения, но, похоже, не работает.
Дополнительная информация: я использую базу данных MS SQL и Hibernate 5.4.17 с Spring Boot и Spring Data Jpa.
Что-то не так с моей реализацией? Я с нетерпением жду ответа от всех вас. Большое спасибо!