Skip to content

Commit 1adbcfa

Browse files
Vincent Bouthinonbeikov
authored andcommitted
HHH-19589 : Test demonstrating that @converter is ignored when @TypeRegistration is present
1 parent 2f1b47d commit 1adbcfa

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.converted.converter;
6+
7+
import java.util.BitSet;
8+
9+
import org.hibernate.annotations.TypeRegistration;
10+
import org.hibernate.orm.test.mapping.basic.bitset.BitSetHelper;
11+
import org.hibernate.orm.test.mapping.basic.bitset.BitSetUserType;
12+
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.JiraKey;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.junit.jupiter.api.Test;
18+
19+
import jakarta.persistence.AttributeConverter;
20+
import jakarta.persistence.Convert;
21+
import jakarta.persistence.Converter;
22+
import jakarta.persistence.Entity;
23+
import jakarta.persistence.Id;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* <pre>
29+
* The @Converter should take precedence over @TypeRegistration.
30+
* This test shows that this is not the case.
31+
*
32+
* To ensure that the @Converter is taken into account without @TypeRegistration, you just need to remove the @TypeRegistration.
33+
* </pre>
34+
*
35+
* @author Vincent Bouthinon
36+
*/
37+
@DomainModel(
38+
annotatedClasses = {
39+
ConverterOverrideTypeRegisttrationTest.SimpleEntity.class
40+
}
41+
)
42+
@SessionFactory
43+
@JiraKey(value = "HHH-19589")
44+
public class ConverterOverrideTypeRegisttrationTest {
45+
46+
@Test
47+
void test(SessionFactoryScope scope) {
48+
scope.inTransaction( session -> {
49+
final SimpleEntity object = new SimpleEntity( 77L );
50+
BitSet bitSet = new BitSet();
51+
bitSet.set( 0, true );
52+
object.setBitSet( bitSet );
53+
session.persist( object );
54+
session.flush();
55+
session.clear();
56+
SimpleEntity simpleEntity = session.find( SimpleEntity.class, object.id );
57+
assertThat( simpleEntity.getBitSet().get( 7 ) ).isTrue();
58+
} );
59+
}
60+
61+
62+
@Entity(name = "SimpleEntity")
63+
@TypeRegistration(basicClass = BitSet.class, userType = BitSetUserType.class) // Remove this annotation to test the use of @Converter
64+
public static class SimpleEntity {
65+
66+
@Id
67+
private Long id;
68+
@Convert(converter = BitSetConverter.class)
69+
private BitSet bitSet;
70+
71+
public SimpleEntity() {
72+
}
73+
74+
public SimpleEntity(Long id) {
75+
this.id = id;
76+
}
77+
78+
public BitSet getBitSet() {
79+
return bitSet;
80+
}
81+
82+
public void setBitSet(final BitSet bitSet) {
83+
this.bitSet = bitSet;
84+
}
85+
}
86+
87+
@Converter
88+
public static class BitSetConverter implements AttributeConverter<BitSet, String> {
89+
90+
@Override
91+
public String convertToDatabaseColumn(final BitSet attribute) {
92+
return BitSetHelper.bitSetToString( attribute );
93+
}
94+
95+
@Override
96+
public BitSet convertToEntityAttribute(final String dbData) {
97+
BitSet bitSet = new BitSet();
98+
bitSet.set( 7, true );
99+
return bitSet;
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)