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

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

问题描述

我是美利基@GeneratedValue(strategy = GenerationType.AUTO)在我的实体上生成ID。

我现在不知道它是如何工作的,但是我的子表,生成ID值,它跟随着母顺序。

  //父表格
@Entity
@Table(name =parent)
public class Parent {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id)
私人长ID;


@OneToMany(cascade = {CascadeType.ALL},fetch = FetchType.LAZY)
@JoinColumn(name =parentId)
@ForeignKey =FKparent)
私人列表< child>儿童;


$ b //子表
@Entity
@Table(name =child)
public class Child {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id)
私人长ID;
}

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

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



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

解决方案


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


这是预期的行为。当使用 @GeneratedValue(strategy = GenerationType.AUTO)时,JPA提供者将为特定数据库选择合适的策略。就Oracle而言,这将是SEQUENCE,并且由于您没有指定任何内容,因此Hibernate将使用一个名为 hibernate_sequence 的全局序列。



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

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

  @Id 
@GeneratedValue(strategy = GenerationType.AUTO,generator =my_entity_seq_gen )
@SequenceGenerator(name =my_entity_seq_gen,sequenceName =MY_ENTITY_SEQ)
private长ID;


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

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

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?

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

解决方案

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.

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.

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;

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

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