1

I'm creating a new Spring boot project with MySQL and I don't know why the ordering in my database is showing up alphabetically. I want to know if it can follow by my class ordering like id, name, email, gender then age.

This is my first time using this method to generate the database, usually I generate it from phpMyAdmin.

Here is my end result in phpMyAdmin:

id, address, age, email, gender, name

I want it to follow the order from my entity class which is:

id, name, email, gender, age, address

This is my application.properties:

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=user
spring.datasource.password=x


spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

I also created User.java:

package hello.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity // This tells Hibernate to make a table out of this class
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String name;

    private String email;

    private String gender;

    private String age;

    private String address;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

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



    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}

and UserRepository.java:

package hello.model;

import org.springframework.data.repository.CrudRepository;

import hello.model.User;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface UserRepository extends CrudRepository<User, Long> {

}
Gemmy
  • 21
  • 3
user2285201
  • 179
  • 4
  • 17
  • 1
    And what "order" were they in the DDL to create the table? because if you left it to your JPA provider it will be in the JPA provider log. I expect your JPA provider simply created the table with alphabetical ordering. JPA doesn't define the ordering, but some JPA providers have extensions allowing defining the order of columns ... does Hibernate? – Neil Stockton Jul 15 '17 at 18:08
  • You can reference https://stackoverflow.com/a/65731578/5046887 – Changemyminds Jan 15 '21 at 08:45

1 Answers1

5

Yes , you are right hibernate DDL generate columns names in alphabetically order. When hibernate do parsing of entity mapping it builds model based on fields from entity class. For handling entity proprties/fields it use PropertyContainer helper class , and this helper store fields in TreeMap. When DDL genarator start work it iterate fields and create db column name , as iterator from TreeMap gives sotred order all columns name also sorted. You can't change it.

xyz
  • 5,228
  • 2
  • 26
  • 35
  • 1
    that exactly i need to know and [https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html] good for a read – user2285201 Jul 17 '17 at 04:46