开发者

Hibernate: two sequences within a single entity with JPA

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="orderSequen开发者_JAVA百科ce",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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜