oracle 上的休眠序列,@GeneratedValue(strategy = GenerationType.AUTO) [英] Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO)

查看:76
本文介绍了oracle 上的休眠序列,@GeneratedValue(strategy = GenerationType.AUTO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

//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;
}

在父级上插入的 ID 值更新序列.在 child 上插入的 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...

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

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

我仅使用 entityManager.persist(parent);

推荐答案

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

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

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

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.

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

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.

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

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

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

这篇关于oracle 上的休眠序列,@GeneratedValue(strategy = GenerationType.AUTO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆