|
| 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.subselectfetch; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import org.hibernate.Session; |
| 16 | +import org.hibernate.dialect.SQLServerDialect; |
| 17 | +import org.hibernate.dialect.SybaseDialect; |
| 18 | +import org.hibernate.mapping.Collection; |
| 19 | +import org.hibernate.testing.RequiresDialect; |
| 20 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 21 | + |
| 22 | +import static org.hamcrest.CoreMatchers.is; |
| 23 | +import static org.junit.Assert.assertThat; |
| 24 | + |
| 25 | +@RequiresDialect({SQLServerDialect.class,SybaseDialect.class}) |
| 26 | +public class SubselectFetchWithFormulaTransactSqlTest extends BaseNonConfigCoreFunctionalTestCase { |
| 27 | + @Override |
| 28 | + protected String getBaseForMappings() { |
| 29 | + return ""; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + protected String[] getMappings() { |
| 34 | + return new String[] { |
| 35 | + "org/hibernate/test/subselectfetch/NameTransactSql.hbm.xml", |
| 36 | + "org/hibernate/test/subselectfetch/Value.hbm.xml" |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + @Before |
| 41 | + public void before() { |
| 42 | + Session session = openSession(); |
| 43 | + session.getTransaction().begin(); |
| 44 | + |
| 45 | + Name chris = new Name(); |
| 46 | + chris.setId( 1 ); |
| 47 | + chris.setName( "chris" ); |
| 48 | + Value cat = new Value(); |
| 49 | + cat.setId(1); |
| 50 | + cat.setName( chris ); |
| 51 | + cat.setValue( "cat" ); |
| 52 | + Value canary = new Value(); |
| 53 | + canary.setId( 2 ); |
| 54 | + canary.setName( chris ); |
| 55 | + canary.setValue( "canary" ); |
| 56 | + |
| 57 | + session.persist( chris ); |
| 58 | + session.persist( cat ); |
| 59 | + session.persist( canary ); |
| 60 | + |
| 61 | + Name sam = new Name(); |
| 62 | + sam.setId(2); |
| 63 | + sam.setName( "sam" ); |
| 64 | + Value seal = new Value(); |
| 65 | + seal.setId( 3 ); |
| 66 | + seal.setName( sam ); |
| 67 | + seal.setValue( "seal" ); |
| 68 | + Value snake = new Value(); |
| 69 | + snake.setId( 4 ); |
| 70 | + snake.setName( sam ); |
| 71 | + snake.setValue( "snake" ); |
| 72 | + |
| 73 | + session.persist( sam ); |
| 74 | + session.persist(seal); |
| 75 | + session.persist( snake ); |
| 76 | + |
| 77 | + session.getTransaction().commit(); |
| 78 | + session.close(); |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void after() { |
| 83 | + Session session = openSession(); |
| 84 | + session.getTransaction().begin(); |
| 85 | + session.createQuery( "delete Value" ).executeUpdate(); |
| 86 | + session.createQuery( "delete Name" ).executeUpdate(); |
| 87 | + session.getTransaction().commit(); |
| 88 | + session.close(); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + @Test |
| 93 | + public void checkSubselectWithFormula() throws Exception { |
| 94 | + // as a pre-condition make sure that subselect fetching is enabled for the collection... |
| 95 | + Collection collectionBinding = metadata().getCollectionBinding( Name.class.getName() + ".values" ); |
| 96 | + assertThat( collectionBinding.isSubselectLoadable(), is( true ) ); |
| 97 | + |
| 98 | + // Now force the subselect fetch and make sure we do not get SQL errors |
| 99 | + Session session = openSession(); |
| 100 | + session.getTransaction().begin(); |
| 101 | + List results = session.createCriteria(Name.class).list(); |
| 102 | + for (Object result : results) { |
| 103 | + Name name = (Name) result; |
| 104 | + name.getValues().size(); |
| 105 | + } |
| 106 | + session.getTransaction().commit(); |
| 107 | + session.close(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments