HHH-13728 JPQL for Enum with converter
Added new test which shows that JPQL with enum with custom converter fails to build correct query.
This commit is contained in:
parent
0c0248d448
commit
3948ef42c4
|
@ -19,6 +19,7 @@ import javax.persistence.Table;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -31,6 +32,7 @@ import static org.junit.Assert.assertNull;
|
||||||
* Test AttributeConverter functioning in various Query scenarios.
|
* Test AttributeConverter functioning in various Query scenarios.
|
||||||
*
|
*
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
|
* @author Ľubomír Varga
|
||||||
*/
|
*/
|
||||||
public class QueryTest extends BaseNonConfigCoreFunctionalTestCase {
|
public class QueryTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
|
|
||||||
|
@ -46,6 +48,17 @@ public class QueryTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue("HHH-13728") // https://hibernate.atlassian.net/browse/HHH-13728
|
||||||
|
public void testJpqlEnumWithConverterLiteral() {
|
||||||
|
Session session = openSession();
|
||||||
|
session.getTransaction().begin();
|
||||||
|
Employee jDoe = (Employee) session.createQuery( "from Employee e where e.contractType = org.hibernate.test.converter.QueryTest.ContractType.INTERNAL" ).uniqueResult();
|
||||||
|
assertNotNull( jDoe );
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJpqlBooleanLiteral() {
|
public void testJpqlBooleanLiteral() {
|
||||||
Session session = openSession();
|
Session session = openSession();
|
||||||
|
@ -89,6 +102,8 @@ public class QueryTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
public Float salary;
|
public Float salary;
|
||||||
@Convert( converter = BooleanConverter.class )
|
@Convert( converter = BooleanConverter.class )
|
||||||
public boolean active = true;
|
public boolean active = true;
|
||||||
|
@Column(length = 1)
|
||||||
|
public ContractType contractType = ContractType.INTERNAL;
|
||||||
|
|
||||||
public Employee() {
|
public Employee() {
|
||||||
}
|
}
|
||||||
|
@ -118,6 +133,43 @@ public class QueryTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum ContractType {
|
||||||
|
EXTERNAL_COMPANY('E'),
|
||||||
|
INTERNAL('I'),
|
||||||
|
CONTRACTOR('C'),
|
||||||
|
UNKNOWN('?');
|
||||||
|
|
||||||
|
private final Character code;
|
||||||
|
|
||||||
|
ContractType(Character code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ContractType[] contractTypes = new ContractType[255];
|
||||||
|
|
||||||
|
static {
|
||||||
|
for (int i = 0; i < 255; i++) {
|
||||||
|
contractTypes[i] = UNKNOWN;
|
||||||
|
}
|
||||||
|
contractTypes['E'] = EXTERNAL_COMPANY;
|
||||||
|
contractTypes['I'] = INTERNAL;
|
||||||
|
contractTypes['C'] = CONTRACTOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ContractType fromCode(Character charCode) {
|
||||||
|
ContractType result = contractTypes[charCode];
|
||||||
|
if (result == UNKNOWN) {
|
||||||
|
throw new RuntimeException("I am not able to convert \"" + charCode + "\" to ContractType! Check app version.");
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Character getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Converter( autoApply = true )
|
@Converter( autoApply = true )
|
||||||
public static class SalaryConverter implements AttributeConverter<Float,Long> {
|
public static class SalaryConverter implements AttributeConverter<Float,Long> {
|
||||||
@Override
|
@Override
|
||||||
|
@ -141,6 +193,22 @@ public class QueryTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Converter( autoApply = true )
|
||||||
|
public static class ContractTypeConverter implements AttributeConverter<ContractType,Character> {
|
||||||
|
public static final ContractType CONTRACTTYPE_FOR_NULL_CHARACTER = ContractType.UNKNOWN;
|
||||||
|
public static final Character CHARACTER_FOR_NULL_CONTRACTTYPE = CONTRACTTYPE_FOR_NULL_CHARACTER.getCode();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Character convertToDatabaseColumn(ContractType contractType) {
|
||||||
|
return contractType == null ? CHARACTER_FOR_NULL_CONTRACTTYPE : contractType.getCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ContractType convertToEntityAttribute(Character charCode) {
|
||||||
|
return charCode == null ? CONTRACTTYPE_FOR_NULL_CHARACTER : ContractType.fromCode(charCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class BooleanConverter implements AttributeConverter<Boolean,Integer> {
|
public static class BooleanConverter implements AttributeConverter<Boolean,Integer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue