转载

使用Hibernate和Oracle 10g方言,如何用JPA生成我的id?

我有一些代码:

@Id
@SequenceGenerator(name = "SOMETHING_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOMETHING_SEQ")
@Column(name = "SOMETHING", nullable = false)
private Long id;

hibernate如何提供我的身份?

我在我的数据库中看到一个名为’hibernate_sequence’的序列,没有其他hibernate’特殊表’.

实际上,这里你的SOMETHING_SEQ是你在hibernate配置中某处配置的序列的名称.而hibernate_sequence是数据库中的序列名称.在配置中,它看起来像下面,

<sequence-generator name="SOMETHING_SEQ" 
    sequence-name="hibernate_sequence"
    allocation-size="<any_number_value>"/>

您可以使用注释完全跳过此配置.那么你的@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;

  ...
  ...

}

翻译自:https://stackoverflow.com/questions/2384420/how-is-my-id-being-generated-with-jpa-using-hibernate-with-the-oracle-10g-dialec

原文  https://codeday.me/bug/20190113/527447.html
正文到此结束
Loading...