开发者

Why @SequenceGenerator has a name?

The javadoc says

A unique generator name that can be referenced by one or more classes to be the generator for primary key value开发者_高级运维s

But its annotation that is put on top of @Entity, so I don't understand the reasoning.

Can I use in @Entity @SequenceGenerator defined in other @Entity?


The idea is that multiple entities might want to share a key space.

For example if you have two entities Foo and Bar you'd want to avoid having both a Foo with id 1 and a Bar with id 1. You'd do that by specifying the same name for their @SequenceGenerator annotations.


You when using generator in @GeneratedValue annotation refer it by name. This is why the name is used. Sequence generator could be declared inside GeneratedValue annotation, but they decided otherwise. It gives you the possibility to use same Generator on multiple fields, or even share them between entities. When you not use this feature, it is of course some overhead for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜