Search code examples
javadatabasehibernatejpaspring-data-jpa

How can I change the database table column order?


I am trying to learn something using this tutorial.

Now I connect my new database with my Spring (Java) something. However, I don't understand why in RUN output in IJ and SQL Shell shows wrong order of lines. The first line in column should be ID. After that, name, email, date, age should follow.

In reality columns are shown in this order: age,date,id,email,name. How can I change it?

Example from SQL Shell:

 Column |          Type          | Collation | Nullable | Default
--------+------------------------+-----------+----------+---------
 age    | integer                |           |          |
 dob    | date                   |           |          |
 id     | bigint                 |           | not null |
 email  | character varying(255) |           |          |
 name   | character varying(255) |           |          |
Indexes:
    "student_pkey" PRIMARY KEY, btree (id)

My Student class:


@Entity
@Table
public class Student {

    @Id
    @SequenceGenerator(
            name = "student_sequence",
            sequenceName = "student_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "student_sequence"
    )
    private Long id;
    private String name;
    private String email;
    private LocalDate dob;
    private Integer age;

    public Student() {
    }

    public Student(Long id, String name, String email, LocalDate dob, Integer age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.dob = dob;
        this.age = age;
    }

    public Student(String name, String email, LocalDate dob, Integer age) {
        this.name = name;
        this.email = email;
        this.dob = dob;
        this.age = age;
    }

Getters,Setters and to String

I've tried to change long to Long and nothing else. I don't know how to change order of lines here. It looks like everything is fine.


Solution

  • Because you configure Hibernate to auto-generate and create the table schema by setting spring.jpa.hibernate.ddl-auto to create-drop. It has its own rule to determine the order of the columns in the generated create tables DDL which the has nothing to do with the order of fields in the related entity Java class.

    Since hibernate 6.2 , it provides a way to customize the column order in the generated DDL through ColumnOrderingStrategy but the behaviour you want still not support out of the box.

    If you want to take back the total control about defining the column order in the tables , you have to manually create the table by yourself rather than rely on Hibernate to do it. You have to set spring.jpa.hibernate.ddl-auto to none to tell Hibernate not to do that.

    Note: consider to use the DB schema management tool such as Flyway or Liquibase to manage all schema changes for any serious production application.