Home > AI > Backend > SpringBoot > mysql-connector-java >

@Column(name =)

To avoid code refractor brings a new column in database

Without @Column(name=)

@Data
@NoArgsConstructor
@Entity
public class Employee {
    @Id
    @GeneratedValue
    public Long id;


    public String name;
}

If I refractor the attribute “name” to “firstname”

@Data
@NoArgsConstructor
@Entity
public class Employee {
    @Id
    @GeneratedValue
    public Long id;


    public String firstname;
}

We can see the old column is still there, while we have a new column. This would cause problem to the database.

So, we need to use @Column(name=) to fix the database column name regardless of the code attribute name

With @Column(name=)

@Data
@NoArgsConstructor
@Entity
public class Employee {
    @Id
    @GeneratedValue
    public Long id;


    @Column(name="name")
    public String firstname;
}

You are free to refractor.

Leave a Reply