Skip to content

Commit e61c30e

Browse files
cigalybeikov
authored andcommitted
HHH-16781 - Test case showing the problem
1 parent aab1852 commit e61c30e

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package org.hibernate.orm.test.type;
2+
3+
import java.util.Comparator;
4+
import java.util.Locale;
5+
6+
import org.hibernate.annotations.JavaType;
7+
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
8+
import org.hibernate.persister.entity.EntityPersister;
9+
import org.hibernate.type.descriptor.WrapperOptions;
10+
import org.hibernate.type.descriptor.java.AbstractClassJavaType;
11+
import org.hibernate.type.descriptor.jdbc.JdbcType;
12+
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
13+
import org.hibernate.type.descriptor.jdbc.VarcharJdbcType;
14+
15+
import org.hibernate.testing.orm.junit.DomainModel;
16+
import org.hibernate.testing.orm.junit.JiraKey;
17+
import org.hibernate.testing.orm.junit.SessionFactory;
18+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
19+
import org.junit.jupiter.api.Test;
20+
21+
import jakarta.persistence.Entity;
22+
import jakarta.persistence.GeneratedValue;
23+
import jakarta.persistence.Id;
24+
import jakarta.persistence.Table;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
28+
29+
@DomainModel(annotatedClasses = { OverrideStandardJavaTypeTest.SampleEntity.class })
30+
@SessionFactory
31+
@JiraKey("HHH-16781")
32+
public class OverrideStandardJavaTypeTest {
33+
34+
@Test
35+
public void verifyMappings(SessionFactoryScope scope) {
36+
final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory()
37+
.getRuntimeMetamodels()
38+
.getMappingMetamodel();
39+
final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor( SampleEntity.class );
40+
41+
final var languageJavaTypeDescriptor = entityDescriptor.findAttributeMapping( "language" )
42+
.getSingleJdbcMapping().getJavaTypeDescriptor();
43+
assertInstanceOf( LocaleAsLanguageTagType.class, languageJavaTypeDescriptor );
44+
}
45+
46+
@Test
47+
public void validateJpa(SessionFactoryScope scope) {
48+
final var id = scope.fromTransaction(
49+
session -> {
50+
final var entity = new SampleEntity();
51+
entity.language = Locale.forLanguageTag( "en-Latn" );
52+
session.persist( entity );
53+
return entity.id;
54+
}
55+
);
56+
scope.inSession(
57+
session -> assertEquals(
58+
Locale.forLanguageTag( "en-Latn" ),
59+
session.find( SampleEntity.class, id ).language
60+
)
61+
);
62+
}
63+
64+
@Test
65+
public void validateNative(SessionFactoryScope scope) {
66+
final var id = scope.fromTransaction(
67+
session -> {
68+
final var entity = new SampleEntity();
69+
entity.language = Locale.forLanguageTag( "en-Latn" );
70+
session.persist( entity );
71+
return entity.id;
72+
}
73+
);
74+
scope.inSession(
75+
session ->
76+
assertEquals(
77+
"en-Latn",
78+
session.createNativeQuery(
79+
"select language from locale_as_language_tag where id=:id", String.class )
80+
.setParameter( "id", id )
81+
.getSingleResult()
82+
)
83+
);
84+
}
85+
86+
87+
@Entity
88+
@Table(name = "locale_as_language_tag")
89+
public static class SampleEntity {
90+
91+
@Id
92+
@GeneratedValue
93+
private Integer id;
94+
95+
@JavaType(LocaleAsLanguageTagType.class)
96+
private Locale language;
97+
}
98+
99+
public static class LocaleAsLanguageTagType extends AbstractClassJavaType<Locale> {
100+
101+
public static final LocaleAsLanguageTagType INSTANCE = new LocaleAsLanguageTagType();
102+
103+
public static class LocaleComparator implements Comparator<Locale> {
104+
105+
public static final LocaleComparator INSTANCE = new LocaleComparator();
106+
107+
@Override
108+
public int compare(Locale o1, Locale o2) {
109+
return o1.toString().compareTo( o2.toString() );
110+
}
111+
}
112+
113+
LocaleAsLanguageTagType() {
114+
super( Locale.class );
115+
}
116+
117+
@Override
118+
public Comparator<Locale> getComparator() {
119+
return LocaleComparator.INSTANCE;
120+
}
121+
122+
@Override
123+
public String toString(Locale value) {
124+
return value.toLanguageTag();
125+
}
126+
127+
private Locale fromString(String string) {
128+
return Locale.forLanguageTag( string );
129+
}
130+
131+
@Override
132+
@SuppressWarnings({ "unchecked" })
133+
public <X> X unwrap(Locale value, Class<X> type, WrapperOptions options) {
134+
if ( value == null ) {
135+
return null;
136+
}
137+
if ( String.class.isAssignableFrom( type ) ) {
138+
return (X) toString( value );
139+
}
140+
throw unknownUnwrap( type );
141+
}
142+
143+
@Override
144+
public <X> Locale wrap(X value, WrapperOptions options) {
145+
if ( value == null ) {
146+
return null;
147+
}
148+
if ( value instanceof String ) {
149+
return fromString( (String) value );
150+
}
151+
throw unknownWrap( value.getClass() );
152+
}
153+
154+
@Override
155+
public JdbcType getRecommendedJdbcType(final JdbcTypeIndicators indicators) {
156+
return VarcharJdbcType.INSTANCE;
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)