|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.hql; |
| 8 | + |
| 9 | +import javax.persistence.Entity; |
| 10 | +import javax.persistence.GeneratedValue; |
| 11 | +import javax.persistence.Id; |
| 12 | +import javax.persistence.Table; |
| 13 | + |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import org.hibernate.Session; |
| 17 | +import org.hibernate.hql.internal.ast.QuerySyntaxException; |
| 18 | +import org.hibernate.testing.TestForIssue; |
| 19 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertNull; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | +import static org.junit.Assert.fail; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Gail Badner |
| 28 | + */ |
| 29 | +@TestForIssue( jiraKey = "HHH-10757") |
| 30 | +public class CastNullSelectExpressionTest extends BaseCoreFunctionalTestCase { |
| 31 | + |
| 32 | + @Test |
| 33 | + @TestForIssue(jiraKey = "HHH-10757") |
| 34 | + public void testSelectCastNull() { |
| 35 | + Session s = openSession(); |
| 36 | + s.getTransaction().begin(); |
| 37 | + Person person = new Person(); |
| 38 | + person.firstName = "Herman"; |
| 39 | + person.middleName = "Joseph"; |
| 40 | + person.lastName = "Munster"; |
| 41 | + s.persist( person ); |
| 42 | + s.flush(); |
| 43 | + s.clear(); |
| 44 | + |
| 45 | + Object[] result = (Object[]) s.createQuery( |
| 46 | + "select firstName, cast( null as string ), lastName from CastNullSelectExpressionTest$Person where lastName='Munster'" |
| 47 | + ).uniqueResult(); |
| 48 | + |
| 49 | + assertEquals( 3, result.length ); |
| 50 | + assertEquals( "Herman", result[0] ); |
| 51 | + assertNull( result[1] ); |
| 52 | + assertEquals( "Munster", result[2] ); |
| 53 | + |
| 54 | + s.getTransaction().rollback(); |
| 55 | + s.close(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @TestForIssue(jiraKey = "HHH-10757") |
| 60 | + public void testSelectNewCastNull() { |
| 61 | + Session s = openSession(); |
| 62 | + s.getTransaction().begin(); |
| 63 | + Person person = new Person(); |
| 64 | + person.firstName = "Herman"; |
| 65 | + person.middleName = "Joseph"; |
| 66 | + person.lastName = "Munster"; |
| 67 | + s.persist( person ); |
| 68 | + s.flush(); |
| 69 | + s.clear(); |
| 70 | + |
| 71 | + Person result = (Person) s.createQuery( |
| 72 | + "select new CastNullSelectExpressionTest$Person( id, firstName, cast( null as string ), lastName ) from CastNullSelectExpressionTest$Person where lastName='Munster'" |
| 73 | + ).uniqueResult(); |
| 74 | + assertEquals( "Herman", result.firstName ); |
| 75 | + assertNull( result.middleName ); |
| 76 | + assertEquals( "Munster", result.lastName ); |
| 77 | + |
| 78 | + s.getTransaction().rollback(); |
| 79 | + s.close(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected Class<?>[] getAnnotatedClasses() { |
| 84 | + return new Class[] { Person.class }; |
| 85 | + } |
| 86 | + |
| 87 | + @Entity |
| 88 | + @Table(name = "PERSON") |
| 89 | + private static class Person { |
| 90 | + @Id |
| 91 | + @GeneratedValue |
| 92 | + private long id; |
| 93 | + |
| 94 | + private String firstName; |
| 95 | + |
| 96 | + private String middleName; |
| 97 | + |
| 98 | + private String lastName; |
| 99 | + |
| 100 | + private Integer age; |
| 101 | + |
| 102 | + Person() { |
| 103 | + } |
| 104 | + |
| 105 | + public Person(long id, String firstName, String middleName, String lastName) { |
| 106 | + this.id = id; |
| 107 | + this.firstName = firstName; |
| 108 | + this.middleName = middleName; |
| 109 | + this.lastName = lastName; |
| 110 | + } |
| 111 | + |
| 112 | + public Person(long id, String firstName, Integer age, String lastName) { |
| 113 | + this.id = id; |
| 114 | + this.firstName = firstName; |
| 115 | + this.middleName = null; |
| 116 | + this.lastName = lastName; |
| 117 | + this.age = age; |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments