Java jpa中生成的表中的错误排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1298322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:58:12  来源:igfitidea点击:

Wrong ordering in generated table in jpa

javahibernatejpa

提问by Shervin Asgari

This (should) be a rather simple thing to do, however I am struggling.

这(应该)是一件相当简单的事情,但是我很挣扎。

I want a table to be generated like this:

我想像这样生成一个表:

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?

但是,当我查看数据库时,我发现排序是错误的。有人知道我如何强制休眠/jpa 以正确的顺序生成表吗?

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:

这是我的实体 bean 的样子:

@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;
    }
}

采纳答案by cletus

Hibernate generates columns in alphabeticalorder. According to this postthe reason is given as:

Hibernate按字母顺序生成列。根据这篇文章,原因如下:

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.

它被排序以确保跨集群的确定性排序。

我们不能依赖 vm 每次都以相同的顺序返回方法,所以我们必须做一些事情。

Apparently it used to be in the order of occurrence but this changed between 3.2.0 GA and 3.2.1 GA.

显然它曾经是按照出现的顺序,但在 3.2.0 GA 和 3.2.1 GA 之间发生了变化。

I also found Schema auto generation creates columns in alphabetical order for compound primary keysand this seems to be like your problem. This ticket is about the order changing in primary keys and that negatively impacts index performance.

我还发现模式自动生成按字母顺序为复合主键创建列,这似乎是您的问题。这张票是关于主键中的顺序变化,这会对索引性能产生负面影响。

There is no fix for this other than a workaround of naming the columns in such a way that they come out in the correct order (no, I'm not kidding).

除了以正确的顺序命名列的解决方法外,没有其他解决方法(不,我不是在开玩笑)。

回答by DataNucleus

DataNucleus allows the extension specifying the position for schema generation, FWIW.

DataNucleus 允许指定模式生成位置的扩展,FWIW。