Java 如何使用带有 Oracle 10g 方言的 Hibernate 使用 JPA 生成我的 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2384420/
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
How is my id being generated with JPA using Hibernate with the Oracle 10g dialect?
提问by JavaRocky
I have some code:
我有一些代码:
@Id
@SequenceGenerator(name = "SOMETHING_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOMETHING_SEQ")
@Column(name = "SOMETHING", nullable = false)
private Long id;
How is hibernate providing my id?
hibernate 如何提供我的 ID?
I see in my database there a single sequence named 'hibernate_sequence' and no other hibernate 'special tables'.
我在我的数据库中看到一个名为“hibernate_sequence”的序列,没有其他休眠“特殊表”。
采纳答案by Adeel Ansari
Actually, here your SOMETHING_SEQ
is the name of sequence you configured somewhere in your hibernate config. And hibernate_sequence
is the sequence name in the database. In configuration it would be looking something like below,
实际上,这里SOMETHING_SEQ
是您在休眠配置中某处配置的序列名称。并且hibernate_sequence
是在数据库中的序列名称。在配置中,它看起来像下面这样,
<sequence-generator name="SOMETHING_SEQ"
sequence-name="hibernate_sequence"
allocation-size="<any_number_value>"/>
You can completely skip this configuration by using annotation instead. Then your @SequenceGenerator
annotation would need to provide few more paramters. Below is the example.
您可以通过使用注释来完全跳过此配置。那么您的@SequenceGenerator
注释将需要提供更多参数。下面是示例。
@SequenceGenerator(name="SOMETHING_SEQ", sequenceName="hibernate_sequence", allocationSize=10)
For example multiple entity classes would do something like below,
例如,多个实体类将执行如下操作,
@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;
...
...
}
回答by Lombo
In Oracle you don't have the auto_increment type as in MySQL. So, to generate an auto_increment column you need to use a sequence.
在 Oracle 中,您没有 MySQL 中的 auto_increment 类型。因此,要生成 auto_increment 列,您需要使用序列。
This is an example of how you can achieve this.
这是您如何实现这一目标的示例。
create table test (id number, testdata varchar2(255));
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
create trigger test_trigger
before insert on test
for each row
begin
select test_seq.nextval into :new.id from dual;
end;
So you create a sequence and use a trigger before each row is inserted to add its id.
因此,您创建一个序列并在插入每一行之前使用触发器来添加其 id。
So hibernate must be doing something like this, or instead of using the trigger doing
所以 hibernate 必须做这样的事情,或者而不是使用触发器做
insert into test values(test_seq.nextval, 'no trigger needed!');
Note: Example taken from here
注意:示例取自此处
回答by kgrad
In order to name the sequence you have to set the sequenceName
in your @SequenceGenerator
annotation:
为了命名序列,您必须sequenceName
在@SequenceGenerator
注释中设置:
@GeneratedValue(name="gen", strategy = GeneratorType.SEQUENCE)
@SequenceGenerator(name="gen", sequenceName="Sequence_Name", allocationSize = 1)
@Id
public Long getId()
{
// ...
}
Of note, if you are using a pre-existing generator, your allocationSize
must match the allocation size of that generator.
值得注意的是,如果您使用的是预先存在的生成器,则您allocationSize
必须匹配该生成器的分配大小。
回答by Pascal Thivent
How is hibernate providing my id?
hibernate 如何提供我的 ID?
Well, you explicitly told the JPA engine to generate identifier automatically (with the @GeneratedValue
annotation) using a strategy of type SEQUENCE
indicating that a database sequenceshould be used to generate the identifier. In case you wonder, sequences are database specific objects(e.g. Oracle) that can be used to generate unique integers.
好吧,您明确地告诉 JPA 引擎@GeneratedValue
使用SEQUENCE
指示应使用数据库序列生成标识符的类型策略自动生成标识符(使用注释)。如果您想知道,序列是可用于生成唯一整数的数据库特定对象(例如 Oracle)。
I see in my database there a single sequence named 'hibernate_sequence'
我在我的数据库中看到一个名为“hibernate_sequence”的序列
You didn't use the sequenceName
annotation element in your @SequenceGenerator
to specify the name of the database sequence object to use so Hibernate created a default sequence object during schema generation (which defaults to hibernate_sequence
). To specify a sequence, do it like this:
您没有使用sequenceName
annotation 元素@SequenceGenerator
来指定要使用的数据库序列对象的名称,因此 Hibernate 在模式生成期间创建了一个默认序列对象(默认为hibernate_sequence
)。要指定序列,请执行以下操作:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_entity_seq_gen")
@SequenceGenerator(name = "my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private Long id;