S
S
Samvel Ayvazyan2020-01-19 17:18:41
Java
Samvel Ayvazyan, 2020-01-19 17:18:41

How to fill a column in one table with data from a column in another table when the program starts?

There are two tables in MySQL:
a) event:

id | provider_id | client_id
1 | 1                  | <null>
2 | 2                  | <null>
3 | 2                  | <null>
4 | 3                  | 4

b) providers:
id | client_id
1  | 23
2  | 45
3  | 4

The task is to update the client_id field from the Event table with the values ​​from the client_id field of the Providers table.
The request for this case has already been described:
UPDATE event
INNER JOIN providers ON event.provider_id = providers.id
SET event.client_id = providers.client_id

Question - is it possible to configure the launch of the program on Spring / Spring Boot in such a way that every time the program / build is launched in CI / CD, this SQL query is executed?
There is an opinion that this can be implemented using the Job (Spring Batch) class, but I do not quite understand how this is done.
I do not ask for the solution of the problem itself, but at least a side - where to dig, what to find the solution yourself.
Thanks in advance! :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-01-19
@mr_zadre

You can simply declare a method annotated with @PostConstruct in any convenient bean and perform the necessary actions in it. It's only better to make this bean dependent on the EntityManager so that it doesn't get created earlier. Or declare an ApplicationListener that listens for the ContextRefreshedEvent. Only it is not clear why, the whole task looks like a crutch.

H
hudrogen, 2020-01-23
@hudrogen

If a build tool is used, then maven has sql:execute for example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question