Java oracle 上的休眠序列,@GeneratedValue(strategy = GenerationType.AUTO)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3068692/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:11:36  来源:igfitidea点击:

Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO)

javaoraclehibernateorm

提问by Moli

I'm usign @GeneratedValue(strategy = GenerationType.AUTO) to generate the ID on my entity.

我使用 @GeneratedValue(strategy = GenerationType.AUTO) 在我的实体上生成 ID。

I don't now how it works, but on my child table, generates ID values, that follow the parent sequence.

我现在不知道它是如何工作的,但在我的子表上,生成遵循父序列的 ID 值。

//parent table
@Entity
@Table (name = "parent")
public class Parent {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column (name = "id")
    private long id;


    @OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
    @JoinColumn (name = "parentId")
    @ForeignKey (name = "FKparent")
    private List<child> child;

}

//child table
@Entity
@Table (name = "child")
public class Child {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column (name = "id")
    private long id;
}

The inserted ID values on parent, updates the sequence. The inserted ID values on child, updates the sequence. On the next insert of parent, the sequence... uses values updated by child insertions...

父级上插入的 ID 值更新序列。在 child 上插入的 ID 值更新序列。在父的下一次插入时,序列...使用由子插入更新的值...

This Annotations, aren't creating two sequences, only one. Is this correct/expected?

这个注解,不是创建两个序列,只有一个。这是正确的/预期的吗?

I inserted my entities with my DAO service only using entityManager.persist(parent);

我仅使用 DAO 服务插入了我的实体 entityManager.persist(parent);

回答by Jens Schauder

Yes this is correct and expected.

是的,这是正确的和预期的。

You can create individual sequences for each table, but IMHO that is just extra code with no actual benefit.

您可以为每个表创建单独的序列,但恕我直言,这只是额外的代码,没有实际好处。

回答by Pascal Thivent

These Annotations are no creating two sequences, only one. Is this correct/expected?

这些注释不是创建两个序列,只有一个。这是正确的/预期的吗?

That's the expected behavior. When using @GeneratedValue(strategy = GenerationType.AUTO), the JPA provider will pick an appropriate strategy for the particular database. In the case of Oracle, this will be SEQUENCE and, since you did not specify anything, Hibernate will use a single global sequence called hibernate_sequence.

这是预期的行为。使用 时@GeneratedValue(strategy = GenerationType.AUTO),JPA 提供程序将为特定数据库选择适当的策略。在 Oracle 的情况下,这将是 SEQUENCE,并且由于您没有指定任何内容,Hibernate 将使用一个名为hibernate_sequence.

Is this correct? Well, I don't know, it depends on your needs. Just in case, the default maximum value for an Oracle sequence is 1E+27, or 1,000,000,000,000,000,000,000,000,000. That's enough for many.

这样对吗?好吧,我不知道,这取决于您的需求。以防万一,Oracle 序列的默认最大值是 1E+27,或 1,000,000,000,000,000,000,000,000,000。这对很多人来说已经足够了。

Now, it is possible to use GenerationType.AUTOand still control the name of the sequence when the database uses sequences:

现在,GenerationType.AUTO当数据库使用序列时,可以使用并仍然控制序列的名称:

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private Long id;

回答by Elton Sandré

@Entity
@Table(name = "table_seq")
@SequenceGenerator(name= "NAME_SEQUENCE", sequenceName = "SEQ_ID", initialValue=1, allocationSize = 1)
public class SeqEntity implements Serializable {
  private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="NAME_SEQUENCE")
 private Long id;

}