astparserloadingtest.java
来自「hibernate-3.0.5 中文文档」· Java 代码 · 共 673 行 · 第 1/2 页
JAVA
673 行
txn.commit(); session.close(); } public void testImplicitJoin() throws Exception { Session session = openSession(); Transaction t = session.beginTransaction(); Animal a = new Animal(); a.setBodyWeight(0.5f); a.setBodyWeight(1.5f); Animal b = new Animal(); Animal mother = new Animal(); mother.setBodyWeight(10.0f); mother.addOffspring(a); mother.addOffspring(b); session.persist(a); session.persist(b); session.persist(mother); List list = session.createQuery("from Animal a where a.mother.bodyWeight < 2.0 or a.mother.bodyWeight > 9.0").list(); assertEquals( list.size(), 2 ); list = session.createQuery("from Animal a where a.mother.bodyWeight > 2.0 and a.mother.bodyWeight > 9.0").list(); assertEquals( list.size(), 2 ); session.delete(b); session.delete(a); session.delete(mother); t.commit(); session.close(); } public void testFromOnly() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "from Animal" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); session.close(); destroyTestBaseData(); } public void testSimpleSelect() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "select a from Animal as a" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); session.close(); destroyTestBaseData(); } public void testEntityPropertySelect() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "select a.mother from Animal as a" ).list();// assertEquals("Incorrect result size", 2, results.size()); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); session.close(); destroyTestBaseData(); } public void testWhere() throws Exception { createTestBaseData(); Session session = openSession(); List results = null; results = session.createQuery( "from Animal an where an.bodyWeight > 10" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); results = session.createQuery( "from Animal an where not an.bodyWeight > 10" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); results = session.createQuery( "from Animal an where an.bodyWeight between 0 and 10" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); results = session.createQuery( "from Animal an where an.bodyWeight not between 0 and 10" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); results = session.createQuery( "from Animal an where sqrt(an.bodyWeight)/2 > 10" ).list(); assertEquals( "Incorrect result size", 0, results.size() ); results = session.createQuery( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); session.close(); destroyTestBaseData(); } public void testEntityFetching() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "from Animal an join fetch an.mother" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); Animal mother = ( ( Animal ) results.get( 0 ) ).getMother(); assertTrue( "fetch uninitialized", mother != null && Hibernate.isInitialized( mother ) ); results = session.createQuery( "select an from Animal an join fetch an.mother" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); mother = ( ( Animal ) results.get( 0 ) ).getMother(); assertTrue( "fetch uninitialized", mother != null && Hibernate.isInitialized( mother ) ); session.close(); destroyTestBaseData(); } public void testCollectionFetching() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "from Animal an join fetch an.offspring" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); Collection os = ( ( Animal ) results.get( 0 ) ).getOffspring(); assertTrue( "fetch uninitialized", os != null && Hibernate.isInitialized( os ) && os.size() == 1 ); results = session.createQuery( "select an from Animal an join fetch an.offspring" ).list(); assertEquals( "Incorrect result size", 1, results.size() ); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); os = ( ( Animal ) results.get( 0 ) ).getOffspring(); assertTrue( "fetch uninitialized", os != null && Hibernate.isInitialized( os ) && os.size() == 1 ); session.close(); destroyTestBaseData(); } public void testProjectionQueries() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "select an.mother.id, max(an.bodyWeight) from Animal an group by an.mother.id" ).list(); // mysql returns nulls in this group by assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof Object[] ); assertEquals( "Incorrect return dimensions", 2, ( ( Object[] ) results.get( 0 ) ).length ); session.close(); destroyTestBaseData(); } public void testStandardFunctions() throws Exception { Session session = openSession(); Product p = new Product(); p.setDescription("a product"); p.setPrice( new BigDecimal(1.0) ); p.setProductId("abc123"); session.persist(p); Object[] result = (Object[]) session .createQuery("select current_time(), current_date(), current_timestamp() from Product") .uniqueResult(); assertTrue( result[0] instanceof Time ); assertTrue( result[1] instanceof Date ); assertTrue( result[2] instanceof Timestamp ); assertNotNull( result[0] ); assertNotNull( result[1] ); assertNotNull( result[2] ); session.delete(p); session.close(); } public void testDynamicInstantiationQueries() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof Animal ); Iterator iter = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).iterate(); assertTrue( "Incorrect result size", iter.hasNext() ); assertTrue( "Incorrect return type", iter.next() instanceof Animal ); results = session.createQuery( "select new list(an.description, an.bodyWeight) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof List ); assertEquals( "Incorrect return type", ( (List) results.get( 0 ) ).size(), 2 ); iter = session.createQuery( "select new list(an.description, an.bodyWeight) from Animal an" ).iterate(); assertTrue( "Incorrect result size", iter.hasNext() ); Object obj = iter.next(); assertTrue( "Incorrect return type", obj instanceof List ); assertEquals( "Incorrect return type", ( (List) obj ).size(), 2 ); results = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof Map ); assertEquals( "Incorrect return type", ( (Map) results.get( 0 ) ).size(), 2 ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("0") ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("1") ); results = session.createQuery( "select new map(an.description as descr, an.bodyWeight as bw) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof Map ); assertEquals( "Incorrect return type", ( (Map) results.get( 0 ) ).size(), 2 ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("descr") ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("bw") ); iter = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).iterate(); assertTrue( "Incorrect result size", iter.hasNext() ); obj = iter.next(); assertTrue( "Incorrect return type", obj instanceof Map ); assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 ); session.close(); destroyTestBaseData(); } public void testEJBQLFunctions() throws Exception { Session session = openSession(); String hql = "from Animal a where a.description = concat('1', concat('2','3'), '4'||'5')||'0'"; session.createQuery(hql).list(); hql = "from Animal a where substring(a.description, 1, 3) = 'cat'"; session.createQuery(hql).list(); hql = "select substring(a.description, 1, 3) from Animal a"; session.createQuery(hql).list(); hql = "from Animal a where lower(a.description) = 'cat'"; session.createQuery(hql).list(); hql = "select lower(a.description) from Animal a"; session.createQuery(hql).list(); hql = "from Animal a where upper(a.description) = 'CAT'"; session.createQuery(hql).list(); hql = "select upper(a.description) from Animal a"; session.createQuery(hql).list(); hql = "from Animal a where length(a.description) = 5"; session.createQuery(hql).list(); hql = "select length(a.description) from Animal a"; session.createQuery(hql).list(); //note: postgres and db2 don't have a 3-arg form, it gets transformed to 2-args hql = "from Animal a where locate('abc', a.description, 2) = 2"; session.createQuery(hql).list(); hql = "from Animal a where locate('abc', a.description) = 2"; session.createQuery(hql).list(); hql = "select locate('cat', a.description, 2) from Animal a"; session.createQuery(hql).list(); if ( !( getDialect() instanceof DB2Dialect ) ) { hql = "from Animal a where trim(trailing '_' from a.description) = 'cat'"; session.createQuery(hql).list(); hql = "select trim(trailing '_' from a.description) from Animal a"; session.createQuery(hql).list(); hql = "from Animal a where trim(leading '_' from a.description) = 'cat'"; session.createQuery(hql).list(); hql = "from Animal a where trim(both from a.description) = 'cat'"; session.createQuery(hql).list(); } if ( !(getDialect() instanceof HSQLDialect) ) { //HSQL doesn't like trim() without specification hql = "from Animal a where trim(a.description) = 'cat'"; session.createQuery(hql).list(); } hql = "from Animal a where abs(a.bodyWeight) = sqrt(a.bodyWeight)"; session.createQuery(hql).list(); hql = "from Animal a where mod(16, 4) = 4"; session.createQuery(hql).list(); hql = "from Animal a where bit_length(a.bodyWeight) = 24"; session.createQuery(hql).list(); hql = "select bit_length(a.bodyWeight) from Animal a"; session.createQuery(hql).list(); /*hql = "select object(a) from Animal a where CURRENT_DATE = :p1 or CURRENT_TIME = :p2 or CURRENT_TIMESTAMP = :p3"; session.createQuery(hql).list();*/ // todo the following is not supported //hql = "select CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP from Animal a"; //parse(hql, true); //System.out.println("sql: " + toSql(hql)); hql = "from Animal a where a.description like '%a%'"; session.createQuery(hql).list(); hql = "from Animal a where a.description not like '%a%'"; session.createQuery(hql).list(); hql = "from Animal a where a.description like 'x%ax%' escape 'x'"; session.createQuery(hql).list(); session.close(); } public static Test suite() { TestSuite suite = new TestSuite( ASTParserLoadingTest.class ); return suite; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?