1

I have a entity with following columns:

@Table(name="person")
public class Person {
    
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String location;
    
    @CreationTimestamp
    private Date brithDate;

    public Person() {

    }

    public Person(String name, String location, Date brithDate) {
        
        this.name = name;
        this.location = location;
        this.brithDate = brithDate;
    }
    
    

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Date getBrithDate() {
        return brithDate;
    }

    public void setBrithDate(Date brithDate) {
        this.brithDate = brithDate;
    }

    @Override
    public String toString() { 
        return "\nPerson [id=" + id + ", name= " + name + ", location= " + location + ", brithDate= " + brithDate + "]";
    }
    
    

}

after the generation of table it produces columns as :

enter image description here

which is totally different than table generated by this query:

CREATE TABLE person(
    id INTEGER NOT NULL,
    name VARCHAR(255) NOT NULL,
    location VARCHAR(255),
    birth_date TIMESTAMP,
    PRIMARY KEY(id)
);

though I put the variables as same order in query. How to control this behavior of generation of table columns in alphabetical order?

Anish B.
  • 9,111
  • 3
  • 21
  • 41
Ishwor Upreti
  • 27
  • 1
  • 8
  • 1
    Are you sure it isn't the SQL client you're using that shows them sorted? Try `describe person` to confirm. – sp00m Dec 09 '20 at 15:01
  • **desc person** gives following `'id', 'int', 'NO', 'PRI', NULL, '' 'brith_date', 'datetime(6)', 'YES', '', NULL, '' 'location', 'varchar(255)', 'YES', '', NULL, '' 'name', 'varchar(255)', 'YES', '', NULL, ''` – Ishwor Upreti Dec 09 '20 at 15:07
  • 1
    @IshworUprety By default, hibernate does. Look at this : https://stackoverflow.com/questions/1298322/wrong-ordering-in-generated-table-in-jpa – Anish B. Dec 09 '20 at 15:09

1 Answers1

1

There is no certain order of columns when tables are auto-generated or created by hibernate.

This is an issue for many developers but still there is no solution available given by Hibernate team.

If you want you can create table columns manually in alphabetic order. That can be a workaround I can suggest.

Anish B.
  • 9,111
  • 3
  • 21
  • 41