====== Hibernate @ValueGenerationType ======
* [[java:hibernate|Hibernate]]에서 필드의 값을 자동으로 생성하는 기법
* [[https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/annotations/ValueGenerationType.html|ValueGenerationType (Hibernate JavaDocs)]] 는 메타 어노테이션으로 다른 어노테이션을 값을 자동생성하는 어노테이션으로 지정해준다.
* [[https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/tuple/AnnotationValueGeneration.html|AnnotationValueGeneration]] 를 구현하여 실제로 값을 생성해주고, 이렇게 생성된 값이 필드의 값으로 자동으로 들어간다.
* 이를 기반으로 하여 [[https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/tuple/CreationTimestampGeneration.html|CreationTimestampGeneration]] [[https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/tuple/UpdateTimestampGeneration.html|UpdateTimestampGeneration]] 등이 만들어졌다.
* see [[java:hibernate:generated|Hibernate @Generated]]
===== Example =====
@Entity(name = "Event")
public static class Event {
@Id
@GeneratedValue
private Long id;
@Column(name = "`timestamp`")
@FunctionCreationTimestamp
private Date timestamp;
//Constructors, getters, and setters are omitted for brevity
}
@ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface FunctionCreationTimestamp {}
// DB를 통해 자동생성 지원
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";
}
}
쿼리가 아래처럼 실행된다.
INSERT INTO Event ("timestamp", id)
VALUES (current_timestamp, 1)
// In memory 상에서 자동생성
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 the in-memory generated value
* @return {@code true}
*/
public ValueGenerator> getValueGenerator() {
return (session, owner) -> new Date( );
}
/**
* Returns false because the value is generated by the database.
* @return false
*/
public boolean referenceColumnInSql() {
return false;
}
/**
* Returns null because the value is generated in-memory.
* @return null
*/
public String getDatabaseGeneratedReferencedColumnValue() {
return null;
}
}
쿼리가 다음과 같이 날라간다.
INSERT INTO Event ("timestamp", id)
VALUES ('Tue Mar 01 10:58:18 EET 2016', 1)