📄 astparserloadingtest.java
字号:
s.delete( tiger ); s.delete( mother ); s.delete( zoo ); s.getTransaction().commit(); s.close(); } public void testJoinFetchCollectionOfValues() { // note: simply performing syntax checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "select h from Human as h join fetch h.nickNames" ).list(); s.getTransaction().commit(); s.close(); } public void testIntegerLiterals() { // note: simply performing syntax checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Foo where long = 1" ).list(); s.createQuery( "from Foo where long = " + Integer.MIN_VALUE ).list(); s.createQuery( "from Foo where long = " + Integer.MAX_VALUE ).list(); s.createQuery( "from Foo where long = 1L" ).list(); s.createQuery( "from Foo where long = " + (Long.MIN_VALUE + 1) + "L" ).list(); s.createQuery( "from Foo where long = " + Long.MAX_VALUE + "L" ).list(); s.createQuery( "from Foo where integer = " + (Long.MIN_VALUE + 1) ).list();// currently fails due to HHH-1387// s.createQuery( "from Foo where long = " + Long.MIN_VALUE ).list(); s.getTransaction().commit(); s.close(); } public void testDecimalLiterals() { // note: simply performing syntax checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Animal where bodyWeight > 100.0e-10" ).list(); s.createQuery( "from Animal where bodyWeight > 100.0E-10" ).list(); s.createQuery( "from Animal where bodyWeight > 100.001f" ).list(); s.createQuery( "from Animal where bodyWeight > 100.001F" ).list(); s.createQuery( "from Animal where bodyWeight > 100.001d" ).list(); s.createQuery( "from Animal where bodyWeight > 100.001D" ).list(); s.createQuery( "from Animal where bodyWeight > .001f" ).list(); s.createQuery( "from Animal where bodyWeight > 100e-10" ).list(); s.createQuery( "from Animal where bodyWeight > .01E-10" ).list(); s.createQuery( "from Animal where bodyWeight > 1e-38" ).list(); s.getTransaction().commit(); s.close(); } public void testNakedPropertyRef() { // note: simply performing syntax and column/table resolution checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Animal where bodyWeight = bodyWeight" ).list(); s.createQuery( "select bodyWeight from Animal" ).list(); s.createQuery( "select max(bodyWeight) from Animal" ).list(); s.getTransaction().commit(); s.close(); } public void testNakedComponentPropertyRef() { // note: simply performing syntax and column/table resolution checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Human where name.first = 'Gavin'" ).list(); s.createQuery( "select name from Human" ).list(); s.createQuery( "select upper(h.name.first) from Human as h" ).list(); s.createQuery( "select upper(name.first) from Human" ).list(); s.getTransaction().commit(); s.close(); } public void testNakedImplicitJoins() { // note: simply performing syntax and column/table resolution checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Animal where mother.father.id = 1" ).list(); s.getTransaction().commit(); s.close(); } public void testNakedEntityAssociationReference() { // note: simply performing syntax and column/table resolution checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Animal where mother = :mother" ).setParameter( "mother", null ).list(); s.getTransaction().commit(); s.close(); } public void testNakedMapIndex() throws Exception { // note: simply performing syntax and column/table resolution checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Zoo where mammals['dog'].description like '%black%'" ).list(); s.getTransaction().commit(); s.close(); } public void testInvalidFetchSemantics() { Session s = openSession(); s.beginTransaction(); try { s.createQuery( "select mother from Human a left join fetch a.mother mother" ).list(); fail( "invalid fetch semantic allowed!" ); } catch( QueryException e ) { } try { s.createQuery( "select mother from Human a left join fetch a.mother mother" ).list(); fail( "invalid fetch semantic allowed!" ); } catch( QueryException e ) { } s.getTransaction().commit(); s.close(); } public void testArithmetic() { Session s = openSession(); Transaction t = s.beginTransaction(); Zoo zoo = new Zoo(); zoo.setName("Melbourne Zoo"); s.persist(zoo); s.createQuery("select 2*2*2*2*(2*2) from Zoo").uniqueResult(); s.createQuery("select 2 / (1+1) from Zoo").uniqueResult(); int result0 = ( (Integer) s.createQuery("select 2 - (1+1) from Zoo").uniqueResult() ).intValue(); int result1 = ( (Integer) s.createQuery("select 2 - 1 + 1 from Zoo").uniqueResult() ).intValue(); int result2 = ( (Integer) s.createQuery("select 2 * (1-1) from Zoo").uniqueResult() ).intValue(); int result3 = ( (Integer) s.createQuery("select 4 / (2 * 2) from Zoo").uniqueResult() ).intValue(); int result4 = ( (Integer) s.createQuery("select 4 / 2 * 2 from Zoo").uniqueResult() ).intValue(); int result5 = ( (Integer) s.createQuery("select 2 * (2/2) from Zoo").uniqueResult() ).intValue(); int result6 = ( (Integer) s.createQuery("select 2 * (2/2+1) from Zoo").uniqueResult() ).intValue(); assertEquals(result0, 0); assertEquals(result1, 2); assertEquals(result2, 0); assertEquals(result3, 1); assertEquals(result4, 4); assertEquals(result5, 2); assertEquals(result6, 4); s.delete(zoo); t.commit(); s.close(); } public void testNestedCollectionFetch() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery("from Animal a left join fetch a.offspring o left join fetch o.offspring where a.mother.id = 1 order by a.description").list(); s.createQuery("from Zoo z left join fetch z.animals a left join fetch a.offspring where z.name ='MZ' order by a.description").list(); s.createQuery("from Human h left join fetch h.pets a left join fetch a.offspring where h.name.first ='Gavin' order by a.description").list(); t.commit(); s.close(); } public void testSelectClauseSubselect() { Session s = openSession(); Transaction t = s.beginTransaction(); Zoo zoo = new Zoo(); zoo.setName("Melbourne Zoo"); zoo.setMammals( new HashMap() ); zoo.setAnimals( new HashMap() ); Mammal plat = new Mammal(); plat.setBodyWeight( 11f ); plat.setDescription( "Platypus" ); plat.setZoo(zoo); plat.setSerialNumber("plat123"); zoo.getMammals().put("Platypus", plat); zoo.getAnimals().put("plat123", plat); s.persist( plat ); s.persist(zoo); s.createQuery("select (select max(z.id) from a.zoo z) from Animal a").list(); s.createQuery("select (select max(z.id) from a.zoo z where z.name=:name) from Animal a") .setParameter("name", "Melbourne Zoo").list(); s.delete(plat); s.delete(zoo); t.commit(); s.close(); } public void testInitProxy() { Session s = openSession(); Transaction t = s.beginTransaction(); Mammal plat = new Mammal(); plat.setBodyWeight( 11f ); plat.setDescription( "Platypus" ); s.persist( plat ); s.flush(); s.clear(); plat = (Mammal) s.load(Mammal.class, plat.getId() ); assertFalse( Hibernate.isInitialized(plat) ); Object plat2 = s.createQuery("from Animal a").uniqueResult(); assertSame(plat, plat2); assertTrue( Hibernate.isInitialized(plat) ); s.delete(plat); t.commit(); s.close(); } public void testSelectClauseImplicitJoin() { Session s = openSession(); Transaction t = s.beginTransaction(); Zoo zoo = new Zoo(); zoo.setName("The Zoo"); zoo.setMammals( new HashMap() ); zoo.setAnimals( new HashMap() ); Mammal plat = new Mammal(); plat.setBodyWeight( 11f ); plat.setDescription( "Platypus" ); plat.setZoo(zoo); plat.setSerialNumber("plat123"); zoo.getMammals().put("Platypus", plat); zoo.getAnimals().put("plat123", plat); s.persist( plat ); s.persist(zoo); s.flush(); s.clear(); Query q = s.createQuery("select distinct a.zoo from Animal a where a.zoo is not null"); Type type = q.getReturnTypes()[0]; assertTrue( type instanceof ManyToOneType ); assertEquals( ( (ManyToOneType) type ).getAssociatedEntityName(), "org.hibernate.test.hql.Zoo" ); zoo = (Zoo) q.list().get(0); assertEquals( zoo.getMammals().size(), 1 ); assertEquals( zoo.getAnimals().size(), 1 ); s.clear(); s.delete(plat); s.delete(zoo); t.commit(); s.close(); } public void testSelectClauseImplicitJoinWithIterate() { Session s = openSession(); Transaction t = s.beginTransaction(); Zoo zoo = new Zoo(); zoo.setName("The Zoo"); zoo.setMammals( new HashMap() ); zoo.setAnimals( new HashMap() ); Mammal plat = new Mammal(); plat.setBodyWeight( 11f ); plat.setDescription( "Platypus" ); plat.setZoo(zoo); plat.setSerialNumber("plat123"); zoo.getMammals().put("Platypus", plat); zoo.getAnimals().put("plat123", plat); s.persist( plat ); s.persist(zoo); s.flush(); s.clear(); Query q = s.createQuery("select distinct a.zoo from Animal a where a.zoo is not null"); Type type = q.getReturnTypes()[0]; assertTrue( type instanceof ManyToOneType ); assertEquals( ( (ManyToOneType) type ).getAssociatedEntityName(), "org.hibernate.test.hql.Zoo" ); zoo = (Zoo) q .iterate().next(); assertEquals( zoo.getMammals().size(), 1 ); assertEquals( zoo.getAnimals().size(), 1 ); s.clear(); s.delete(plat); s.delete(zoo); t.commit(); s.close(); } public void testComponentOrderBy() { Session s = openSession(); Transaction t = s.beginTransaction(); Long id1 = ( Long ) s.save( genSimpleHuman( "John", "Jacob" ) ); Long id2 = ( Long ) s.save( genSimpleHuman( "Jingleheimer", "Schmidt" ) ); s.flush(); // the component is defined with the firstName column first... List results = s.createQuery( "from Human as h order by h.name" ).list(); assertEquals( "Incorrect return count", 2, results.size() ); Human h1 = ( Human ) results.get( 0 ); Human h2 = ( Human ) results.get( 1 ); assertEquals( "Incorrect ordering", id2, h1.getId() ); assertEquals( "Incorrect ordering", id1, h2.getId() ); s.delete( h1 ); s.delete( h2 ); t.commit(); s.close(); } private Human genSimpleHuman(String fName, String lName) { Human h = new Human(); h.setName( new Name( fName, 'X', lName ) ); return h; } public void testCastInSelect() { Session s = openSession(); Transaction t = s.beginTransaction(); Animal a = new Animal(); a.setBodyWeight(12.4f); a.setDescription("an animal"); s.persist(a); Integer bw = (Integer) s.createQuery("select cast(bodyWeight as integer) from Animal").uniqueResult(); bw = (Integer) s.createQuery("select cast(a.bodyWeight as integer) from Animal a").uniqueResult(); bw.toString(); s.delete(a); t.commit(); s.close(); } public void testAliases() { Session s = openSession(); Transaction t = s.beginTransaction(); Animal a = new Animal(); a.setBodyWeight(12.4f); a.setDescription("an animal"); s.persist(a); String[] aliases1 = s.createQuery("select a.bodyWeight as abw, a.description from Animal a").getReturnAliases(); assertEquals(aliases1[0], "abw"); assertEquals(aliases1[1], "1"); String[] aliases2 = s.createQuery("select count(*), avg(a.bodyWeight) as avg from Animal a").getReturnAliases(); assertEquals(aliases2[0], "0"); assertEquals(aliases2[1], "avg"); s.delete(a); t.commit(); s.close(); } public void testParameterMixing() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( "from Animal a where a.description = ? and a.bodyWeight = ? or a.bodyWeight = :bw" ) .setString( 0, "something" ) .setFloat( 1, 12345f ) .setFloat( "bw", 123f ) .list(); t.commit(); s.close(); } public void testOrdinalParameters() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -