使用注释的 Hibernate ID 生成器 "increment"

Hibernate ID generator "increment" by using annotation

根据 Hibernate 开发人员指南 3.3 here,Hibernate 以多种方式提供对生成标识符的支持。但这是通过使用基于 XML 的映射 。 [1] 如何使用注释来做同样的事情?
我尤其对 'increment' 类型感兴趣。我发现最接近的是使用 @GeneratedValue(strategy=GenerationType.AUTO)。但这是基于 JPA 的策略。
如何使用基于注释的 Hibernate?
甚至这些信息也没有出现在 4.3 版的 Hibernate 开发人员指南中!这有什么特别的原因吗?

更新
我非常了解来自 JPA 的四种策略。我对 Hibernate 提供的其他类型很感兴趣。如 hiloincrement 等。在文档中,这是通过使用 XML configurations.Is 完成的,有什么方法可以将它与注释一起使用吗?

Hibernate 实现 JPA 并使用 JPA id 生成策略。

在此处查看 4.3 的文档: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/:第 5.1.2.2 节。标识符生成器

Hibernate 还提供了@GenericGenerator,可用于通过传入策略属性来配置 Hibernate 特定的生成器

对于Hibernate 4.x你可以找到4种Generation Types

GeneratorType.AUTO - This is the default strategy and is portable across different databases. Hibernate chooses the appropriate ID based on the database.

GeneratorType.IDENTITY - This setting is based on the identity provided by some databases; it is the respon‐ sibility of the database to provide a unique identifier.

GeneratorType.SEQUENCE - Some databases provide a mechanism of sequenced numbers, so this setting will let Hibernate use the sequence number.

GeneratorType.TABLE - Sometimes the primary keys have been created from a unique column in another table. In this case, use the TABLE generator.

随着Annotations:

如果 ID 生成策略是 NOT SET,则表示您正在使用 AUTO Strategy

要使用其他的,注释如下:

@Entity(name = "TBL_EMPLOYEE")
public class Employee {
@Id
@Column(name="ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int employeeId =0;
...
}

public class Employee {
@Id
@Column(name="EMPLOYEE_ID")
@GeneratedValue (strategy= GenerationType.SEQUENCE, generator="empSeqGen")
@SequenceGenerator(name = "empSeqGen", sequenceName = "EMP_SEQ_GEN")
private int employeeId =0;
...
 }

public class Employee {
@Id
@Column(name="ID")
@GeneratedValue (strategy= GenerationType.TABLE, generator="empTableGen")
@TableGenerator(name = "empTableGen", table = "EMP_ID_TABLE")
private int empoyeeId =0;
...
}

您也可以使用 Composite Identifiers,在这种情况下我建议您使用 Just Hibernate.

这对我有用:

@Id @GeneratedValue(generator = "increment")
private int id;