Hibernate:使用 JPA 的单个实体中的两个序列
我有一个使用 JPA 注释的映射类,我希望它为两个字段使用两个单独的序列:
@Entity
@Table(name="my_table", schema="schema")
@SequenceGenerator(name="sequence",sequenceName="schema.local_key_sequence")
@SequenceGenerator(name="orderSequence",sequenceName="schema.message_received_sequence")
public class MyClass
{
@Id
@Column(name="unique_key",unique=true)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
private long id;
@Column(name="sequence_received")
@GeneratedValue( strategy = GenerationType.SEQUENCE,generator="orderSequence" )
private long sequenceReceived;
[...]
}
但是,JPA 禁止我在实体中使用 @SequenceGenerator
。我该怎么做呢。
注意:为了澄清起见,我希望从 local_key_sequence
的下一个值自动生成 ID,并从 message_received_sequence
自动生成 sequenceReceived。
I have a mapping class using JPA annotations, and I want it to use two separate sequences for two fields:
@Entity
@Table(name="my_table", schema="schema")
@SequenceGenerator(name="sequence",sequenceName="schema.local_key_sequence")
@SequenceGenerator(name="orderSequence",sequenceName="schema.message_received_sequence")
public class MyClass
{
@Id
@Column(name="unique_key",unique=true)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
private long id;
@Column(name="sequence_received")
@GeneratedValue( strategy = GenerationType.SEQUENCE,generator="orderSequence" )
private long sequenceReceived;
[...]
}
However, JPA forbids me from having to @SequenceGenerator
in the Entity. How should I do this.
Note: for clarification, I want the ID to be generated automatically from the next value of local_key_sequence
, and sequenceReceived to be generated autonatically from message_received_sequence
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能在主键上使用“由数据库值生成”。
然而,如果只有字段 sequenceReceived 也是 Id(复合 Id 的一部分),Hibernate(Hibernate Annotations 项目)就可以支持这一点。
You can use Generated by database value on Primary keys only.
However Hibernate (Hibernate Annotations project) can support this if only field sequenceReceived will be also Id (part of composite Id).