jpa中生成的表中的错误排序 [英] Wrong ordering in generated table in jpa

查看:25
本文介绍了jpa中生成的表中的错误排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想像这样生成一个表格:

<前>ID组织编号姓名

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

<前>组织机构;+--------------------+--------------+------+-----+---------+----------------+|领域 |类型 |空 |钥匙 |默认 |额外 |+--------------------+--------------+------+-----+---------+----------------+|身份证 |bigint(20) |否 |PRI |空 |自动增量||姓名 |varchar(255) |否 ||空 |||组织编号 |varchar(255) |否 |联合 |空 ||+--------------------+--------------+------+-----+---------+----------------+

这是我的实体 bean 的样子:

@Entity@NamedQuery(name = "allOrganizations", query = "SELECT org FROM Organization org order by name")公共类组织{私人长ID;私人字符串组织编号;私人字符串名称;公共组织(){}公共组织(字符串名称){this.name = 名称;}@ID@GeneratedValue公共长 getId() {返回标识;}@SuppressWarnings("未使用")私有无效 setId(长 ID){this.id = id;}@不是空的@Column(唯一=真,可为空=假)公共字符串 getOrganizationNumber() {返回组织编号;}公共无效setOrganizationNumber(字符串组织编号){this.organizationNumber =组织编号;}@不是空的@Column(可为空=假)公共字符串 getName() {返回名称;}公共无效集名称(字符串名称){this.name = 名称;}@覆盖公共字符串 toString() {返回 this.name + " " + this.organizationNumber;}}

解决方案

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

<块引用>

排序以确保确定性跨集群排序.

我们不能依赖虚拟机来返回方法每次都以相同的顺序所以我们必须做点什么.

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

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

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

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?

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 generates columns in alphabetical order. According to this post the reason is given as:

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.

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

I also found Schema auto generation creates columns in alphabetical order for compound primary keys and 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).

这篇关于jpa中生成的表中的错误排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆