Skip to content

Commit 1c9bd95

Browse files
committed
HHH-15331 Add test for issue
1 parent 1da8943 commit 1c9bd95

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.hibernate.orm.test.mapping.converted.converter;
2+
3+
import java.net.URI;
4+
5+
import org.hibernate.testing.TestForIssue;
6+
import org.hibernate.testing.orm.junit.DomainModel;
7+
import org.hibernate.testing.orm.junit.SessionFactory;
8+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
import jakarta.persistence.AttributeConverter;
13+
import jakarta.persistence.Convert;
14+
import jakarta.persistence.Converter;
15+
import jakarta.persistence.Entity;
16+
import jakarta.persistence.Id;
17+
import jakarta.persistence.TypedQuery;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
@DomainModel(
22+
annotatedClasses = { AttributeConvertAndDynamicInstantiationTest.TestEntity.class }
23+
)
24+
@SessionFactory
25+
@TestForIssue(jiraKey = "HHH-15331")
26+
public class AttributeConvertAndDynamicInstantiationTest {
27+
28+
@BeforeEach
29+
public void setUp(SessionFactoryScope scope) {
30+
scope.inTransaction(
31+
session -> {
32+
TestEntity entity = new TestEntity();
33+
entity.setLabel( "hibernate" );
34+
entity.setUri( URI.create( "https://hibernate.org/orm/" ) );
35+
session.persist( entity );
36+
}
37+
);
38+
}
39+
40+
@Test
41+
public void testDynamicInstantiation(SessionFactoryScope scope) {
42+
scope.inTransaction(
43+
session -> {
44+
final TypedQuery<TestEntity> eQuery = session.createQuery(
45+
"FROM TestEntity AS e",
46+
TestEntity.class
47+
);
48+
assertEquals( 1, eQuery.getResultList().size() );
49+
50+
final TypedQuery<TestEntityDTO> dtoQuery = session.createQuery(
51+
"SELECT new org.hibernate.orm.test.mapping.converted.converter.AttributeConvertAndDynamicInstantiationTest$TestEntityDTO(e.label , e.uri ) FROM TestEntity AS e",
52+
TestEntityDTO.class
53+
);
54+
assertEquals( 1, dtoQuery.getResultList().size() );
55+
}
56+
);
57+
}
58+
59+
@Entity(name = "TestEntity")
60+
public static class TestEntity {
61+
@Id
62+
private long id;
63+
64+
@Convert(converter = URIConverter.class)
65+
private URI uri;
66+
67+
private String label;
68+
69+
public String getLabel() {
70+
return label;
71+
}
72+
73+
public void setLabel(String label) {
74+
this.label = label;
75+
}
76+
77+
public URI getUri() {
78+
return uri;
79+
}
80+
81+
public void setUri(URI uri) {
82+
this.uri = uri;
83+
}
84+
85+
public long getId() {
86+
return id;
87+
}
88+
}
89+
90+
@Converter
91+
public static class URIConverter implements AttributeConverter<URI, String> {
92+
93+
@Override
94+
public String convertToDatabaseColumn(URI attribute) {
95+
if ( attribute != null ) {
96+
return attribute.toString();
97+
}
98+
return null;
99+
}
100+
101+
@Override
102+
public URI convertToEntityAttribute(String dbData) {
103+
if ( dbData != null ) {
104+
return URI.create( dbData );
105+
}
106+
return null;
107+
}
108+
109+
}
110+
111+
public static class TestEntityDTO {
112+
private String label;
113+
114+
private URI uri;
115+
116+
public TestEntityDTO(String label, URI uri) {
117+
this.label = label;
118+
this.uri = uri;
119+
}
120+
}
121+
122+
}

0 commit comments

Comments
 (0)