📄 foobartest.java
字号:
Object[] row = (Object[]) rs.next(); assertTrue( row[0] instanceof Long ); assertTrue( row[1] instanceof String ); assertTrue( row[2] instanceof Foo ); assertTrue( row[3] instanceof Foo ); } assertTrue(count!=0); list = s.find("select avg(foo.float), max(foo.component.name), count(distinct foo.id) from foo in class Foo"); rs = list.iterator(); count=0; while ( rs.hasNext() ) { count++; Object[] row = (Object[]) rs.next(); assertTrue( row[0] instanceof Float ); assertTrue( row[1] instanceof String ); assertTrue( row[2] instanceof Integer ); } assertTrue(count!=0); list = s.find("select foo.long, foo.component, foo, foo.foo from foo in class Foo"); rs = list.iterator(); count=0; while ( rs.hasNext() ) { count++; Object[] row = (Object[]) rs.next(); assertTrue( row[0] instanceof Long ); assertTrue( row[1] instanceof FooComponent ); assertTrue( row[2] instanceof Foo ); assertTrue( row[3] instanceof Foo ); } assertTrue(count!=0); s.save( new Holder("ice T") ); s.save( new Holder("ice cube") ); assertTrue( s.find("from o in class java.lang.Object").size()==15 ); System.out.println( s.find("from n in class Named") ); assertTrue( s.find("from n in class Named").size()==7 ); assertTrue( s.find("from n in class Named where n.name is not null").size()==4 ); iter = s.iterate("from n in class Named"); while ( iter.hasNext() ) { assertTrue( iter.next() instanceof Named ); } s.save( new Holder("bar") ); iter = s.iterate("from n0 in class Named, n1 in class Named where n0.name = n1.name"); int cnt = 0; while ( iter.hasNext() ) { Object[] row = (Object[]) iter.next(); if ( row[0]!=row[1] ) cnt++; } if ( !(getDialect() instanceof HSQLDialect) ) { assertTrue(cnt==2); assertTrue( s.find("from n0 in class Named, n1 in class Named where n0.name = n1.name").size()==7 ); } Query qu = s.createQuery("from n in class Named where n.name = :name"); qu.getReturnTypes(); qu.getNamedParameters(); iter = s.iterate("from o in class java.lang.Object"); int c = 0; while ( iter.hasNext() ) { iter.next(); c++; } assertTrue(c==16); s.iterate("select baz.code, min(baz.count) from baz in class Baz group by baz.code"); iter = s.iterate("selecT baz from baz in class Baz where baz.stringDateMap['foo'] is not null or baz.stringDateMap['bar'] = ?", new Date(), Hibernate.DATE); assertFalse( iter.hasNext() ); list = s.find("select baz from baz in class Baz where baz.stringDateMap['now'] is not null"); assertTrue( list.size()==1 ); list = s.find("select baz from baz in class Baz where baz.stringDateMap['now'] is not null and baz.stringDateMap['big bang'] < baz.stringDateMap['now']"); assertTrue( list.size()==1 ); list = s.find("select index(date) from Baz baz join baz.stringDateMap date"); System.out.println(list); assertTrue( list.size()==2 ); s.find("from foo in class Foo where foo.integer not between 1 and 5 and foo.string not in ('cde', 'abc') and foo.string is not null and foo.integer<=3"); s.find("from Baz baz inner join baz.collectionComponent.nested.foos foo where foo.string is null"); if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) /*&& !(dialect instanceof Oracle9Dialect)*/ ) { s.find("from Baz baz inner join baz.fooSet where '1' in (from baz.fooSet foo where foo.string is not null)"); s.find("from Baz baz where 'a' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)"); s.find("from Baz baz where 'b' in baz.collectionComponent.nested.foos.elements and 1.0 in baz.collectionComponent.nested.floats.elements"); } s.find("from Foo foo join foo.foo where foo.foo in ('1','2','3')"); if ( !(getDialect() instanceof HSQLDialect) ) s.find("from Foo foo left join foo.foo where foo.foo in ('1','2','3')"); s.find("select foo.foo from Foo foo where foo.foo in ('1','2','3')"); s.find("select foo.foo.string from Foo foo where foo.foo in ('1','2','3')"); s.find("select foo.foo.string from Foo foo where foo.foo.string in ('1','2','3')"); s.find("select foo.foo.long from Foo foo where foo.foo.string in ('1','2','3')"); s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') or foo.foo.long in (1,2,3)"); s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') group by foo.foo.long"); s.find("from Foo foo1 left join foo1.foo foo2 left join foo2.foo where foo1.string is not null"); s.find("from Foo foo1 left join foo1.foo.foo where foo1.string is not null"); s.find("from Foo foo1 left join foo1.foo foo2 left join foo1.foo.foo foo3 where foo1.string is not null"); s.find("select foo.formula from Foo foo where foo.formula > 0"); s.delete("from Holder"); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.createQuery("from Baz baz left outer join fetch baz.manyToAny").uniqueResult(); assertTrue( Hibernate.isInitialized( baz.getManyToAny() ) ); assertTrue( baz.getManyToAny().size()==2 ); BarProxy barp = (BarProxy) baz.getManyToAny().get(0); s.find("from Baz baz join baz.manyToAny"); assertTrue( s.find("select baz from Baz baz join baz.manyToAny a where index(a) = 0").size()==1 ); FooProxy foop = (FooProxy) s.get( Foo.class, foo.getKey() ); assertTrue( foop == baz.getManyToAny().get(1) ); barp.setBaz(baz); assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['now'] is not null").size()==1 ); assertTrue( s.find("select bar from Bar bar join bar.baz b where b.stringDateMap['big bang'] < b.stringDateMap['now'] and b.stringDateMap['now'] is not null").size()==1 ); assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['big bang'] < bar.baz.stringDateMap['now'] and bar.baz.stringDateMap['now'] is not null").size()==1 ); s.delete(barp); s.delete(baz); s.delete( foop.getFoo() ); s.delete(foop); s.flush(); s.connection().commit(); s.close(); } public void testCascadeDeleteDetached() throws Exception { Session s = openSession(); Baz baz = new Baz(); List list = new ArrayList(); list.add( new Fee() ); baz.setFees(list); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.get( Baz.class, baz.getCode() ); s.connection().commit(); s.close(); assertFalse( Hibernate.isInitialized( baz.getFees() ) ); s = openSession(); s.delete(baz); s.flush(); assertFalse( s.iterate("from Fee").hasNext() ); s.connection().commit(); s.close(); s = openSession(); baz = new Baz(); list = new ArrayList(); list.add( new Fee() ); list.add( new Fee() ); baz.setFees(list); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.get( Baz.class, baz.getCode() ); Hibernate.initialize( baz.getFees() ); s.connection().commit(); s.close(); assertTrue( baz.getFees().size()==2 ); s = openSession(); s.delete(baz); s.flush(); assertFalse( s.iterate("from Fee").hasNext() ); s.connection().commit(); s.close(); } public void testForeignKeys() throws Exception { Session s = openSession(); Baz baz = new Baz(); Foo foo = new Foo(); List bag = new ArrayList(); bag.add(foo); baz.setIdFooBag(bag); baz.setFoo(foo); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load( Baz.class, baz.getCode() ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); } public void testDatabinder() throws Exception { Session s = openSession(); BarProxy bar = new Bar(); s.save(bar); FooProxy foo = new Foo(); s.save(foo); FooProxy foo2 = new Foo(); s.save(foo2); foo2.setFoo(foo); Baz baz = new Baz(); s.save(baz); baz.setDefaults(); baz.setFooArray( new FooProxy[] { foo, foo2, bar, null } ); bar.setFoo(foo); s.flush(); s.connection().commit(); s.close(); s = openSession(); bar = (BarProxy) s.load( Foo.class, bar.getKey() ); foo = (FooProxy) s.load( Foo.class, foo.getKey() ); foo2 = (FooProxy) s.load( Foo.class, foo2.getKey() ); baz = (Baz) s.load( Baz.class, baz.getCode() ); Databinder binder = getSessions().openDatabinder(); binder.setInitializeLazy(true); binder.bind(foo2).bind(baz).bind(foo).bind(bar); System.out.println( binder.toGenericXML() ); binder.bind(foo2).bind(baz); System.out.println( binder.toXML() ); assertTrue( "dom", binder.toDOM()!=null ); assertTrue( "generic dom", binder.toGenericDOM()!=null ); s.delete(foo); s.delete(baz); s.delete(bar); s.delete(foo2); s.flush(); s.connection().commit(); s.close(); } public void testNonlazyCollection() throws Exception { Session s = openSession(); Baz baz = new Baz(); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.createCriteria(Baz.class) .setFetchMode("stringDateMap", FetchMode.EAGER) .uniqueResult(); assertTrue( Hibernate.isInitialized( baz.getFooToGlarch() ) ); assertTrue( Hibernate.isInitialized( baz.getFooComponentToFoo() ) ); assertTrue( !Hibernate.isInitialized( baz.getStringSet() ) ); assertTrue( Hibernate.isInitialized( baz.getStringDateMap() ) ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); } public void testReuseDeletedCollection() throws Exception { Session s = openSession(); Baz baz = new Baz(); baz.setDefaults(); s.save(baz); s.flush(); s.delete(baz); Baz baz2 = new Baz(); baz2.setStringArray( new String[] {"x-y-z"} ); s.save(baz2); s.flush(); s.connection().commit(); s.close(); baz2.setStringSet( baz.getStringSet() ); baz2.setStringArray( baz.getStringArray() ); baz2.setFooArray( baz.getFooArray() ); s = openSession(); s.update(baz2); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz2 = (Baz) s.load( Baz.class, baz2.getCode() ); assertTrue( baz2.getStringArray().length==3 ); assertTrue( baz2.getStringSet().size()==3 ); s.delete(baz2); s.flush(); s.connection().commit(); s.close(); } public void testPropertyRef() throws Exception { Session s = openSession(); Holder h = new Holder(); h.setName("foo"); Holder h2 = new Holder(); h2.setName("bar"); h.setOtherHolder(h2); Serializable hid = s.save(h); Qux q = new Qux(); q.setHolder(h2); Serializable qid = s.save(q); s.flush(); s.connection().commit(); s.close(); s = openSession(); h = (Holder) s.load(Holder.class, hid); assertEquals( h.getName(), "foo"); assertEquals( h.getOtherHolder().getName(), "bar"); Object[] res = (Object[]) s.find("from Holder h join h.otherHolder oh where h.otherHolder.name = 'bar'").get(0); assertTrue( res[0]==h ); q = (Qux) s.get(Qux.class, qid); assertTrue( q.getHolder() == h.getOtherHolder() ); s.delete(h); s.delete(q); s.flush(); s.connection().commit(); s.close(); } public void testQueryCollectionOfValues() throws Exception { Session s = openSession(); Baz baz = new Baz(); baz.setDefaults(); s.save(baz); Glarch g = new Glarch(); Serializable gid = s.save(g); if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) /*&& !(dialect instanceof MckoiDialect)*/ && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) ) { s.filter( baz.getFooArray(), "where size(this.bytes) > 0"); s.filter( baz.getFooArray(), "where 0 in elements(this.bytes)"); } s.flush(); s.connection().commit(); s.close(); s = openSession(); //s.find("from Baz baz where baz.fooSet.string = 'foo'"); //s.find("from Baz baz where baz.fooArray.string = 'foo'"); //s.find("from Baz baz where baz.fooSet.foo.string = 'foo'"); //s.find("from Baz baz join baz.fooSet.foo foo where foo.string = 'foo'"); s.find("from Baz baz join baz.fooSet foo join foo.foo.foo foo2 where foo2.string = 'foo'"); s.find("from Baz baz join baz.fooArray foo join foo.foo.foo foo2 where foo2.string = 'foo'"); s.find("from Baz baz join baz.stringDateMap date where index(date) = 'foo'"); s.find("from Baz baz join baz.topGlarchez g where index(g) = 'A'"); s.find("select index(g) from Baz baz join baz.topGlarchez g"); assertTrue( s.find("from Baz baz left join baz.stringSet").size()==3 ); baz = (Baz) s.find("from Baz baz join baz.stringSet str where str='foo'").get(0); assertTrue( !Hibernate.isInitialized( baz.getStringSet() ) ); baz = (Baz) s.find("from Baz baz left join fetch baz.stringSet").get(0); assertTrue( Hibernate.isInitialized( baz.getStringSet() ) ); assertTrue( s.find("from Baz baz join baz.stringSet string where string='foo'").size()==1 ); assertTrue( s.find("from Baz baz inner join baz.components comp where comp.name='foo'").size()==1 ); //List bss = s.find("select baz, ss from Baz baz inner join baz.stringSet ss"); s.find("from Glarch g inner join g.fooComponents comp where comp.fee is not null"); s.find("from Glarch g inner join g.fooComponents comp join comp.fee fee where fee.count > 0"); s.find("from Glarch g inner join g.fooComponents comp where comp.fee.count is not null"); s.delete(baz); //s.delete("from Glarch g"); s.delete( s.get(Glarch.class, gid) ); s.flush(); s.connection().commit(); s.close(); } public void testBatchLoad() throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -