📄 fumtest.java
字号:
vList = s.find("from fum in class Fum where fum.id.short = ?",new Short(fiShort),Hibernate.SHORT); assertTrue( "find by composite key query (find fi object)", vList.size() == 1 ); fi = (Fum)vList.get(0); assertTrue( "find by composite key query (check fi object)", fi.getId().getString().equals("fi") ); // Make sure we can return all of the objects by searching by the date id assertTrue( "find by composite key query with arguments", s.find("from fum in class Fum where fum.id.date <= ? and not fum.fum='FRIEND'",new Date(),Hibernate.DATE).size()==4 ); s.flush(); s.connection().commit(); s.close(); s = openSession(); assertTrue( s.iterate("select fum.id.short, fum.id.date, fum.id.string from fum in class Fum").hasNext() ); assertTrue( s.iterate("select fum.id from fum in class Fum").hasNext() ); Query qu = s.createQuery("select fum.fum, fum , fum.fum, fum.id.date from fum in class Fum"); Type[] types = qu.getReturnTypes(); assertTrue(types.length==4); for ( int k=0; k<types.length; k++) { assertTrue( types[k]!=null ); } assertTrue(types[0] instanceof StringType); assertTrue(types[1] instanceof EntityType); assertTrue(types[2] instanceof StringType); assertTrue(types[3] instanceof DateType); Iterator iter = qu.iterate(); int j = 0; while ( iter.hasNext() ) { j++; assertTrue( ( (Object[]) iter.next() )[1] instanceof Fum ); } assertTrue( "iterate on composite key", j==8 ); fum = (Fum) s.load( Fum.class, fum.getId() ); s.filter( fum.getQuxArray(), "where this.foo is null" ); s.filter( fum.getQuxArray(), "where this.foo.id = ?", "fooid", Hibernate.STRING ); Query f = s.createFilter( fum.getQuxArray(), "where this.foo.id = :fooId" ); f.setString("fooId", "abc"); assertFalse( f.iterate().hasNext() ); iter = s.iterate("from fum in class Fum where not fum.fum='FRIEND'"); int i = 0; while ( iter.hasNext() ) { fum = (Fum) iter.next(); //iter.remove(); s.delete(fum); i++; } assertTrue( "iterate on composite key", i==4 ); s.flush(); s.iterate("from fu in class Fum, fo in class Fum where fu.fo.id.string = fo.id.string and fo.fum is not null"); s.find("from Fumm f1 inner join f1.fum f2"); s.connection().commit(); s.close(); } public void testCompositeIDCollections() throws Exception { if ( getDialect() instanceof SQLServerDialect ) return; if ( getDialect() instanceof HSQLDialect ) return; Session s = openSession(); Fum fum1 = new Fum( fumKey("fum1") ); Fum fum2 = new Fum( fumKey("fum2") ); fum1.setFum("fee fo fi"); fum2.setFum("fee fo fi"); s.save(fum1); s.save(fum2); Qux q = new Qux(); s.save(q); Set set = new HashSet(); List list = new ArrayList(); set.add(fum1); set.add(fum2); list.add(fum1); q.setFums(set); q.setMoreFums(list); fum1.setQuxArray( new Qux[] {q} ); s.flush(); s.connection().commit(); s.close(); s = openSession(); q = (Qux) s.load( Qux.class, q.getKey() ); assertTrue( "collection of fums", q.getFums().size()==2 ); assertTrue( "collection of fums", q.getMoreFums().size()==1 ); assertTrue( "unkeyed composite id collection", ( (Fum) q.getMoreFums().get(0) ).getQuxArray()[0]==q ); Iterator iter = q.getFums().iterator(); iter.hasNext(); Fum f = (Fum) iter.next(); s.delete(f); iter.hasNext(); f = (Fum) iter.next(); s.delete(f); s.delete(q); s.flush(); s.connection().commit(); s.close(); } public void testDeleteOwner() throws Exception { if ( getDialect() instanceof SQLServerDialect ) return; Session s = openSession(); Qux q = new Qux(); s.save(q); Fum f1 = new Fum( fumKey("f1") ); Fum f2 = new Fum( fumKey("f2") ); Set set = new HashSet(); set.add(f1); set.add(f2); List list = new LinkedList(); list.add(f1); list.add(f2); f1.setFum("f1"); f2.setFum("f2"); q.setFums(set); q.setMoreFums(list); s.save(f1); s.save(f2); s.flush(); s.connection().commit(); s.close(); s = openSession(); q = (Qux) s.load( Qux.class, q.getKey(), LockMode.UPGRADE ); s.lock( q, LockMode.UPGRADE ); s.delete(q); s.flush(); s.connection().commit(); s.close(); s = openSession(); list = s.find("from fum in class Fum where not fum.fum='FRIEND'"); assertTrue( "deleted owner", list.size()==2 ); s.lock( list.get(0), LockMode.UPGRADE ); s.lock( list.get(1), LockMode.UPGRADE ); Iterator iter = list.iterator(); while ( iter.hasNext() ) { s.delete( iter.next() ); } s.flush(); s.connection().commit(); s.close(); } public void testCompositeIDs() throws Exception { Session s = openSession(); Fo fo = Fo.newFo(); Properties props = new Properties(); props.setProperty("foo", "bar"); props.setProperty("bar", "foo"); fo.setSerial(props); fo.setBuf( "abcdefghij1`23%$*^*$*\n\t".getBytes() ); s.save( fo, fumKey("an instance of fo") ); s.flush(); props.setProperty("x", "y"); s.flush(); s.connection().commit(); s.close(); s = openSession(); fo = (Fo) s.load( Fo.class, fumKey("an instance of fo") ); props = (Properties) fo.getSerial(); assertTrue( props.getProperty("foo").equals("bar") ); assertTrue( props.getProperty("x").equals("y") ); assertTrue( fo.getBuf()[0]=='a' ); fo.getBuf()[1]=(byte)126; s.flush(); s.connection().commit(); s.close(); s = openSession(); fo = (Fo) s.load( Fo.class, fumKey("an instance of fo") ); assertTrue( fo.getBuf()[1]==126 ); assertTrue( s.iterate("from fo in class Fo where fo.id.string like 'an instance of fo'").next()==fo ); s.delete(fo); s.flush(); try { s.save( Fo.newFo() ); assertTrue(false); } catch (Exception e) { //System.out.println( e.getMessage() ); } s.connection().commit(); s.close(); } public void testKeyManyToOne() throws Exception { Session s = openSession(); Inner sup = new Inner(); InnerKey sid = new InnerKey(); sup.setDudu("dudu"); sid.setAkey("a"); sid.setBkey("b"); sup.setId(sid); Middle m = new Middle(); MiddleKey mid = new MiddleKey(); mid.setOne("one"); mid.setTwo("two"); mid.setSup(sup); m.setId(mid); m.setBla("bla"); Outer d = new Outer(); OuterKey did = new OuterKey(); did.setMaster(m); did.setDetailId("detail"); d.setId(did); d.setBubu("bubu"); s.save(sup); s.save(m); s.save(d); s.flush(); s.connection().commit(); s.close(); s = openSession(); Inner in = (Inner) s.find("from Inner").get(0); assertTrue( in.getMiddles().size()==1 ); s.flush(); s.connection().commit(); s.close(); s = openSession(); assertTrue( s.find("from Inner _inner join _inner.middles middle").size()==1 ); s.flush(); s.connection().commit(); s.close(); s = openSession(); d = (Outer) s.load(Outer.class, did); assertTrue( d.getId().getMaster().getId().getSup().getDudu().equals("dudu") ); s.delete(d); s.delete( d.getId().getMaster() ); s.save( d.getId().getMaster() ); s.save(d); s.flush(); s.connection().commit(); s.close(); s = openSession(); d = (Outer) s.find("from Outer o where o.id.detailId = ?", d.getId().getDetailId(), Hibernate.STRING ).get(0); s.find("from Outer o where o.id.master.id.sup.dudu is not null"); s.find("from Outer o where o.id.master.id.sup.id.akey is not null"); if ( !(getDialect() instanceof HSQLDialect) ) s.find("from Inner i where i.backOut.id.master.id.sup.id.akey = i.id.bkey"); s.find("select o.id.master.id.sup.dudu from Outer o where o.id.master.id.sup.dudu is not null"); s.find("select o.id.master.id.sup.id.akey from Outer o where o.id.master.id.sup.id.akey is not null"); if ( !(getDialect() instanceof HSQLDialect) ) s.find("select i.backOut.id.master.id.sup.id.akey from Inner i where i.backOut.id.master.id.sup.id.akey = i.id.bkey"); s.find("from Outer o where o.id.master.bla = ''"); s.find("from Outer o where o.id.master.id.one = ''"); s.find("from Inner inn where inn.id.bkey is not null and inn.backOut.id.master.id.sup.id.akey > 'a'"); s.find("from Outer as o left join o.id.master m left join m.id.sup where o.bubu is not null"); s.find("from Outer as o left join o.id.master.id.sup s where o.bubu is not null"); s.find("from Outer as o left join o.id.master m left join o.id.master.id.sup s where o.bubu is not null"); s.delete(d); s.delete( d.getId().getMaster() ); s.delete( d.getId().getMaster().getId().getSup() ); s.flush(); s.connection().commit(); s.close(); } public void testCompositeKeyPathExpressions() throws Exception { Session s = openSession(); s.find("select fum1.fo from fum1 in class Fum where fum1.fo.fum is not null"); s.find("from fum1 in class Fum where fum1.fo.fum is not null order by fum1.fo.fum"); if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof PointbaseDialect) ) { s.find("from fum1 in class Fum where exists elements(fum1.friends)"); s.find("from fum1 in class Fum where size(fum1.friends) = 0"); } s.find("select fum1.friends.elements from fum1 in class Fum"); s.find("from fum1 in class Fum, fr in elements( fum1.friends )"); s.connection().commit(); s.close(); } public String[] getMappings() { return new String[] { "FooBar.hbm.xml", "Baz.hbm.xml", "Qux.hbm.xml", "Glarch.hbm.xml", "Fum.hbm.xml", "Fumm.hbm.xml", "Fo.hbm.xml", "One.hbm.xml", "Many.hbm.xml", "Immutable.hbm.xml", "Fee.hbm.xml", "Vetoer.hbm.xml", "Holder.hbm.xml", "Location.hbm.xml", "Stuff.hbm.xml", "Container.hbm.xml", "Simple.hbm.xml", "Middle.hbm.xml" }; } public static Test suite() { return new TestSuite(FumTest.class); } public static void main(String[] args) throws Exception { TestRunner.run( suite() ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -