如何使用JPA/Hibernate设置复合主键的列顺序 [英] How to set the column order of a composite primary key using JPA/Hibernate

查看:451
本文介绍了如何使用JPA/Hibernate设置复合主键的列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在复合主键中的列排序遇到麻烦.我有一个包含以下内容的表:

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 MessageInfo implements Serializable {

    private byte loc;
    private long epochtime;

    @Column(name = "loc")
    public byte getLoc() {
        return loc;
    }    

    @Column(name = "epochtime")
    public long getEpochtime() {
        return epochtime;
    }
}

它在此映射中使用:

@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class AbstractMessage implements Message {

    private MessageInfo info;
    private int blah;

    @EmbeddedId
    public MessageInfo getInfo() {
        return info;
    }
}

当我使用具体的@Table类将AbstractMessage子类化时,hibernate创建的数据库和表没有错误.问题在于,hibernate会以我想要的相反顺序生成带有列的复合主键.

When I subclass AbstractMessage with a concrete @Table class hibernate creates the database and table with no errors. The problem is that hibernate is generating the composite primary key with the columns in the reverse order of what I would like.

CREATE TABLE  `mydb`.`concrete_table` (
  `epochtime` bigint(20) NOT NULL,
  `loc` tinyint(4) NOT NULL,
  `blah` smallint(6) DEFAULT NULL,
  `foo` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`epochtime`,`loc`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我希望主键为

PRIMARY KEY (`loc`,`epochtime`)

因为我知道我最多将有10个位置,但是每个位置都有很多时间.

Since I know that I will have a maximum of 10 loc's, but many epochtimes for each loc.

任何帮助将不胜感激=)

Any help would be appreciated =)

推荐答案

我真的不认为有办法做到这一点.我所能做的就是建议您使用已有的SQL create语句(将其更改为正确的顺序)并在生产中手动运行它.

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.

在测试中让Hibernate做它的事.

In tests let Hibernate do its thing.

这篇关于如何使用JPA/Hibernate设置复合主键的列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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