Skip to content

Commit 6017ae4

Browse files
committed
HHH-18564 Add test for issue
1 parent 3c8214f commit 6017ae4

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.converted.converter.object;
6+
7+
import org.hibernate.testing.orm.junit.DomainModel;
8+
import org.hibernate.testing.orm.junit.Jira;
9+
import org.hibernate.testing.orm.junit.SessionFactory;
10+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
11+
import org.junit.jupiter.api.AfterAll;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.Test;
14+
15+
import jakarta.persistence.AttributeConverter;
16+
import jakarta.persistence.Convert;
17+
import jakarta.persistence.Converter;
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.GeneratedValue;
20+
import jakarta.persistence.Id;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* @author Marco Belladelli
26+
*/
27+
@DomainModel( annotatedClasses = {
28+
ConvertedClassAttributeTest.EntityWithStatus.class,
29+
Status.class,
30+
} )
31+
@SessionFactory
32+
@Jira( "https://hibernate.atlassian.net/browse/HHH-18564" )
33+
public class ConvertedClassAttributeTest {
34+
@Test
35+
public void testConvertedAttributeSelection(SessionFactoryScope scope) {
36+
scope.inTransaction( session -> {
37+
final var resultList = session.createQuery(
38+
"select t.status from EntityWithStatus t",
39+
Status.class
40+
).getResultList();
41+
assertThat( resultList ).hasSize( 2 ).containsExactlyInAnyOrder( Status.ONE, Status.TWO );
42+
} );
43+
}
44+
45+
@Test
46+
public void testLiteralPredicateDomainForm(SessionFactoryScope scope) {
47+
scope.inTransaction( session -> {
48+
var result = session.createQuery(
49+
String.format( "select t from EntityWithStatus t where t.status > %s.ONE", Status.class.getName() ),
50+
EntityWithStatus.class
51+
).getSingleResult();
52+
assertThat( result.getStatus().getValue() ).isEqualTo( 2 );
53+
} );
54+
}
55+
56+
@Test
57+
public void testLiteralPredicateRelationalForm(SessionFactoryScope scope) {
58+
scope.inTransaction( session -> {
59+
var result = session.createQuery(
60+
"select t from EntityWithStatus t where t.status > 1",
61+
EntityWithStatus.class
62+
).getSingleResult();
63+
assertThat( result.getStatus().getValue() ).isEqualTo( 2 );
64+
} );
65+
}
66+
67+
@BeforeAll
68+
public void setUp(SessionFactoryScope scope) {
69+
scope.inTransaction( session -> {
70+
final EntityWithStatus entity1 = new EntityWithStatus();
71+
entity1.setStatus( Status.ONE );
72+
session.persist( entity1 );
73+
final EntityWithStatus entity2 = new EntityWithStatus();
74+
entity2.setStatus( Status.TWO );
75+
session.persist( entity2 );
76+
} );
77+
}
78+
79+
@AfterAll
80+
public void tearDown(SessionFactoryScope scope) {
81+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
82+
}
83+
84+
@Entity( name = "EntityWithStatus" )
85+
static class EntityWithStatus {
86+
@Id
87+
@GeneratedValue
88+
private Long id;
89+
90+
@Convert( converter = StatusConverter.class )
91+
private Status status;
92+
93+
public Long getId() {
94+
return id;
95+
}
96+
97+
public Status getStatus() {
98+
return status;
99+
}
100+
101+
public void setStatus(Status status) {
102+
this.status = status;
103+
}
104+
}
105+
106+
@Converter
107+
static class StatusConverter implements AttributeConverter<Status, Integer> {
108+
@Override
109+
public Integer convertToDatabaseColumn(Status attribute) {
110+
return attribute == null ? null : attribute.getValue();
111+
}
112+
113+
@Override
114+
public Status convertToEntityAttribute(Integer dbData) {
115+
return dbData == null ? null : Status.from( dbData );
116+
}
117+
}
118+
119+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.converted.converter.object;
6+
7+
import org.hibernate.annotations.Imported;
8+
9+
/**
10+
* @author Marco Belladelli
11+
*/
12+
@Imported
13+
public class Status {
14+
public static Status ONE = new Status( 1 );
15+
public static Status TWO = new Status( 2 );
16+
17+
private final int value;
18+
19+
public Status(int value) {
20+
this.value = value;
21+
}
22+
23+
public int getValue() {
24+
return value;
25+
}
26+
27+
public static Status from(int value) {
28+
return new Status( value );
29+
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if ( this == o ) {
34+
return true;
35+
}
36+
if ( o == null || getClass() != o.getClass() ) {
37+
return false;
38+
}
39+
40+
final Status status = (Status) o;
41+
return value == status.value;
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return value;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "Status{" + "value=" + value + '}';
52+
}
53+
}

0 commit comments

Comments
 (0)