lives
lives

Reputation: 1185

JPA hibernate move data from one table to another

I would want to move data from one table to another.

The table names are different but all columns are same . One is active table and other is history table.

Conventional way is to create duplicate entities manually for the history table and then delete from active table and insert into history table.

Is there any other easy way of doing this ?

Upvotes: 2

Views: 4965

Answers (1)

SEY_91
SEY_91

Reputation: 1687

The easiest way to move data between tables is SQL:

INSERT INTO activeTable SELECT * FROM historyTable

Otherwise, with hibernate and spring-data-jpa you must deal with it manually like you said:

List<History> allData = historyRepository.findAll();
for(History h : allData) {
ActiveEntity e = new ActiveEntity();
e.setField(h.getField1());
activeEntityRepository.save(e);
}

Upvotes: 1

Related Questions