Существует класс с EmbeddedId.
@Entity
@NoArgsConstructor
@Getter
@Setter
@Table(name = "indicator_values")
public class IndicatorValue {
@EmbeddedId
public IndicatorValueId id;
@Column(name = "value")
public BigDecimal value;
// Конструктор для эксперимента (используется в репозитории)
public IndicatorValue(
String categoryCode,
String indicatorCode,
BigDecimal value,
LocalDate effectiveDate
) {
this.value = value;
this.id = new IndicatorValueId(categoryCode, indicatorCode, effectiveDate);
}
}
Помимо него существует класс @Embeddable, который определяет этот идентификатор.
@Embeddable
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@Setter
public class IndicatorValueId implements Serializable {
@Column(name = "category_code")
private String categoryCode;
@Column(name = "indicator_code")
private String indicatorCode;
@Column(name = "effective_date")
private LocalDate effectiveDate;
}
И репозиторий есть (пробовал разные способы проверить результат — это не полный список).
public interface IndicatorValueRepository extends JpaRepository {
@Query("""
SELECT iv
FROM IndicatorValue iv
WHERE iv.id.indicatorCode = :indicatorCode
AND iv.id.effectiveDate < :effectiveDate
ORDER BY iv.id.effectiveDate DESC
""")
List findByIndicatorCodeBeforeDate(
@Param("indicatorCode") String indicatorCode,
@Param("effectiveDate") LocalDate effectiveDate
);
@Query("""
SELECT NEW by.vezhlivec.normsservice.entity.IndicatorValue(
iv.id.categoryCode,
iv.id.indicatorCode,
iv.value,
iv.id.effectiveDate
)
FROM IndicatorValue iv
WHERE iv.id.indicatorCode = :indicatorCode
AND iv.id.categoryCode IS NULL
AND iv.id.effectiveDate < :effectiveDate
ORDER BY iv.id.effectiveDate DESC
""")
List findByIndicatorCodeBeforeDate1(
@Param("indicatorCode") String indicatorCode,
@Param("effectiveDate") LocalDate effectiveDate
);
List findByIdIndicatorCode(String indicatorCode);
List findByValue(BigDecimal value);
}
Основная проблема заключается в том, что данные извлекаются правильно только, когда я выбираю определенное поле, использую собственный запрос или выполняю сопоставление с помощью конструктора (как в findByIndicatorCodeBeforeDate1).
Во всех остальных случаях — при попытке получить объект или список объектов — я либо получаю значение NULL, либо список с правильным количеством элементов, но каждый элемент имеет значение null.
Даже стандартный метод findById() возвращает значение null, несмотря на то, что запись определенно существует в базе данных.
Вот тестовый фрагмент из сервиса
List iv = indicatorValueRepository.findByIdIndicatorCode("worker_4th_grade_hour_rate_construction");
System.out.println(iv);
List v = indicatorValueRepository.findByValue(new BigDecimal("13.02"));
System.out.println(v);
List iv1 = indicatorValueRepository
.findByIndicatorCodeBeforeDate("worker_4th_grade_hour_rate_construction", period.atDay(1));
System.out.println(iv1);
List iv2 = indicatorValueRepository
.findByIndicatorCodeBeforeDate1("worker_4th_grade_hour_rate_construction", period.atDay(1));
for (IndicatorValue iv3 : iv2) {
log.debug("Код категории: {}, код индикатора: {}, значение: {}, дата: {}",
iv3.getId().getCategoryCode(),
iv3.getId().getIndicatorCode(),
iv3.getValue(),
iv3.getId().getEffectiveDate());
}
Optional iv4 = indicatorValueRepository
.findById(new IndicatorValueId(
null,
"worker_4th_grade_hour_rate_construction",
LocalDate.of(2025, 9, 1)
)
);
System.out.println(iv4.orElse(null));
и соответствующий вывод журнала.
2025-11-28T22:28:44.318+03:00 DEBUG 414781 --- [nio-8080-exec-2] org.hibernate.SQL :
select
iv1_0.category_code,
iv1_0.effective_date,
iv1_0.indicator_code,
iv1_0.value
from
indicator_values iv1_0
where
iv1_0.indicator_code=?
2025-11-28T22:28:44.323+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
2025-11-28T22:28:44.324+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
2025-11-28T22:28:44.324+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
[null, null, null]
2025-11-28T22:28:44.326+03:00 DEBUG 414781 --- [nio-8080-exec-2] org.hibernate.SQL :
select
iv1_0.category_code,
iv1_0.effective_date,
iv1_0.indicator_code,
iv1_0.value
from
indicator_values iv1_0
where
iv1_0.value=?
2025-11-28T22:28:44.327+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
2025-11-28T22:28:44.327+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
2025-11-28T22:28:44.327+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
2025-11-28T22:28:44.327+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
2025-11-28T22:28:44.327+03:00 TRACE 414781 --- [nio-8080-exec-2] org.hibernate.orm.jdbc.extract : extracted value (1:VARCHAR) -> [null]
[null, null, null, null, null]
2025-11-28T22:28:44.330+03:00 DEBUG 414781 --- [nio-8080-exec-2] org.hibernate.SQL :
select
iv1_0.category_code,
iv1_0.effective_date,
iv1_0.indicator_code,
iv1_0.value
from
indicator_values iv1_0
where
iv1_0.indicator_code=?
and iv1_0.effective_date
Подробнее здесь: https://stackoverflow.com/questions/798 ... bedded-key