📄 foobartest.java
字号:
list = q.list(); assertTrue( ((Bar)list.get(0)).getX() == bar.getX()); q.setInteger("valueX", bar2.getX()+1); list = q.list(); assertTrue( ((Bar)list.get(0)).getX() == bar2.getX()); s.delete(baz); s.delete(bar2); s.flush(); s.connection().commit(); s.close(); } public void testVerifyParameter() throws HibernateException { Session s = openSession(); try { Query q = s.createQuery("select bar from Bar as bar where bar.x > :myX"); q.list(); fail("Should throw QueryException for missing myX"); } catch (QueryException iae) { // should happen } finally { s.close(); } s = openSession(); try { Query q = s.createQuery("select bar from Bar as bar where bar.x > ?"); q.list(); fail("Should throw QueryException for missing ?"); } catch (QueryException iae) { // should happen } finally { s.close(); } s = openSession(); try { Query q = s.createQuery("select bar from Bar as bar where bar.x > ? or bar.short = 1 or bar.string = 'ff ? bb'"); q.setInteger(0, 1); q.list(); } catch (QueryException iae) { fail("Should not throw QueryException for missing ?"); } finally { s.close(); } s = openSession(); try { Query q = s.createQuery("select bar from Bar as bar where bar.string = ' ? ' or bar.string = '?'"); q.list(); } catch (QueryException iae) { fail("Should not throw QueryException for ? in quotes"); } finally { s.close(); } s = openSession(); try { Query q = s.createQuery("select bar from Bar as bar where bar.string = ? or bar.string = ? or bar.string = ?"); q.setParameter(0, "bull"); q.setParameter(2, "shit"); q.list(); fail("should throw exception telling me i have not set parameter 1"); } catch (QueryException iae) { // should happen! } finally { s.close(); } } public void testDyna() throws Exception { Session s = openSession(); GlarchProxy g = new Glarch(); g.setName("G"); Serializable id = s.save(g); s.flush(); s.connection().commit(); s.close(); s = openSession(); g = (GlarchProxy) s.load(Glarch.class, id); assertTrue( g.getName().equals("G") ); assertTrue( g.getDynaBean().get("foo").equals("foo") && g.getDynaBean().get("bar").equals( new Integer(66) ) ); assertTrue( ! (g instanceof Glarch) ); g.getDynaBean().put("foo", "bar"); s.flush(); s.connection().commit(); s.close(); s = openSession(); g = (GlarchProxy) s.load(Glarch.class, id); assertTrue( g.getDynaBean().get("foo").equals("bar") && g.getDynaBean().get("bar").equals( new Integer(66) ) ); g.setDynaBean(null); s.flush(); s.connection().commit(); s.close(); s = openSession(); g = (GlarchProxy) s.load(Glarch.class, id); assertTrue( g.getDynaBean()==null ); s.delete(g); s.flush(); s.connection().commit(); s.close(); } public void testFindByCriteria() throws Exception { if (getDialect() instanceof DB2Dialect) return; Session s = openSession(); Foo f = new Foo(); s.save(f); s.flush(); List list = s.createCriteria(Foo.class) .add( Expression.eq( "integer", f.getInteger() ) ) .add( Expression.eqProperty("integer", "integer") ) .add( Expression.like( "string", f.getString().toUpperCase() ).ignoreCase() ) .add( Expression.in( "boolean", new Boolean[] { f.getBoolean(), f.getBoolean() } ) ) .setFetchMode("foo", FetchMode.EAGER) .setFetchMode("baz", FetchMode.LAZY) .setFetchMode("abstracts", FetchMode.EAGER) .list(); if ( ! (getDialect() instanceof HSQLDialect) ) assertTrue( list.size()==1 && list.get(0)==f ); list = s.createCriteria(Foo.class).add( Expression.disjunction() .add( Expression.eq( "integer", f.getInteger() ) ) .add( Expression.like( "string", f.getString() ) ) .add( Expression.eq( "boolean", f.getBoolean() ) ) ) .add( Expression.isNotNull("boolean") ) .list(); assertTrue( list.size()==1 && list.get(0)==f ); Foo example = new Foo(); example.setString("a STRing"); list = s.createCriteria(Foo.class).add( Example.create(example) .excludeZeroes() .ignoreCase() .excludeProperty("bool") .excludeProperty("char") .excludeProperty("yesno") ) .list(); assertTrue( "Example API without like did not work correctly, size was " + list.size(), list.size()==1 && list.get(0)==f ); example.setString("rin"); list = s.createCriteria(Foo.class).add( Example.create(example) .excludeZeroes() .enableLike(MatchMode.ANYWHERE) .excludeProperty("bool") .excludeProperty("char") .excludeProperty("yesno") ) .list(); assertTrue( "Example API without like did not work correctly, size was " + list.size(), list.size()==1 && list.get(0)==f ); list = s.createCriteria(Foo.class) .add( Expression.or( Expression.and( Expression.eq( "integer", f.getInteger() ), Expression.like( "string", f.getString() ) ), Expression.eq( "boolean", f.getBoolean() ) ) ) .list(); assertTrue( list.size()==1 && list.get(0)==f ); list = s.createCriteria(Foo.class) .setMaxResults(5) .addOrder( Order.asc("date") ) .list(); assertTrue( list.size()==1 && list.get(0)==f ); list = s.createCriteria(Foo.class).setMaxResults(0).list(); assertTrue( list.size()==0 ); list = s.createCriteria(Foo.class) .setFirstResult(1) .addOrder( Order.asc("date") ) .addOrder( Order.desc("string") ) .list(); assertTrue( list.size()==0 ); list = s.createCriteria(Foo.class) .setFetchMode("component.importantDates", FetchMode.EAGER) .list(); assertTrue( list.size()==3 ); list = s.createCriteria(Foo.class) .setFetchMode("component.importantDates", FetchMode.EAGER) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .list(); assertTrue( list.size()==1 ); f.setFoo( new Foo() ); s.save( f.getFoo() ); s.flush(); s.connection().commit(); s.close(); if (getDialect() instanceof HSQLDialect) { s = openSession(); s.delete("from Foo foo"); s.flush(); s.connection().commit(); s.close(); return; } s = openSession(); list = s.createCriteria(Foo.class) .add( Expression.eq( "integer", f.getInteger() ) ) .add( Expression.like( "string", f.getString() ) ) .add( Expression.in( "boolean", new Boolean[] { f.getBoolean(), f.getBoolean() } ) ) .add( Expression.isNotNull("foo") ) .setFetchMode("foo", FetchMode.EAGER) .setFetchMode("baz", FetchMode.LAZY) .setFetchMode("component.glarch", FetchMode.LAZY) .setFetchMode("foo.baz", FetchMode.LAZY) .setFetchMode("foo.component.glarch", FetchMode.LAZY) .list(); f = (Foo) list.get(0); assertTrue( Hibernate.isInitialized( f.getFoo() ) ); assertTrue( !Hibernate.isInitialized( f.getComponent().getGlarch() ) ); s.save( new Bar() ); list = s.createCriteria(Bar.class) .list(); assertTrue( list.size()==1 ); assertTrue( s.createCriteria(Foo.class).list().size()==3 ); s.delete( list.get(0) ); s.delete( f.getFoo() ); s.delete(f); s.flush(); s.connection().commit(); s.close(); } public void testAfterDelete() throws Exception { Session s = openSession(); Foo foo = new Foo(); s.save(foo); s.flush(); s.delete(foo); s.save(foo); s.delete(foo); s.flush(); s.connection().commit(); s.close(); } public void testCollectionWhere() throws Exception { Foo foo1 = new Foo(); Foo foo2 = new Foo(); Baz baz = new Baz(); Foo[] arr = new Foo[10]; arr[0] = foo1; arr[9] = foo2; Session s = openSession(); s.save(foo1); s.save(foo2); baz.setFooArray(arr); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load( Baz.class, baz.getCode() ); assertTrue( baz.getFooArray().length==1 ); assertTrue( s.find("from Baz baz, baz.fooArray foo").size()==1 ); assertTrue( s.find("from Foo foo").size()==2 ); assertTrue( s.filter( baz.getFooArray(), "" ).size()==1 ); //assertTrue( s.delete("from java.lang.Object o")==9 ); s.delete("from Foo foo"); s.delete(baz); int rows=s.connection().createStatement().executeUpdate( "delete from fooArray where id_='" + baz.getCode() + "' and i>=8" ); assertTrue(rows==1); s.flush(); s.connection().commit(); s.close(); } public void testComponentParent() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); BarProxy bar = new Bar(); bar.setBarComponent( new FooComponent() ); Baz baz = new Baz(); baz.setComponents( new FooComponent[] { new FooComponent(), new FooComponent() } ); s.save(bar); s.save(baz); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); bar = (BarProxy) s.load(Bar.class, bar.getKey()); s.load(baz, baz.getCode()); assertTrue( bar.getBarComponent().getParent()==bar ); assertTrue( baz.getComponents()[0].getBaz()==baz && baz.getComponents()[1].getBaz()==baz ); s.delete(baz); s.delete(bar); t.commit(); s.close(); } public void testCollectionCache() throws Exception { Session s = openSession(); Baz baz = new Baz(); baz.setDefaults(); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.load( Baz.class, baz.getCode() ); 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 testAssociationId() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Bar bar = new Bar(); String id = (String) s.save(bar); MoreStuff more = new MoreStuff(); more.setName("More Stuff"); more.setIntId(12); more.setStringId("id"); Stuff stuf = new Stuff(); stuf.setMoreStuff(more); more.setStuffs( new ArrayList() ); more.getStuffs().add(stuf); stuf.setFoo(bar); stuf.setId(1234); stuf.setProperty( TimeZone.getDefault() ); s.save(more); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); assertTrue( s.find( "from s in class Stuff where s.foo.id = ? and s.id.id = ? and s.moreStuff.id.intId = ? and s.moreStuff.id.stringId = ?", new Object[] { bar, new Long(1234), new Integer(12), "id" }, new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.INTEGER, Hibernate.STRING } ).size()==1 ); assertTrue( s.find( "from s in class Stuff where s.foo.id = ? and s.id.id = ? and s.moreStuff.name = ?", new Object[] { bar, new Long(1234), "More Stuff" }, new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.STRING } ).size()==1 ); s.find("from s in class Stuff where s.foo.string is not null"); assertTrue( s.find("from s in class Stuff where s.foo > '0' order by s.foo").size()==1 ); //s.createCriteria(Stuff.class).createCriteria("id.foo").add( Expression.isNull("foo") ).list(); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); FooProxy foo = (FooProxy) s.load(Foo.class, id); s.load(more, more); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Stuff stuff = new Stuff(); stuff.setFoo(foo); stuff.setId(1234); stuff.setMoreStuff(more); s.load(stuff, stuff); assertTrue( stuff.getProperty().equals( TimeZone.getDefault() ) ); assertTrue( stuff.getMoreStuff().getName().equals("More Stuff") ); s.delete("from ms in class MoreStuff"); s.delete("from foo in class Foo"); t.commit(); s.close(); } public void testC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -