如何使用 Hibernate 和 postgresql 为每个表实现一对多关系的自动递增 id 字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17020544/
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
how to implement auto increment id field per table with Hibernate and postgresql for one to many relationships
提问by Capitaine
I have 4 entities one-to-many Message entity:
我有 4 个实体一对多消息实体:
@Entity
@Table(name = "Messages")
public class Message {
...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messagesounds_id")
private Sound sound;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messagevibrations_id")
private Vibration vibration;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messagecolors_id")
private Color color;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messageplatforms_id")
private Platform platform;
...
}
whereas other 4 entities look like this:
而其他 4 个实体如下所示:
@Entity
@Table( name = "MessageSounds" , uniqueConstraints=@UniqueConstraint(columnNames = {"name"}))
public class Sound {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="messagesounds_id")
private Set<Message> message;
...
}
I can successfully create the first message record referenced to those first records of the 4 referenced tables. But the message table in postgresql db looks like this:
我可以成功创建引用到 4 个引用表的第一条记录的第一条消息记录。但是 postgresql db 中的消息表是这样的:
id |...| messagesounds_id | messagevibrations_id | messagecolors_id | messageplatforms_id |
1 |...| 2 | 3 | 4 | 5 |
I want to let every primary key of the 4 referenced tables be auto incremented, so that the first record of message table should look like this:
我想让 4 个引用表的每个主键自动递增,以便消息表的第一条记录应如下所示:
id |...| messagesounds_id | messagevibrations_id | messagecolors_id | messageplatforms_id |
1 |...| 1 | 1 | 1 | 1 |
How to achieve this using annotations?
如何使用注释来实现这一点?
回答by Capitaine
Ok, from Hibernate use of PostgreSQL sequence does not affect sequence tablecited by a_horse_with_no_nameless,
好的,从Hibernate 使用 PostgreSQL 序列不会影响a_horse_with_no_nameless引用的序列表,
I managed to set the auto incremented primary keys of every referenced tables like these:
我设法设置每个引用表的自动递增主键,如下所示:
@Id
@SequenceGenerator(name="pk_sequence",sequenceName="messagesounds_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="pk_sequence")
private int id;
Thanks a lot for all the research.
非常感谢所有的研究。
回答by Ayaz Ali Khatri
@Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; and you leave it null (0) when persisting. (null if you use the Integer / Long wrappers)
@Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; 并且在坚持时将其保留为 null (0)。(如果您使用 Integer / Long 包装器,则为 null)
In some cases the AUTO strategy is resoved to SEQUENCE rathen than to IDENTITY or TABLE, so you might want to manually set it to IDENTITY or TABLE (depending on the underlying database).
在某些情况下,AUTO 策略被解析为 SEQUENCE 而不是 IDENTITY 或 TABLE,因此您可能需要手动将其设置为 IDENTITY 或 TABLE(取决于底层数据库)。
It seems SEQUENCE + specifying the sequence name worked for you.
似乎 SEQUENCE + 指定序列名称对您有用。