Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

H2 database columns and values don't converge JAVA Spring

I am new one at java and spring framework and have this problem. I have class, which has fields, that should be columns in H2. It looks like this:

package com.bankapp.bankwebapplication.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class PersonClient implements Client {
    @Id
    @Column(nullable = false, unique = true)
    private Long id;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    @Column(nullable = false)
    private String firstName;

    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }

    @Column(nullable = false)
    private String lastName;

    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }

    @Column(nullable = false)
    private String address;

    public String getAddress() { return address; }
    public void setAddress(String address) { this.address = address; }

    @Column
    private String workPhone;

    public String getWorkPhone() { return workPhone; }
    public void setWorkPhone(String workPhone) { this.workPhone = workPhone; }

    @Column
    private String homePhone;

    public String getHomePhone() { return homePhone; }
    public void setHomePhone(String homePhone) { this.homePhone = homePhone; }

    @Override
    public void getDetails() {

    }
}

Also, I have data.sql file that inserts 1 value into that table:

INSERT INTO person_client VALUES (1, 'firstName', 'lastName', 'paper street', '+123123', '+321321')

So, the problem is that it looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Why? And how can I fix that?

>Solution :

Always specify the target columns in INSERT statements:

INSERT INTO person_client 
  (id, first_name, last_name, address, home_phone, work_phone) 
VALUES 
  (1, 'firstName', 'lastName', 'paper street', '+123123', '+321321')

If you don’t specify the target columns, the values are matched by position and apparently the columns are created in a different order than you think they are.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading