Home > Back-end >  H2 database columns and values don't converge JAVA Spring
H2 database columns and values don't converge JAVA Spring

Time:11-30

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:

enter image description here

Why? And how can I fix that?

CodePudding user response:

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.

CodePudding user response:

agree with @a_horse_with_no_name, if you not specify column names it will insert based on the position/index. And all your java variables are in string that is the reason it does't throw any classcast exception.

  • Related