我正在使用MySQL创建一个新的Spring启动项目,我不知道为什么我的数据库中的排序按字母顺序显示。我想知道它是否可以跟我的班级订购一样,如id,姓名,电子邮件,性别和年龄。
这是我第一次使用此方法生成数据库,通常我是从phpMyAdmin生成的。
这是我在phpMyAdmin中的最终结果:
id, address, age, email, gender, name
我希望它遵循我的实体类的顺序:
id, name, email, gender, age, address
这是我的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
我还创建了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;
}
}
和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> {
}
答案 0 :(得分:1)
是的,你是对的hibernate DDL按字母顺序生成列名。当hibernate对实体映射进行解析时,它会根据实体类中的字段构建模型。对于处理实体属性/字段,它使用PropertyContainer助手类,并在TreeMap中使用此助手存储字段。当DDL genarator开始工作时,它迭代字段并创建db列名,因为TreeMap中的迭代器给出了sotred顺序,所有列名也被排序。你无法改变它。