扩展@kevin-bowersox 的答案。
Hibernate主键生成策略和具体生成器之间的关系,分别在org.hibernate.id.IdentifierGeneratorFactory
static {
GENERATORS.put("uuid", UUIDHexGenerator.class); // "deprecated" for new use
GENERATORS.put("hilo", TableHiLoGenerator.class); // removed in Hibernate 5
GENERATORS.put("assigned", Assigned.class);
GENERATORS.put("identity", IdentityGenerator.class);
GENERATORS.put("select", SelectGenerator.class);
GENERATORS.put("sequence", SequenceGenerator.class);
GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);
GENERATORS.put("increment", IncrementGenerator.class);
GENERATORS.put("foreign", ForeignGenerator.class);
GENERATORS.put("guid", GUIDGenerator.class);
GENERATORS.put("uuid.hex", UUIDHexGenerator.class); // uuid.hex is deprecated
GENERATORS.put("sequence-identity", SequenceIdentityGenerator.class);
}
在 Hibernate 4.3 中,我发现org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory
类还有 3 种策略:
register("uuid2", UUIDGenerator.class);
register("enhanced-sequence", SequenceStyleGenerator.class);
register("enhanced-table", TableGenerator.class);
以上十五种策略,加上native
,是 Hibernate 默认支持的十六代策略。
示例native
:
@GeneratedValue(generator = "nativeGenerator")
@GenericGenerator(name = "nativeGenerator", strategy = "native")