How to set the column order of a composite primary key using JPA/Hibernate

隐身守侯 提交于 2019-12-01 22:15:50

I really don't think there is a way to do this. All I can do is suggest you use the SQL create statement you have (change it to have the correct order) and run it manually in production.

In tests let Hibernate do its thing.

Paulius Matulionis

There is a way to do it. How hibernate chooses to order a set of columns for a primary key is alphabetical by your object names defined.

So for e.g. if you declare your objects like this:

private byte loc;
private long epochtime;

You'll get as you are getting now:

(`epochtime`,`loc`)

But if you rename them for e.g.:

private byte aloc;
private long epochtime;

It would generate it as:

(`aloc`, `epochtime`)

As a comes before e.

That's what I found out when I wanted my clustered index to be in the specific order. I know it is irritating but it's the only way I could find so that I won't have to change my schema manually.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!