1212import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
1313import org .hibernate .testing .orm .junit .Jpa ;
1414import org .hibernate .type .descriptor .WrapperOptions ;
15+ import org .hibernate .usertype .AnnotationBasedUserType ;
1516import org .hibernate .usertype .UserType ;
1617import org .junit .jupiter .api .Test ;
1718
3637import static org .junit .jupiter .api .Assertions .assertEquals ;
3738
3839@ Jpa (annotatedClasses = {MetaUserTypeTest .Thing .class , MetaUserTypeTest .SecondThing .class ,
39- MetaUserTypeTest .ThirdThing .class , MetaUserTypeTest .Things .class })
40+ MetaUserTypeTest .ThirdThing .class , MetaUserTypeTest .FourthThing . class , MetaUserTypeTest . Things .class })
4041public class MetaUserTypeTest {
4142
4243 @ Test void test (EntityManagerFactoryScope scope ) {
@@ -75,6 +76,18 @@ public class MetaUserTypeTest {
7576 assertEquals ( Period .of ( 1 , 2 , 3 ), thing .period );
7677 assertEquals ( Period .ofDays ( 42 ), thing .days );
7778 } );
79+
80+ scope .inTransaction ( em -> {
81+ FourthThing thing = new FourthThing ();
82+ thing .period = Period .of ( 1 , 2 , 3 );
83+ thing .days = Period .ofDays ( 42 );
84+ em .persist ( thing );
85+ } );
86+ scope .inTransaction ( em -> {
87+ FourthThing thing = em .find ( FourthThing .class , 1 );
88+ assertEquals ( Period .of ( 1 , 2 , 3 ), thing .period );
89+ assertEquals ( Period .ofDays ( 42 ), thing .days );
90+ } );
7891 }
7992
8093 @ Test void testCollection (EntityManagerFactoryScope scope ) {
@@ -118,6 +131,15 @@ public class MetaUserTypeTest {
118131 Period days ;
119132 }
120133
134+ @ Entity static class FourthThing {
135+ @ Id @ GeneratedValue
136+ long id ;
137+ @ FourthTimePeriod
138+ Period period ;
139+ @ FourthTimePeriod (days = true )
140+ Period days ;
141+ }
142+
121143 @ Entity static class Things {
122144 @ Id @ GeneratedValue
123145 long id ;
@@ -148,6 +170,13 @@ public class MetaUserTypeTest {
148170 boolean days () default false ;
149171 }
150172
173+ @ Type (FourthPeriodType .class )
174+ @ Target ({METHOD , FIELD })
175+ @ Retention (RUNTIME )
176+ public @interface FourthTimePeriod {
177+ boolean days () default false ;
178+ }
179+
151180 static class PeriodType extends AbstractPeriodType {
152181
153182 PeriodType (TimePeriod timePeriod ) {
@@ -169,14 +198,31 @@ static class ThirdPeriodType extends AbstractPeriodType {
169198 ThirdPeriodType (ThirdTimePeriod timePeriod , Member member ) {
170199 super (timePeriod .days ());
171200 if ( !timePeriod .equals ( ( (Field ) member ).getAnnotation ( ThirdTimePeriod .class ) )) {
201+ // only for validation
172202 throw new IllegalArgumentException (member + " should be annotated with " + timePeriod );
173203 }
174204 }
175205
176206 }
177207
208+ static class FourthPeriodType extends AbstractPeriodType implements AnnotationBasedUserType <FourthTimePeriod , Period > {
209+
210+ FourthPeriodType () {
211+ super (false );
212+ }
213+
214+ @ Override
215+ public void initialize (FourthTimePeriod timePeriod , Member member ) {
216+ days = timePeriod .days ();
217+ if ( !timePeriod .equals ( ( (Field ) member ).getAnnotation ( FourthTimePeriod .class ) )) {
218+ // only for validation
219+ throw new IllegalArgumentException (member + " should be annotated with " + timePeriod );
220+ }
221+ }
222+ }
223+
178224 static abstract class AbstractPeriodType implements UserType <Period > {
179- private final boolean days ;
225+ boolean days ;
180226
181227 AbstractPeriodType (boolean days ) {
182228 this .days = days ;
0 commit comments