这(应该)是一件相当简单的事情,但我正在努力。
我想要这样生成一个表:
id organizationNumber name
However, when I look in the database, I see that the ordering is wrong. Does anybody know how I can force hibernate/jpa to generate the table with correct ordering?
desc Organization; +--------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | organizationNumber | varchar(255) | NO | UNI | NULL | | +--------------------+--------------+------+-----+---------+----------------+
This is how my entity bean looks like:
@Entity
@NamedQuery(name = "allOrganizations", query = "SELECT org FROM Organization org order by name")
public class Organization {
private Long id;
private String organizationNumber;
private String name;
public Organization() {
}
public Organization(String name) {
this.name = name;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(Long id) {
this.id = id;
}
@NotEmpty
@Column(unique=true, nullable=false)
public String getOrganizationNumber() {
return organizationNumber;
}
public void setOrganizationNumber(String organizationNumber) {
this.organizationNumber = organizationNumber;
}
@NotEmpty
@Column(nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.name + " " + this.organizationNumber;
}
}
最佳答案
Hibernate 按字母 顺序生成列。根据this post原因如下:
It is sorted to ensurce deterministic ordering across clusters.
We can't rely on the vm to return the methods in the same order every time so we had to do something.
显然它曾经是按出现顺序排列的,但在 3.2.0 GA 和 3.2.1 GA 之间发生了变化。
我还找到了Schema auto generation creates columns in alphabetical order for compound primary keys这似乎是你的问题。这张票是关于主键中的顺序更改,这对索引性能有负面影响。
除了以正确的顺序命名列的解决方法之外,没有解决此问题的方法(不,我不是在开玩笑)。
关于java - jpa 中生成的表中的顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52382327/