|
23 | 23 | */
|
24 | 24 | package org.hibernate.jpa.test.criteria.tuple;
|
25 | 25 |
|
| 26 | +import java.util.Date; |
26 | 27 | import java.util.List;
|
27 | 28 | import javax.persistence.EntityManager;
|
28 | 29 | import javax.persistence.Tuple;
|
|
38 | 39 | import org.hibernate.jpa.test.metamodel.Customer;
|
39 | 40 | import org.hibernate.jpa.test.metamodel.Customer_;
|
40 | 41 |
|
41 |
| -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; |
42 | 42 | import static org.junit.Assert.assertEquals;
|
| 43 | +import static org.junit.Assert.assertNotNull; |
43 | 44 | import static org.junit.Assert.assertTrue;
|
44 | 45 | import static org.junit.Assert.fail;
|
45 | 46 |
|
@@ -145,6 +146,52 @@ public void testIllegalArgumentExceptionBuildingTupleWithSameAliases() {
|
145 | 146 | em.close();
|
146 | 147 | }
|
147 | 148 |
|
| 149 | + @Test |
| 150 | + public void testVariousTupleAccessMethods() { |
| 151 | + EntityManager em = entityManagerFactory().createEntityManager(); |
| 152 | + em.getTransaction().begin(); |
| 153 | + Customer c1 = new Customer(); |
| 154 | + c1.setId( "c1" ); |
| 155 | + c1.setAge( 18 ); |
| 156 | + c1.setName( "Bob" ); |
| 157 | + em.persist( c1 ); |
| 158 | + em.getTransaction().commit(); |
| 159 | + em.close(); |
| 160 | + |
| 161 | + em = entityManagerFactory().createEntityManager(); |
| 162 | + em.getTransaction().begin(); |
| 163 | + |
| 164 | + final CriteriaBuilder builder = em.getCriteriaBuilder(); |
| 165 | + CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); |
| 166 | + Root<Customer> customerRoot = criteria.from( Customer.class ); |
| 167 | + Path<String> namePath = customerRoot.get( Customer_.name ); |
| 168 | + namePath.alias( "NAME" ); |
| 169 | + Path<Integer> agePath = customerRoot.get( Customer_.age ); |
| 170 | + agePath.alias( "AGE" ); |
| 171 | + criteria.multiselect( namePath, agePath ); |
| 172 | + |
| 173 | + List<Tuple> results = em.createQuery( criteria ).getResultList(); |
| 174 | + Tuple tuple = results.get( 0 ); |
| 175 | + assertNotNull( tuple ); |
| 176 | + assertNotNull( tuple.get( "NAME" ) ); |
| 177 | + assertNotNull( tuple.get( "NAME", String.class ) ); |
| 178 | + try { |
| 179 | + tuple.get( "NAME", Date.class ); |
| 180 | + fail( "Accessing Customer#name tuple as Date should have thrown exception" ); |
| 181 | + } |
| 182 | + catch (IllegalArgumentException expected) { |
| 183 | + } |
| 184 | + |
| 185 | + em.getTransaction().commit(); |
| 186 | + em.close(); |
| 187 | + |
| 188 | + em = entityManagerFactory().createEntityManager(); |
| 189 | + em.getTransaction().begin(); |
| 190 | + em.createQuery( "delete Customer" ).executeUpdate(); |
| 191 | + em.getTransaction().commit(); |
| 192 | + em.close(); |
| 193 | + } |
| 194 | + |
148 | 195 | @Test
|
149 | 196 | public void testIllegalArgumentExceptionBuildingSelectArrayWithSameAliases() {
|
150 | 197 | EntityManager em = entityManagerFactory().createEntityManager();
|
|
0 commit comments