Spring + jpa + jpa Crud repository + hibernate-envers application
I have two tables, UserRecord and UserRecord_AUD (the second is created automatically by Hibernate)
Table UserRecord id name surname age version Table UserRecord_AUD id name surname age version REV REVINFO
When I write a user to the database, it is written to the UserRecord table. If I update the record, for example, by updating the user's age, then the new version is written to the UserRecord, and the old one is transferred to the UserRecord_AUD. Thus, in UserRecord_AUD, all records (historicity) are accumulated and they have the same value in the Id field.
The goal is to read entries from RiskMetric_AUD with the same id using Spring's CrudRepository.
Example:
=============================== Table UserRecord 11-1 Ivan Ivanov 23 2 22-2 Natasha Ivanova 22 1 =============================== Table UsersRecord_AUD 11-1 Ivan Ivanov 9 0 11-1 Ivan Ivanov 9 1 22-2 Natasha Ivanova 22 0 ===============================
My repository
@Repository public interface UserRepository extends CrudRepository<UserRecord, String> {}
I need a custom method that accepts an id list. And instead of a sequential call (when I need to request a history 10 times for different id) repository.find (id);
One-time query on id list:
repository.find(ids);
How to do it?:
I tried
@Query(value = "select id, name, surname, age, REV as VERSION, IDCALC from USERRECORD_AUD where PR_KEY in (:ids)", nativeQuery = true) List<UserRecord> findHistoryByIds(List<String> ids);
But, got an exception
Must not be null or empty! On JDKs <8, you need to use @param for named parameters, on JDKs 8 or better, you need to compile with -parameters .; nested exception is java.lang.IllegalArgumentException: must be null or empty! On JDKs <8, you need to use @param for named parameters, on JDKs 8 or better, be sure to compile with -parameters.
My JPA Entity:
@Entity @Audited @Table(name = "UserRecord") @NoArgsConstructor(access = AccessLevel.PUBLIC) @AllArgsConstructor @Getter @Setter @Access(AccessType.FIELD) public class UserRecord { @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid2") @Column(name = "id") private String id; @Column(name = "name", length = 100, unique = false) private String name; @Column(name = "surname", length = 100, nullable = false) private String surname; @Column(name = "age", length = 100, nullable = false) private String age; @Version private int version;
I also tried the option:
@Query(value = "select id, name, surname, age, REV as VERSION, IDCALC from USERRECORD_AUD where PR_KEY in :ids", nativeQuery = true) List<UserRecord> findHistoryByIds(@Param("ids") List<String> ids);
But, for example, if there was one record and three updates, that is, in theory, it was expected that 4 different entities would return, one entity returned, 4 times.
Option
@Query("select h.PR_KEY, h.name, h.surname, h.age, h.REV as VERSION from USERRECORD_AUD h where h.PR_KEY in (:ids)") List<UserRecord> findHistoryByIds(List<String> ids);
returned a mapping error
QuerySyntaxException: USERRECORD_AUD is not mapped [select h.PR_KEY, h.name, h.surname, h.age, h.REV as v hrs.PR_KEY in (: ids) ")