java Hibernate ID 生成器使用注解“递增”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32629956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate ID generator "increment" by using annotation
提问by Ram
As per the Hibernate Developer guide 3.3 here, Hibernate provides support in many ways for generating Identifiers. But this is by using XML based mapping. [1] How to do the same by using Annotations?
Especially I'm interested in 'increment' type. The closest thing that i found is using @GeneratedValue(strategy=GenerationType.AUTO)
. But this is a JPA based strategy.
How can I use Hibernate based using Annotations?
And Even this information is not present in the Hibernate Developer Guide of version 4.3! Any particular reason for this?
根据此处的 Hibernate 开发人员指南 3.3 ,Hibernate 以多种方式提供了生成标识符的支持。但这是通过使用基于 XML 的映射。[1] 如何使用 Annotations 做同样的事情?
特别是我对“增量”类型感兴趣。我发现的最接近的东西是使用@GeneratedValue(strategy=GenerationType.AUTO)
. 但这是基于 JPA 的策略。
如何使用基于注释的 Hibernate?
甚至这些信息也没有出现在 4.3 版的 Hibernate 开发人员指南中!这有什么特别的原因吗?
UPDATE
I'm very well aware of the four strategies which is from JPA. I'm interested in the other types which Hibernate provides. Like hilo
, increment
, and so on. In documentation this is done by using XML configurations.Is there any way to use it with Annotations?
更新
我非常了解来自 JPA 的四种策略。我对 Hibernate 提供的其他类型感兴趣。像hilo
,,increment
等等。在文档中,这是通过使用 XML 配置完成的。有没有办法将它与注释一起使用?
回答by Gaurav
Hibernate implements JPA and uses the JPA id generation strategy.
Hibernate 实现了 JPA 并使用 JPA id 生成策略。
Check the documentation here for 4.3 : http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/: Section5.1.2.2. Identifier generator
在此处查看 4.3 的文档: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/:Section5.1.2.2。标识符生成器
Hibernate also provides @GenericGeneratorwhich can be used to configure Hibernate specific generator by passing in strategy attribute
Hibernate 还提供了@GenericGenerator,它可用于通过传入策略属性来配置 Hibernate 特定的生成器
回答by José Mendes
For Hibernate 4.x
you can find 4 types of Generation Types
因为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.
GeneratorType.AUTO - 这是默认策略,可跨不同数据库移植。Hibernate 根据数据库选择合适的 ID。
GeneratorType.IDENTITY - 此设置基于某些数据库提供的身份;提供唯一标识符是数据库的责任。
GeneratorType.SEQUENCE - 一些数据库提供了序列号的机制,所以这个设置会让 Hibernate 使用序列号。
GeneratorType.TABLE - 有时主键是从另一个表中的唯一列创建的。在这种情况下,请使用 TABLE 生成器。
With the Annotations:
随着 Annotations:
If the ID Generation Strategy is NOT SET
it means you are using the AUTO Strategy
.
如果 ID 生成策略是,NOT SET
则意味着您正在使用AUTO Strategy
.
To use the others, annotate like:
要使用其他人,请注释如下:
@Entity(name = "TBL_EMPLOYEE")
public class Employee {
@Id
@Column(name="ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int employeeId =0;
...
}
or
或者
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;
...
}
or
或者
public class Employee {
@Id
@Column(name="ID")
@GeneratedValue (strategy= GenerationType.TABLE, generator="empTableGen")
@TableGenerator(name = "empTableGen", table = "EMP_ID_TABLE")
private int empoyeeId =0;
...
}
You can also use Composite Identifiers
, in this case I recommend you go for the Book Just Hibernate
.
您也可以使用Composite Identifiers
,在这种情况下,我建议您使用 Book Just Hibernate
。