Day01:自动创建时间_DataBase

@Entity(name = "Event")
public static class Event {

@Id
@GeneratedValue
private Long id;

@Column(name = "`timestamp`")
@FunctionCreationTimestamp
private Date timestamp;

public Event() {}

public Long getId() {
    return id;
}

public Date getTimestamp() {
    return timestamp;
}

}

@ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface FunctionCreationTimestamp {}

public static class FunctionCreationValueGeneration
implements AnnotationValueGeneration {

@Override
public void initialize(FunctionCreationTimestamp annotation, Class propertyType) {
}

/**
 * Generate value on INSERT
 * @return when to generate the value
 */
public GenerationTiming getGenerationTiming() {
    return GenerationTiming.INSERT;
}

/**
 * Returns null because the value is generated by the database.
 * @return null
 */
public ValueGenerator getValueGenerator() {
    return null;
}

/**
 * Returns true because the value is generated by the database.
 * @return true
 */
public boolean referenceColumnInSql() {
    return true;
}

/**
 * Returns the database-generated value
 * @return database-generated value
 */
public String getDatabaseGeneratedReferencedColumnValue() {
    return "current_timestamp";
}

}

你可能感兴趣的:(Day01:自动创建时间_DataBase)