I need to iterate over collection of Enteties and apply UPSERT for each element atomically. As i see it – just need to send this collection into SQL query by SpEL.
I know, what i can iterate over collection by Java code, and send Enteties one by one, but it will not be as atomically as I want to realize. (Or I maybe don’t understand some things).
I wrote a query like this
DO
$do$
BEGIN
FOR entity IN :#{#entitiesCollection}
LOOP
INSERT INTO entity_table AS e (id,
index,
enum_field,
version,
is_active)
VALUES (nextval('entity_id_seq'),
:entity.index,
:entity.enum_field.name(),
:entity.version,
:entity.is_active)
ON CONFLICT (index) DO UPDATE SET field_value = :entity.field.getValue(),
version = :entity.version,
is_active = :entity.isActive
WHERE (e.index = :entity.index)
AND ( (e.version < :entity.version)
OR ((e.is_active <> :entity.isActive) AND (e.version = :entity.version)) );
END LOOP;
END
$do$
for my repository method
@Query(nativeQuery = true, value = "......")
void upsertCollection(@Param("entitiesCollection") List<Entity> entitiesCollection);
but got an exception
QueryException: Named Parameter Not Bound : entity.version
I understand – the error into SpEL references to @Param, but i can’t find the solution.
I would appreciate your help!
Andrew Yatkin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3