[英]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.