I need to convert "horizontal" data representation into "vertical" one. For this I need to run INSERT query multiple times, one for each column of source table.
I can't annotate dozens of @Query
or something.
Can I just run some native query somewhere in the service?
UPDATE
I wrote the following:
@Component
@Slf4j
public class CopyFromInPopulationToBinValues extends Op {
@PersistenceContext
private EntityManager em;
...
@Override
public void run() {
String queryString, queryTemplate;
Bin bin;
Query query;
em.joinTransaction();
//binValueRepo.deleteByStatId(1);
queryString = "delete from bin_values where id in (select bv.id from bin_values bv inner join bin b on bv.bin_id = b.id where b.stat_id = ?1)";
query = em.createNativeQuery( queryString );
query.executeUpdate();
surpisingly, @PersistenceContext
annotation worked and I got not-null em
.
Unfortunately, I can't find valid combination of @Transactional
, @EnableTransactionManagement
annotation and em.joinTransaction()
call so that query worked.
If I don't have em.joinTransaction()
I get
javax.persistence.TransactionRequiredException: Executing an update/delete query
on query execute. If I have it, I get
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'joinTransaction'
on this call.
If I apply @Transactional
to the class or to the run()
method, I have the same situation (no effect).
If I apply @EnableTransactionManagement
(to database config) without any @Transactional
annotations (to this class), it also has no effect.
And if I apply @EnableTransactionManagement
along with any @Transactional
, I get
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '....CopyFromInPopulationToBinValues'