美文网首页
Day01:自动创建时间_DataBase

Day01:自动创建时间_DataBase

作者: 宇宙超人喵学长 | 来源:发表于2017-07-12 17:42 被阅读0次

    @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<FunctionCreationTimestamp> {

    @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

          本文链接:https://www.haomeiwen.com/subject/ckgbhxtx.html