Skip to content

Commit e4f5926

Browse files
dreab8beikov
authored andcommitted
HHH-17687 Add test for issue
1 parent 24d25ce commit e4f5926

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.hibernate.orm.test.mapping.converted.converter;
2+
3+
import org.hibernate.testing.orm.junit.DomainModel;
4+
import org.hibernate.testing.orm.junit.JiraKey;
5+
import org.hibernate.testing.orm.junit.SessionFactory;
6+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
7+
import org.junit.jupiter.api.Test;
8+
9+
import jakarta.persistence.AttributeConverter;
10+
import jakarta.persistence.Convert;
11+
import jakarta.persistence.Entity;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.Id;
14+
import jakarta.persistence.Query;
15+
16+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
17+
18+
@DomainModel(
19+
annotatedClasses = {
20+
AttributeConverterAndNullTest.TestEntity.class
21+
}
22+
)
23+
@SessionFactory
24+
@JiraKey("HHH-17687")
25+
public class AttributeConverterAndNullTest {
26+
27+
@Test
28+
public void testSelectByConnvertedField(SessionFactoryScope scope) {
29+
TheField theField = new TheField( "field1" );
30+
TestEntity testEntity = new TestEntity( theField );
31+
scope.inTransaction(
32+
session -> {
33+
session.persist( testEntity );
34+
}
35+
);
36+
37+
scope.inSession(
38+
session -> {
39+
Query query1 = session.createQuery( "FROM TestEntity WHERE myField = :myField" );
40+
query1.setParameter( "myField", theField );
41+
System.out.println( query1.getSingleResult() );
42+
}
43+
);
44+
45+
TestEntity testEntity2 = new TestEntity( null );
46+
scope.inTransaction(
47+
session ->
48+
session.persist( testEntity2 )
49+
);
50+
51+
scope.inSession(
52+
session -> {
53+
Query query2 = session.createQuery( "FROM TestEntity WHERE myField = :myField" );
54+
query2.setParameter( "myField", null );
55+
assertThat( query2.getResultList().size() ).isEqualTo( 1 );
56+
}
57+
);
58+
}
59+
60+
public static class MyFieldConverter implements AttributeConverter<TheField, String> {
61+
62+
@Override
63+
public String convertToDatabaseColumn(TheField myField) {
64+
if ( myField == null ) {
65+
return "null";
66+
}
67+
return myField.getaField();
68+
}
69+
70+
@Override
71+
public TheField convertToEntityAttribute(String dbValue) {
72+
return new TheField( dbValue );
73+
}
74+
}
75+
76+
@Entity(name = "TestEntity")
77+
public static class TestEntity {
78+
79+
@Id
80+
@GeneratedValue
81+
private Long id;
82+
83+
@Convert(converter = MyFieldConverter.class)
84+
private TheField myField;
85+
86+
public TestEntity() {
87+
}
88+
89+
public TestEntity(TheField myField) {
90+
this.myField = myField;
91+
}
92+
93+
public Long getId() {
94+
return id;
95+
}
96+
97+
public TheField getMyField() {
98+
return myField;
99+
}
100+
}
101+
102+
103+
public static class TheField {
104+
private String aField;
105+
106+
public TheField() {
107+
}
108+
109+
public TheField(String field1) {
110+
this.aField = field1;
111+
}
112+
113+
public String getaField() {
114+
return aField;
115+
}
116+
117+
}
118+
119+
}

0 commit comments

Comments
 (0)