사용자 도구

사이트 도구


java:hibernate:valuegenerationtype

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:hibernate:valuegenerationtype [2021/10/07 21:57]
kwon37xi
java:hibernate:valuegenerationtype [2021/10/07 22:04] (현재)
kwon37xi [Example]
줄 5: 줄 5:
   * 이를 기반으로 하여 [[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]] 등이 만들어졌다.   * 이를 기반으로 하여 [[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]]   * see [[java:hibernate:generated|Hibernate @Generated]]
 +
 +===== Example =====
 +<code java>
 +@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<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";
 + }
 +}
 +</code>
 +쿼리가 아래처럼 실행된다.
 +<code sql>
 +INSERT INTO Event ("timestamp", id)
 +VALUES (current_timestamp, 1)
 +</code>
 +
 +<code java>
 +// In memory 상에서 자동생성
 +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 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;
 + }
 +}
 +</code>
 +쿼리가 다음과 같이 날라간다.
 +<code sql>
 +INSERT INTO Event ("timestamp", id)
 +VALUES ('Tue Mar 01 10:58:18 EET 2016', 1)
 +</code>
java/hibernate/valuegenerationtype.1633611465.txt.gz · 마지막으로 수정됨: 2021/10/07 21:57 저자 kwon37xi