0

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;
Tornike
  • 19
  • 1
  • 9
  • I might ask why you have the occasion to store duplicated data in your tables. If this is just a one time thing, maybe handle it directly on your database. If it recurring and/or it needs to happen from your Hibernate layer, then maybe consider why you are duplicating data. – Tim Biegeleisen Mar 12 '19 at 12:59
  • about recurring thing, I know how to update new data whenever I add it to the database to get the personalNumbers and update them as well but I want to update old data with this new field. Duplicate data is needed to access personal information of a client from directly NaturalPersonReserve Table. – Tornike Mar 12 '19 at 13:04

1 Answers1

0

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.

Vitor Santos
  • 581
  • 4
  • 15