I want to migrate the data from one table to another, besides the two tables with different table name they also have different schema.
For example, existing table are:
@Entity
public class Address {
private String uuid;
private String street;
private String postcode;
}
@Entity
public class User {
private String uuid;
private String name;
@One2One
private Address address;
}
After the requirements changed, we refactor the two class to something like this:
@Entity
public class UserChanged {
private String uuid;
private String name;
@Embedded
@AttributeOverrides(.....)
private Address address;
}
@Embeddable
public class AddressChanged {
private String street;
private String postcode;
}
Which means, in the new, we have only one table “UserChanged”, we have all the columns contain the user information, like uuid, name, street, and postcode. However for the existing, we have two tables which are “User” and “Address”, in the “User” table, there is a foreign key “address” which reference the table “Address” using the primary key.
Address
------------------
uuid varchar (primary key)
street varchar
postcode varchar
User
------------------
uuid varchar (primary key)
name varchar
address_id varchar (foreign key)
UserChanged
------------------
uuid varchar (primary key)
name varchar
street varchar
postcode varchar
So what I want to do is migrate all the data information in “User” and “Address” tables to just one table “UserChanged”, i.e. User + Address -> UserChanged.
Anyone can tell me the sql scripts I should write for this migration requirement.
1 Answer