简体   繁体   中英

Java / Hibernate - Added a new Column to table, now I want to populate it with existing data from another Table

So basically I have two tables : NaturalPerson which holds personalNumber column and NaturalPersonReserve where I added a new column personalNumber . Both tables have existing data in it and I want to populate my NaturalPersonReserve's new Column personalNumber from naturalPerson s table ( I mean the existing data to update from One tables's column to second)

NaturalPerson Entity :

@Entity
@Table(name = "naturalperson")
public class NaturalPerson implements Serializable {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int personId;

    @Column(unique = true)
    private String personalNumber;

    @Column
    private String serialNumber;

    @Column
    private String firstname;

    @Column
    private String lastname;

    @Column
    private String birthdate;

    @Column
    private String gender;

NaturalPersonReserve Entity :

@Entity
@Table(name = "natural_person_reserve")
public class NaturalPersonReserve extends SuperModel{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne
    @JoinColumn(name = "payment_id")
    private PaymentParts payment;

    // The relationship
    @ManyToOne
    @JoinColumn(name = "person_id")
    private NaturalPerson person;

    @ManyToOne
    @JoinColumn(name = "company_id")
    private Company company;

    @Column(name = "amount", columnDefinition = "DECIMAL(10,2) DEFAULT 0.0")
    private double amount;

    @Enumerated(EnumType.STRING)
    @Column(name = "operation_type")
    private EReserveType operationType;

    // My added column
    @Column(unique = true)
    private String personalNumber;

Basically you added a new column, and you want to populate it with data from another table. That's not really a java/spring/hibernate issue. That kind of problem will exist regardless of how your application is built.

I can only see two ways:

  • Write a SQL statement that will update the table based on a select statement. The syntax may change depending on which DB you use. This post has an Oracle example.

  • Write a program (in your case probably in Java) that will update each register with the correct data. It will have the same effect as the above option, but it will be implemented in your language of choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM