我有一些代码:
@Id @SequenceGenerator(name = "SOMETHING_SEQ") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOMETHING_SEQ") @Column(name = "SOMETHING", nullable = false) private Long id;
hibernate状态如何提供我的ID?
我在数据库中看到一个名为“ hibernate_sequence”的序列,没有其他hibernate的“特殊表”。
实际上,这SOMETHING_SEQ是您在hibernate配置中某处配置的序列的名称。并且hibernate_sequence是数据库中的序列名称。在配置中,其外观如下图所示,
SOMETHING_SEQ
hibernate_sequence
<sequence-generator name="SOMETHING_SEQ" sequence-name="hibernate_sequence" allocation-size="<any_number_value>"/>
您可以改为使用注释来完全跳过此配置。然后,您的@SequenceGenerator注释将需要提供更多的参数。以下是示例。
@SequenceGenerator
@SequenceGenerator(name="SOMETHING_SEQ", sequenceName="hibernate_sequence", allocationSize=10)
例如,多个实体类将执行以下操作,
@Entity public class Entity1 { @Id @SequenceGenerator(name = "entity1Seq", sequenceName="ENTITY1_SEQ", allocationSize=1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entity1Seq") @Column(name = "ID", nullable = false) private Long id; ... ... } @Entity public class Entity2 { @Id @SequenceGenerator(name = "entity2Seq", sequenceName="ENTITY2_SEQ", allocationSize=10) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entity2Seq") @Column(name = "ID", nullable = false) private Long id; ... ... }