change default event timing for @Generated to event = INSERT

- this makes sense if @Generated con now be used on @Id properties
- but it's also convenient with @ColumnDefault
This commit is contained in:
Gavin 2022-12-20 00:17:56 +01:00 committed by Gavin King
parent be3621d8f8
commit 3ad3c5a509
10 changed files with 22 additions and 22 deletions

View File

@ -30,9 +30,9 @@ To mark an attribute as generated, applications used `@Generated`:
@Entity
public class Document {
...
@Generated(timing=INSERT)
private Date created;
@Generated
private Date created;
@Generated(event={INSERT,UPDATE})
private Date lastModified;
}
----

View File

@ -19,6 +19,8 @@ import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.Test;
import static org.hibernate.generator.EventType.INSERT;
import static org.hibernate.generator.EventType.UPDATE;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
@ -82,7 +84,7 @@ public class GeneratedTest {
private String middleName5;
@Generated
@Generated(event = {INSERT,UPDATE})
@Column(columnDefinition =
"AS CONCAT(" +
" COALESCE(firstName, ''), " +

View File

@ -20,8 +20,8 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* <ul>
* <li>{@link DynamicInsert @DynamicInsert}, to let the database fill in
* the value of a null entity attribute, or
* <li>{@link Generated @Generated(event=INSERT)}, to populate an entity
* attribute with the defaulted value of a database column.
* <li>{@link Generated @Generated}, to populate an entity attribute with
* the defaulted value of a database column.
* </ul>
* If {@code @Generated} is not used, a {@code default} value can cause state
* held in memory to lose synchronization with the database.

View File

@ -45,7 +45,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* </ul>
* In the second scenario, we may ask Hibernate to resynchronize the in-memory state
* with the database after each {@code insert} or {@code update} by annotating the
* persistent attribute {@link Generated @Generated(value=ALWAYS, writable=true)}.
* persistent attribute {@link Generated @Generated(event={INSERT,UPDATE}, writable=true)}.
* This results in a SQL {@code select} after every {@code insert} or {@code update}.
*
* @see ColumnTransformers

View File

@ -16,7 +16,6 @@ import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.hibernate.generator.EventType.INSERT;
import static org.hibernate.generator.EventType.UPDATE;
/**
* Specifies that the value of the annotated property is generated by the
@ -32,8 +31,7 @@ import static org.hibernate.generator.EventType.UPDATE;
* <ul>
* <li>a database table has a column value populated by a database trigger,
* <li>a mapped column has a default value defined in DDL, in which case
* {@code @Generated(event=INSERT)} is used in conjunction with
* {@link ColumnDefault},
* {@code @Generated} is used in conjunction with {@link ColumnDefault},
* <li>a {@linkplain #sql() SQL expression} is used to compute the value of
* a mapped column, or
* <li>when a custom SQL {@link SQLInsert insert} or {@link SQLUpdate update}
@ -73,7 +71,7 @@ public @interface Generated {
* executed.
* </ul>
*/
EventType[] event() default {INSERT, UPDATE};
EventType[] event() default INSERT;
/**
* Specifies the events that cause the value to be generated by the
@ -90,7 +88,7 @@ public @interface Generated {
* @deprecated use {@link #event()}
*/
@Deprecated(since = "6.2")
GenerationTime value() default GenerationTime.ALWAYS;
GenerationTime value() default GenerationTime.INSERT;
/**
* A SQL expression used to generate the value of the column mapped by

View File

@ -32,14 +32,14 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* A custom SQL insert statement might assign a value to a mapped column as it
* is written. In this case, the corresponding property of the entity remains
* unassigned after the insert is executed unless
* {@link Generated @Generated(event=INSERT)} is specified, forcing Hibernate
* to reread the state of the entity after each insert.
* {@link Generated @Generated} is specified, forcing Hibernate to reread the
* state of the entity after each insert.
* <p>
* Similarly, a custom insert statement might transform a mapped column value
* as it is written. In this case, the state of the entity held in memory
* loses synchronization with the database after the insert is executed unless
* {@link Generated @Generated(event=INSERT, writable=true)} is specified,
* again forcing Hibernate to reread the state of the entity after each insert.
* {@link Generated @Generated(writable=true)} is specified, again forcing
* Hibernate to reread the state of the entity after each insert.
*
* @author Laszlo Benke
*/

View File

@ -35,14 +35,14 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* A custom SQL update statement might assign a value to a mapped column as it
* is written. In this case, the corresponding property of the entity remains
* unassigned after the update is executed unless
* {@link Generated @Generated} is specified, forcing Hibernate to reread the
* state of the entity after each update.
* {@link Generated @Generated(event=UPDATE)} is specified, forcing Hibernate
* to reread the state of the entity after each update.
* <p>
* Similarly, a custom update statement might transform a mapped column value
* as it is written. In this case, the state of the entity held in memory
* loses synchronization with the database after the update is executed unless
* {@link Generated @Generated(writable=true)} is specified, again forcing
* Hibernate to reread the state of the entity after each update.
* {@link Generated @Generated(event=UPDATE, writable=true)} is specified, again
* forcing Hibernate to reread the state of the entity after each update.
*
* @author Laszlo Benke
*/

View File

@ -39,7 +39,7 @@ public class GeneratedGeneration implements InDatabaseGenerator {
}
public GeneratedGeneration(Generated annotation) {
eventTypes = annotation.value() == GenerationTime.ALWAYS
eventTypes = annotation.value() == GenerationTime.INSERT
? fromArray( annotation.event() )
: annotation.value().eventTypes();
sql = isEmpty( annotation.sql() ) ? null : new String[] { annotation.sql() };

View File

@ -49,7 +49,7 @@ import static org.hibernate.generator.internal.NaturalIdHelper.getNaturalIdPrope
* <pre>{@code
* @Entity @Table(name="TableWithPKAssignedByTrigger")
* public class TriggeredEntity {
* @Id @Generated(event = INSERT)
* @Id @Generated
* private Long id;
*
* @NaturalId

View File

@ -21,7 +21,7 @@ import static org.hibernate.generator.EventType.INSERT;
*/
@Entity @Table(name="my_entity")
public class MyEntity {
@Id @Generated(event = INSERT)
@Id @Generated
@ColumnDefault("-666") //workaround for h2 'before insert' triggers being crap
private Long id;