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

后端 未结 2 997
面向向阳花
面向向阳花 2021-01-19 02:45

I\'m having trouble with the ordering of the columns in my composite primary key. I have a table that contains the following:

@Embeddable
public class Messa         


        
相关标签:
2条回答
  • 2021-01-19 03:24

    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.

    0 讨论(0)
  • 2021-01-19 03:28

    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.

    0 讨论(0)
提交回复
热议问题