简体   繁体   English

在JPA中使用@ SequenceGenerator / @ GeneratedValue时-数据库触发器是否冗余/不必要?

[英]When using the @SequenceGenerator/@GeneratedValue in JPA - is a database trigger redundant/unnecessary?

I saw this post ( JPA Entity Lifecycle Events vs database trigger ), but it didn't ask quite as explicitly as I am here: 我看到了这篇文章( JPA实体生命周期事件与数据库触发器 ),但是它并没有像我在这里那样明确地要求:

Am I required to have a sequence AND a trigger for when I insert a row with a PK with a value of (null)? 当我插入带有(null)值的PK的行时,是否需要一个序列和一个触发器?

Or will JPA somehow interpret the sequence annotations as a signal to grab .nextVal? 还是JPA会以某种方式将序列注释解释为抢夺.nextVal的信号?

If you define a sequence on your primary key (@Id annotated field) and you map the sequence using the annotations @GeneratedValue and @SequenceGenerator, you can persist an entity with a null primary key. 如果您在主键(带有@Id注释的字段)上定义了序列,并使用注释@GeneratedValue和@SequenceGenerator映射了序列,则可以使用空主键持久化实体。 JPA will automatically call the sequence to get the next value (or get it from its cache). JPA将自动调用序列以获取下一个值(或从其缓存中获取)。 The primary key declaration should look like. 主键声明应该看起来像。

@Id
@Column(name = "TABLE_PK")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_GENERATOR_NAME")
@SequenceGenerator(name = "SEQUENCE_GENERATOR", sequenceName = "SEQUENCE_NAME")
private Integer id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 SequenceGenerator 和 GeneratedValue 的 JPA - JPA with SequenceGenerator and GeneratedValue JPA 2 @SequenceGenerator @GeneratedValue 产生唯一约束违规 - JPA 2 @SequenceGenerator @GeneratedValue producing unique constraint violation JPA 何时设置 @GeneratedValue @Id - When does the JPA set a @GeneratedValue @Id 有什么区别:使用 JPA @TableGenerator、@GeneratedValue 与数据库 Auto_Increment 的序列 ID - What are the difference between: sequence id using JPA @TableGenerator, @GeneratedValue vs database Auto_Increment 注释快捷方式(Hibernate 的@Id、@GeneratedValue、@SequenceGenerator) - Annotation shortcut (Hibernate's @Id, @GeneratedValue, @SequenceGenerator) MappedSuperclass - 更改子类中的 SequenceGenerator 而不在超类上使用 @GeneratedValue - MappedSuperclass - Change SequenceGenerator in Subclass without @GeneratedValue on the Superclass JPA / Hibernate + Postgres SequenceGenerator - JPA/Hibernate + Postgres SequenceGenerator JPA @GeneratedValue始终为null - JPA @GeneratedValue is allways null JPA,GeneratedValue自动增量为50 - JPA, GeneratedValue autoincrement with 50 有没有办法使用JPA注释和Hibernate动态选择@GeneratedValue策略? - Is there a way to dynamically choose a @GeneratedValue strategy using JPA annotations and Hibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM