📄 foobartest.java
字号:
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 { Session s = openSession(); Baz baz = new Baz(); SortedSet stringSet = new TreeSet(); stringSet.add("foo"); stringSet.add("bar"); Set fooSet = new HashSet(); for (int i=0; i<3; i++) { Foo foo = new Foo(); s.save(foo); fooSet.add(foo); } baz.setFooSet(fooSet); baz.setStringSet(stringSet); s.save(baz); Baz baz2 = new Baz(); fooSet = new HashSet(); for (int i=0; i<2; i++) { Foo foo = new Foo(); s.save(foo); fooSet.add(foo); } baz2.setFooSet(fooSet); s.save(baz2); Baz baz3 = new Baz(); stringSet = new TreeSet(); stringSet.add("foo"); stringSet.add("baz"); baz3.setStringSet(stringSet); s.save(baz3); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load( Baz.class, baz.getCode() ); baz2 = (Baz) s.load( Baz.class, baz2.getCode() ); baz3 = (Baz) s.load( Baz.class, baz3.getCode() ); assertFalse( Hibernate.isInitialized(baz.getFooSet()) || Hibernate.isInitialized(baz2.getFooSet()) || Hibernate.isInitialized(baz3.getFooSet()) ); assertFalse( Hibernate.isInitialized(baz.getStringSet()) || Hibernate.isInitialized(baz2.getStringSet()) || Hibernate.isInitialized(baz3.getStringSet()) ); assertTrue( baz.getFooSet().size()==3 ); assertTrue( Hibernate.isInitialized(baz.getFooSet()) && Hibernate.isInitialized(baz2.getFooSet()) && Hibernate.isInitialized(baz3.getFooSet())); assertTrue( baz2.getFooSet().size()==2 ); assertTrue( baz3.getStringSet().contains("baz") ); assertTrue( Hibernate.isInitialized(baz.getStringSet()) && Hibernate.isInitialized(baz2.getStringSet()) && Hibernate.isInitialized(baz3.getStringSet())); assertTrue( baz.getStringSet().size()==2 && baz2.getStringSet().size()==0 ); s.delete(baz); s.delete(baz2); s.delete(baz3); Iterator iter = new JoinedIterator( new Iterator[] { baz.getFooSet().iterator(), baz2.getFooSet().iterator() } ); while ( iter.hasNext() ) s.delete( iter.next() ); s.flush(); s.connection().commit(); s.close(); } public void testFetchInitializedCollection() throws Exception { Session s = openSession(); Baz baz = new Baz(); Collection fooBag = new ArrayList(); fooBag.add( new Foo() ); fooBag.add( new Foo() ); baz.setFooBag(fooBag); s.save(baz); s.flush(); fooBag = baz.getFooBag(); s.find("from Baz baz left join fetch baz.fooBag"); assertTrue( fooBag==baz.getFooBag() ); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load( Baz.class, baz.getCode() ); Object bag = baz.getFooBag(); assertFalse( Hibernate.isInitialized(bag) ); s.find("from Baz baz left join fetch baz.fooBag"); assertTrue( bag==baz.getFooBag() ); assertTrue( baz.getFooBag().size()==2 ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); } public void testLateCollectionAdd() throws Exception { Session s = openSession(); Baz baz = new Baz(); List l = new ArrayList(); baz.setStringList(l); l.add("foo"); Serializable id = s.save(baz); l.add("bar"); s.flush(); l.add("baz"); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load(Baz.class, id); assertTrue( baz.getStringList().size()==3 && baz.getStringList().contains("bar") ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); } public void testUpdate() throws Exception { Session s = openSession(); Foo foo = new Foo(); s.save(foo); s.flush(); s.connection().commit(); s.close(); foo = (Foo) SerializationHelper.deserialize( SerializationHelper.serialize(foo) ); s = openSession(); FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() ); foo2.setString("dirty"); foo2.setBoolean( new Boolean(false) ); foo2.setBytes( new byte[] { 1,2,3} ); foo2.setDate(null); foo2.setShort( new Short("69") ); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo2.setString("dirty again"); s.update(foo2); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo2.setString("dirty again 2"); s.update(foo2); s.flush(); s.connection().commit(); s.close(); s = openSession(); Foo foo3 = new Foo(); s.load( foo3, foo.getKey() ); // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15 assertTrue( "update", foo2.equalsFoo(foo3) ); s.delete(foo3); s.delete("from Glarch"); s.flush(); s.connection().commit(); s.close(); } public void testListRemove() throws Exception { Session s = openSession(); Baz b = new Baz(); List stringList = new ArrayList(); List feeList = new ArrayList(); b.setFees(feeList); b.setStringList(stringList); feeList.add( new Fee() ); feeList.add( new Fee() ); feeList.add( new Fee() ); feeList.add( new Fee() ); stringList.add("foo"); stringList.add("bar"); stringList.add("baz"); stringList.add("glarch"); s.save(b); s.flush(); stringList.remove(1); feeList.remove(1); s.flush(); s.evict(b); s.refresh(b); assertTrue( b.getFees().size()==3 ); stringList = b.getStringList(); assertTrue( stringList.size()==3 && "baz".equals( stringList.get(1) ) && "foo".equals( stringList.get(0) ) ); s.delete(b); s.delete("from Fee"); s.flush(); s.connection().commit(); s.close(); } public void testFetchInitializedCollectionDupe() throws Exception { Session s = openSession(); Baz baz = new Baz(); Collection fooBag = new ArrayList(); fooBag.add( new Foo() ); fooBag.add( new Foo() ); baz.setFooBag(fooBag); s.save(baz); s.flush(); fooBag = baz.getFooBag(); s.find("from Baz baz left join fetch baz.fooBag"); assertTrue( Hibernate.isInitialized(fooBag) ); assertTrue( fooBag==baz.getFooBag() ); assertTrue( baz.getFooBag().size()==2 ); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load( Baz.class, baz.getCode() ); Object bag = baz.getFooBag(); assertFalse( Hibernate.isInitialized(bag) ); s.find("from Baz baz left join fetch baz.fooBag"); assertTrue( Hibernate.isInitialized(bag) ); assertTrue( bag==baz.getFooBag() ); assertTrue( baz.getFooBag().size()==2 ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); } public void testSortables() throws Exception { Session s = openSession(); Baz b = new Baz(); b.setName("name"); SortedSet ss = new TreeSet(); ss.add( new Sortable("foo") ); ss.add( new Sortable("bar") ); ss.add( new Sortable("baz") ); b.setSortablez(ss); s.save(b); s.flush(); s.connection().commit(); s.close(); s = openSession(); Criteria cr = s.createCriteria(Baz.class); if (getDialect() instanceof PostgreSQLDialect) cr.setFetchMode("topGlarchez", FetchMode.LAZY); List result = cr .addOrder( Order.asc("name") ) .list(); assertTrue( result.size()==1 ); b = (Baz) result.get(0); assertTrue( b.getSortablez().size()==3 ); assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" ); s.connection().commit(); s.close(); s = openSession(); result = s.createQuery("from Baz baz left join fetch baz.sortablez order by baz.name asc") .list(); b = (Baz) result.get(0); assertTrue( b.getSortablez().size()==3 ); assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" ); s.connection().commit(); s.close(); s = openSession(); result = s.createQuery("from Baz baz order by baz.name asc") .list(); b = (Baz) result.get(0); assertTrue( b.getSortablez().size()==3 ); assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" ); s.delete(b); s.flush(); s.connection().commit(); s.close(); } public void testFetchList() throws Exception { Session s = openSession(); Baz baz = new Baz(); s.save(baz); Foo foo = new Foo(); s.save(foo); Foo foo2 = new Foo(); s.save(foo2); s.flush(); List list = new ArrayList(); for ( int i=0; i<5; i++ ) { Fee fee = new Fee(); list.add(fee); } baz.setFees(list); list = s.find("from Foo foo, Baz baz left join fetch baz.fees"); assertTrue( Hibernate.isInitialized( ( (Baz) ( (Object[]) list.get(0) )[1] ).getFees() ) ); s.delete(foo); s.delete(foo2); s.delete(baz); s.flush(); s.connection().commit(); s.close(); } public void testBagOneToMany() throws Exception { Session s = openSession(); Baz baz = new Baz(); List list = new ArrayList(); baz.setBazez(list); list.add( new Baz() ); s.save(baz); s.flush(); list.add( new Baz() ); s.flush(); list.add( 0, new Baz() ); s.flush(); s.delete( list.remove(1) ); s.flush(); s.delete(baz); s.flush(); s.connection().commit();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -