📄 foobartest.java
字号:
//$Id: FooBarTest.java,v 1.1.2.14 2003/11/29 08:05:30 oneovthafew Exp $package org.hibernate.test;import java.io.Serializable;import java.sql.Connection;import java.sql.Time;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Set;import java.util.SortedSet;import java.util.TimeZone;import java.util.TreeMap;import java.util.TreeSet;import junit.framework.Test;import junit.framework.TestSuite;import junit.textui.TestRunner;import net.sf.hibernate.Criteria;import net.sf.hibernate.Databinder;import net.sf.hibernate.FetchMode;import net.sf.hibernate.FlushMode;import net.sf.hibernate.Hibernate;import net.sf.hibernate.HibernateException;import net.sf.hibernate.LazyInitializationException;import net.sf.hibernate.LockMode;import net.sf.hibernate.ObjectDeletedException;import net.sf.hibernate.ObjectNotFoundException;import net.sf.hibernate.Query;import net.sf.hibernate.QueryException;import net.sf.hibernate.ScrollableResults;import net.sf.hibernate.Session;import net.sf.hibernate.Transaction;import net.sf.hibernate.cfg.Environment;import net.sf.hibernate.connection.ConnectionProvider;import net.sf.hibernate.connection.DriverManagerConnectionProvider;import net.sf.hibernate.dialect.DB2Dialect;import net.sf.hibernate.dialect.HSQLDialect;import net.sf.hibernate.dialect.InterbaseDialect;import net.sf.hibernate.dialect.MckoiDialect;import net.sf.hibernate.dialect.MySQLDialect;import net.sf.hibernate.dialect.OracleDialect;import net.sf.hibernate.dialect.PointbaseDialect;import net.sf.hibernate.dialect.PostgreSQLDialect;import net.sf.hibernate.dialect.SAPDBDialect;import net.sf.hibernate.dialect.SybaseDialect;import net.sf.hibernate.engine.SessionFactoryImplementor;import net.sf.hibernate.expression.Example;import net.sf.hibernate.expression.Expression;import net.sf.hibernate.expression.MatchMode;import net.sf.hibernate.expression.Order;import net.sf.hibernate.jmx.HibernateService;import net.sf.hibernate.proxy.HibernateProxy;import net.sf.hibernate.type.Type;import net.sf.hibernate.util.JoinedIterator;import net.sf.hibernate.util.SerializationHelper;public class FooBarTest extends TestCase { public FooBarTest(String arg) { super(arg); } public void testDereferenceLazyCollection() throws Exception { Session s = openSession(); Baz baz = new Baz(); baz.setFooSet( new HashSet() ); Foo foo = new Foo(); baz.getFooSet().add(foo); s.save(foo); s.save(baz); foo.setBytes( "foobar".getBytes() ); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo = (Foo) s.get( Foo.class, foo.getKey() ); assertTrue( Hibernate.isInitialized( foo.getBytes() ) ); assertTrue( foo.getBytes().length==6 ); baz = (Baz) s.get( Baz.class, baz.getCode() ); assertTrue( baz.getFooSet().size()==1 ); s.flush(); s.connection().commit(); s.close(); getSessions().evictCollection("org.hibernate.test.Baz.fooSet"); s = openSession(); baz = (Baz) s.get( Baz.class, baz.getCode() ); assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); baz.setFooSet(null); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo = (Foo) s.get( Foo.class, foo.getKey() ); assertTrue( foo.getBytes().length==6 ); baz = (Baz) s.get( Baz.class, baz.getCode() ); assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); assertTrue( baz.getFooSet().size()==0 ); s.delete(baz); s.delete(foo); s.flush(); s.connection().commit(); s.close(); } public void testMoveLazyCollection() throws Exception { Session s = openSession(); Baz baz = new Baz(); Baz baz2 = new Baz(); baz.setFooSet( new HashSet() ); Foo foo = new Foo(); baz.getFooSet().add(foo); s.save(foo); s.save(baz); s.save(baz2); foo.setBytes( "foobar".getBytes() ); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo = (Foo) s.get( Foo.class, foo.getKey() ); assertTrue( Hibernate.isInitialized( foo.getBytes() ) ); assertTrue( foo.getBytes().length==6 ); baz = (Baz) s.get( Baz.class, baz.getCode() ); assertTrue( baz.getFooSet().size()==1 ); s.flush(); s.connection().commit(); s.close(); getSessions().evictCollection("org.hibernate.test.Baz.fooSet"); s = openSession(); baz = (Baz) s.get( Baz.class, baz.getCode() ); assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); baz2 = (Baz) s.get( Baz.class, baz2.getCode() ); baz2.setFooSet( baz.getFooSet() ); baz.setFooSet(null); assertFalse( Hibernate.isInitialized( baz2.getFooSet() ) ); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo = (Foo) s.get( Foo.class, foo.getKey() ); assertTrue( foo.getBytes().length==6 ); baz = (Baz) s.get( Baz.class, baz.getCode() ); baz2 = (Baz) s.get( Baz.class, baz2.getCode() ); assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); assertTrue( baz.getFooSet().size()==0 ); assertTrue( Hibernate.isInitialized( baz2.getFooSet() ) ); //fooSet has batching enabled assertTrue( baz2.getFooSet().size()==1 ); s.delete(baz); s.delete(baz2); s.delete(foo); s.flush(); s.connection().commit(); s.close(); } public void testCriteriaCollection() throws Exception { Session s = openSession(); Baz baz = new Baz(); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); Baz b = (Baz) s.createCriteria(Baz.class).uniqueResult(); assertTrue( Hibernate.isInitialized( b.getTopGlarchez() ) ); assertTrue( b.getTopGlarchez().size()==0 ); s.delete(b); s.flush(); s.connection().commit(); s.close(); } public void testQuery() throws Exception { Session s = openSession(); Foo foo = new Foo(); s.save(foo); Foo foo2 = new Foo(); s.save(foo2); foo.setFoo(foo2); List list = s.find("from Foo foo inner join fetch foo.foo"); Foo foof = (Foo) list.get(0); assertTrue( Hibernate.isInitialized( foof.getFoo() ) ); list = s.find("from Baz baz left outer join fetch baz.fooToGlarch"); list = s.find( "select foo, bar from Foo foo left outer join foo.foo bar where foo = ?", foo, Hibernate.entity(Foo.class) ); Object[] row1 = (Object[]) list.get(0); assertTrue( row1[0]==foo && row1[1]==foo2 ); s.find("select foo.foo.foo.string from foo in class Foo where foo.foo = 'bar'"); s.find("select foo.foo.foo.foo.string from foo in class Foo where foo.foo.foo = 'bar'"); s.find("select foo.foo.foo.string from foo in class Foo where foo.foo.foo.foo.string = 'bar'"); if ( ! (getDialect() instanceof HSQLDialect) ) s.find("select foo.string from foo in class Foo where foo.foo.foo.foo = foo.foo.foo"); s.find("select foo.string from foo in class Foo where foo.foo.foo = 'bar' and foo.foo.foo.foo = 'baz'"); s.find("select foo.string from foo in class Foo where foo.foo.foo.foo.string = 'a' and foo.foo.string = 'b'"); s.find("from bar in class Bar, foo in elements(bar.baz.fooArray)"); //s.find("from Baz as baz where baz.topComponents[baz].name = 'bazzz'"); if (getDialect() instanceof DB2Dialect) { s.find("from foo in class Foo where lower( foo.foo.string ) = 'foo'"); s.find("from foo in class Foo where lower( (foo.foo.string || 'foo') || 'bar' ) = 'foo'"); s.find("from foo in class Foo where repeat( (foo.foo.string || 'foo') || 'bar', 2 ) = 'foo'"); s.find("from foo in class Bar where foo.foo.integer is not null and repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'"); s.find("from foo in class Bar where foo.foo.integer is not null or repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'"); } if (getDialect() instanceof SybaseDialect) { s.iterate("select baz from Baz as baz join baz.fooArray foo group by baz order by sum(foo.float)"); } s.find("from Foo as foo where foo.component.glarch.name is not null"); s.find("from Foo as foo left outer join foo.component.glarch as glarch where glarch.name = 'foo'"); list = s.find("from Foo"); assertTrue( list.size()==2 && list.get(0) instanceof FooProxy ); list = s.find("from Foo foo left outer join foo.foo"); assertTrue( list.size()==2 && ( (Object[]) list.get(0) )[0] instanceof FooProxy ); s.find("from Foo, Bar"); s.find("from Baz baz left join baz.fooToGlarch, Bar bar join bar.foo"); s.find("from Baz baz left join baz.fooToGlarch join baz.fooSet"); s.find("from Baz baz left join baz.fooToGlarch join fetch baz.fooSet foo left join fetch foo.foo"); /*Query q = s.createQuery("from foo in class Foo where foo.string = ? or foo.string = ? or foo.string in (:list)"); q.setString(0, "foo"); q.setString(1, "bar"); List plist = new ArrayList(); plist.add("baz"); q.setParameterList("list", plist); q.list();*/ list = s.find("from foo in class Foo where foo.string='osama bin laden' and foo.boolean = true order by foo.string asc, foo.component.count desc"); assertTrue( "empty query", list.size()==0 ); Iterator iter = s.iterate("from foo in class Foo where foo.string='osama bin laden' order by foo.string asc, foo.component.count desc"); assertTrue( "empty iterator", !iter.hasNext() ); list = s.find("select foo.foo from foo in class Foo"); assertTrue( "query", list.size()==1 ); assertTrue( "returned object", list.get(0)==foo.getFoo() ); foo.getFoo().setFoo(foo); foo.setString("fizard"); //The following test is disabled for databases with no subselects...also for Interbase (not sure why). if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) ) { // && !db.equals("weblogic") { if ( !( getDialect() instanceof InterbaseDialect ) ) { list = s.find("from foo in class Foo where ? = some foo.component.importantDates.elements", new Date(), Hibernate.DATE); assertTrue( "component query", list.size()==2 ); } list = s.find("from foo in class Foo where size(foo.component.importantDates) = 3"); //WAS: 4 assertTrue( "component query", list.size()==2 ); list = s.find("from foo in class Foo where 0 = size(foo.component.importantDates)"); assertTrue( "component query", list.size()==0 ); list = s.find("from foo in class Foo where exists elements(foo.component.importantDates)"); assertTrue( "component query", list.size()==2 ); s.find("from foo in class Foo where not exists (from bar in class Bar where bar.id = foo.id)"); s.find("select foo.foo from foo in class Foo where foo = some(select x from x in class Foo where x.long > foo.foo.long)"); s.find("select foo.foo from foo in class Foo where foo = some(from Foo x where (x.long > foo.foo.long))"); s.find("select foo.foo from foo in class Foo where foo.long = some( select max(x.long) from Foo x where (x.long > foo.foo.long) group by x.foo )"); s.find("from Foo foo where foo = some(select x from x in class Foo where x.long > foo.foo.long) and foo.foo.string='baz'"); s.find("from Foo foo where foo.foo.string='baz' and foo = some(select x from Foo x where x.long > foo.foo.long)"); s.find("from Foo foo where foo = some(select x from x in class Foo where x.long > foo.foo.long)"); s.iterate("select foo.string, foo.date, foo.foo.string, foo.id from foo in class Foo, baz in class Baz where foo in elements(baz.fooArray) and foo.string like 'foo'"); } list = s.find("from foo in class Foo where foo.component.count is null order by foo.component.count"); assertTrue( "component query", list.size()==0 ); list = s.find("from foo in class Foo where foo.component.name='foo'"); assertTrue( "component query", list.size()==2 ); list = s.find("select distinct foo.component.name, foo.component.name from foo in class Foo where foo.component.name='foo'"); assertTrue( "component query", list.size()==1 ); list = s.find("select distinct foo.component.name, foo.id from foo in class Foo where foo.component.name='foo'"); assertTrue( "component query", list.size()==2 ); list = s.find("select foo.foo from foo in class Foo"); assertTrue( "query", list.size()==2 ); list = s.find("from foo in class Foo where foo.id=?", foo.getKey(), Hibernate.STRING); assertTrue( "id query", list.size()==1 ); list = s.find("from foo in class Foo where foo.key=?", foo.getKey(), Hibernate.STRING); assertTrue( "named id query", list.size()==1 ); assertTrue( "id query", list.get(0)==foo ); list = s.find("select foo.foo from foo in class Foo where foo.string='fizard'"); assertTrue( "query", list.size()==1 ); assertTrue( "returned object", list.get(0)==foo.getFoo() ); list = s.find("from foo in class Foo where foo.component.subcomponent.name='bar'"); assertTrue( "components of components", list.size()==2 ); list = s.find("select foo.foo from foo in class Foo where foo.foo.id=?", foo.getFoo().getKey(), Hibernate.STRING); assertTrue( "by id query", list.size()==1 ); assertTrue( "by id returned object", list.get(0)==foo.getFoo() ); s.find( "from foo in class Foo where foo.foo = ?", foo.getFoo(), Hibernate.entity(Foo.class) ); assertTrue( !s.iterate("from bar in class Bar where bar.string='a string' or bar.string='a string'").hasNext() ); iter = s.iterate( "select foo.component.name, foo.component.importantDates.elements from foo in class Foo where foo.foo.id=?", foo.getFoo().getKey(), Hibernate.STRING ); int i=0; while ( iter.hasNext() ) { i++; Object[] row = (Object[]) iter.next(); assertTrue( row[0] instanceof String && ( row[1]==null || row[1] instanceof Date ) ); } assertTrue(i==3); //WAS: 4 iter = s.iterate( "select max(foo.component.importantDates.elements) from foo in class Foo group by foo.id" ); assertTrue( iter.next() instanceof Date ); list = s.find( "select foo.foo.foo.foo from foo in class Foo, foo2 in class Foo where" + " foo = foo2.foo and not not ( not foo.string='fizard' )" + " and foo2.string between 'a' and (foo.foo.string)" + ( ( getDialect() instanceof HSQLDialect || getDialect() instanceof InterbaseDialect )? " and ( foo2.string in ( 'fiz', 'blah') or 1=1 )" : " and ( foo2.string in ( 'fiz', 'blah', foo.foo.string, foo.string, foo2.string ) )" ) ); assertTrue( "complex query", list.size()==1 ); assertTrue( "returned object", list.get(0)==foo ); foo.setString("from BoogieDown -tinsel town =!@#$^&*())"); list = s.find("from foo in class Foo where foo.string='from BoogieDown -tinsel town =!@#$^&*())'"); assertTrue( "single quotes", list.size()==1 ); list = s.find("from foo in class Foo where not foo.string='foo''bar'"); assertTrue( "single quotes", list.size()==2 ); list = s.find("from foo in class Foo where foo.component.glarch.next is null"); assertTrue( "query association in component", list.size()==2 ); Bar bar = new Bar(); Baz baz = new Baz(); baz.setDefaults(); bar.setBaz(baz); baz.setManyToAny( new ArrayList() ); baz.getManyToAny().add(bar); baz.getManyToAny().add(foo); s.save(bar); s.save(baz); list = s.find(" from bar in class Bar where bar.baz.count=667 and bar.baz.count!=123 and not bar.baz.name='1-E-1'"); assertTrue( "query many-to-one", list.size()==1 ); list = s.find(" from i in class Bar where i.baz.name='Bazza'"); assertTrue( "query many-to-one", list.size()==1 ); Iterator rs = s.iterate("select count(distinct foo.foo) from foo in class Foo"); assertTrue( "count", ( (Integer) rs.next() ).intValue()==2 ); assertTrue( !rs.hasNext() ); rs = s.iterate("select count(foo.foo.boolean) from foo in class Foo"); assertTrue( "count", ( (Integer) rs.next() ).intValue()==2 ); assertTrue( !rs.hasNext() ); rs = s.iterate("select count(*), foo.int from foo in class Foo group by foo.int"); assertTrue( "count(*) group by", ( (Object[]) rs.next() )[0].equals( new Integer(3) ) ); assertTrue( !rs.hasNext() ); rs = s.iterate("select sum(foo.foo.int) from foo in class Foo"); assertTrue( "sum", ( (Integer) rs.next() ).intValue()==4 ); assertTrue( !rs.hasNext() ); rs = s.iterate("select count(foo) from foo in class Foo where foo.id=?", foo.getKey(), Hibernate.STRING); assertTrue( "id query count", ( (Integer) rs.next() ).intValue()==1 ); assertTrue( !rs.hasNext() ); list = s.find( "from foo in class Foo where foo.boolean = ?", new Boolean(true), Hibernate.BOOLEAN ); s.find("select new Foo(fo.x) from Fo fo"); list = s.find("select foo.long, foo.component.name, foo, foo.foo from foo in class Foo"); rs = list.iterator(); int count=0; while ( rs.hasNext() ) { count++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -