HHH-15742 Add test for issue

This commit is contained in:
Marco Belladelli 2022-12-02 15:52:06 +01:00 committed by Christian Beikov
parent d91e878c66
commit cdfbc58bb9
1 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,127 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.mapping.converted.converter;
import java.time.Year;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Converter;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.TypedQuery;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Marco Belladelli
*/
@TestForIssue( jiraKey = "HHH-15742")
public class QueryConvertedAttributeTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
EntityA.class,
};
}
@Before
public void prepare() {
inTransaction( s -> {
EntityA entityA1 = new EntityA( 1, Year.parse("2022") );
EntityA entityA2 = new EntityA( 2, Year.parse("2021") );
EntityA entityA3 = new EntityA( 3, null );
s.persist( entityA1 );
s.persist( entityA2 );
s.persist( entityA3 );
} );
}
@After
public void tearDown() {
inTransaction( s -> {
s.createMutationQuery( "delete entitya" ).executeUpdate();
} );
}
@Test
public void testQueryConvertedAttribute() {
inTransaction( s -> {
TypedQuery<EntityA> query = s.createQuery( "from entitya" , EntityA.class);
List<EntityA> resultList = query.getResultList();
assertEquals(3, resultList.size());
} );
inTransaction( s -> {
TypedQuery<EntityA> query = s.createQuery(
"from entitya a where :year is null or a.year = :year",
EntityA.class
);
query.setParameter( "year", Year.parse( "2022" ) );
List<EntityA> resultList = query.getResultList();
assertEquals(1, resultList.size());
assertEquals(1, resultList.get( 0 ).getId());
assertEquals("2022", resultList.get( 0 ).getYear().toString());
} );
}
@Entity(name = "entitya")
public static class EntityA {
@Id
private Integer id;
@Column(name = "year_attribute")
@Convert(converter = YearConverter.class)
private Year year;
public EntityA() {
}
public EntityA(Integer id, Year year) {
this.id = id;
this.year = year;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
}
@Converter
public static class YearConverter implements AttributeConverter<Year, String> {
@Override
public String convertToDatabaseColumn(Year attribute) {
return attribute != null ? attribute.toString() : null;
}
@Override
public Year convertToEntityAttribute(String dbData) {
return dbData != null ? Year.parse( dbData ) : null;
}
}
}