⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parentchildtest.java

📁 人力资源管理系统主要包括:人员管理、招聘管理、培训管理、奖惩管理和薪金管理五大管理模块。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		Transaction t = s.beginTransaction();		Container c = new Container();		c.setManyToMany( new ArrayList() );		c.setBag( new ArrayList() );		Simple s1 = new Simple();		Simple s2 = new Simple();		s1.setCount(123); s2.setCount(654);		Contained c1 = new Contained();		c1.setBag( new ArrayList() );		c1.getBag().add(c);		c.getBag().add(c1);		c.getManyToMany().add(s1);		c.getManyToMany().add(s2);		Serializable cid = s.save(c); //s.save(c1);		s.save(s1, new Long(12) ); s.save(s2, new Long(-1) );		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.load(Container.class, cid);		assertTrue( c.getBag().size()==1 );		assertTrue( c.getManyToMany().size()==2 );		c1 = (Contained) c.getBag().iterator().next();		assertTrue( c.getBag().size()==1 );		c.getBag().remove(c1);		c1.getBag().remove(c);		assertTrue( c.getManyToMany().remove(0)!=null );		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.load(Container.class, cid);		assertTrue( c.getBag().size()==0 );		assertTrue( c.getManyToMany().size()==1 );		c1 = (Contained) s.load( Contained.class, new Long(c1.getId()) );		assertTrue( c1.getBag().size()==0 );		assertTrue( s.delete("from c in class ContainerX")==1 );		assertTrue( s.delete("from c in class Contained")==1 );		assertTrue( s.delete("from s in class Simple")==2 );		t.commit();		s.close();	}		public void testContainer() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Container c = new Container();		Simple x = new Simple(); x.setCount(123);		Simple y = new Simple(); y.setCount(456);		s.save( x, new Long(1) ); s.save( y, new Long(0) );		List o2m = new ArrayList();		o2m.add(x); o2m.add(null); o2m.add(y);		List m2m = new ArrayList();		m2m.add(x); m2m.add(null); m2m.add(y);		c.setOneToMany(o2m); c.setManyToMany(m2m);		List comps = new ArrayList();		Container.ContainerInnerClass ccic = new Container.ContainerInnerClass();		ccic.setName("foo");		ccic.setSimple(x);		comps.add(ccic);		comps.add(null);		ccic = new Container.ContainerInnerClass();		ccic.setName("bar");		ccic.setSimple(y);		comps.add(ccic);		HashSet compos = new HashSet();		compos.add(ccic);		c.setComposites(compos);		c.setComponents(comps);		One one = new One();		Many many = new Many();		HashSet manies = new HashSet();		manies.add(many);		one.setManies(manies);		many.setOne(one);		ccic.setMany(many);		ccic.setOne(one);		s.save(one);		s.save(many);		s.save(c);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		Integer count = (Integer) s.createQuery("select count(*) from ContainerX as c join c.components as ce join ce.simple as s where ce.name='foo'").uniqueResult();		assertTrue( count.intValue()==1 );		List res = s.find("select c, s from ContainerX as c join c.components as ce join ce.simple as s where ce.name='foo'");		assertTrue(res.size()==1);		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.load( Container.class, new Long( c.getId() ) );		System.out.println( c.getOneToMany() );		System.out.println( c.getManyToMany() );		System.out.println( c.getComponents() );		System.out.println( c.getComposites() );		ccic = (Container.ContainerInnerClass) c.getComponents().get(2);		assertTrue( ccic.getMany().getOne()==ccic.getOne() );		assertTrue( c.getComponents().size()==3 );		assertTrue( c.getComposites().size()==1 );		assertTrue( c.getOneToMany().size()==3 );		assertTrue( c.getManyToMany().size()==3 );		assertTrue( c.getOneToMany().get(0)!=null );		assertTrue( c.getOneToMany().get(2)!=null );		for ( int i=0; i<3; i++ ) {			assertTrue( c.getManyToMany().get(i) == c.getOneToMany().get(i) );		}		Object o1 = c.getOneToMany().get(0);		Object o2 = c.getOneToMany().remove(2);		c.getOneToMany().set(0, o2);		c.getOneToMany().set(1, o1);		o1 = c.getComponents().remove(2);		c.getComponents().set(0, o1);		c.getManyToMany().set( 0, c.getManyToMany().get(2) );		c.getComposites().add(o1);		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.load( Container.class, new Long( c.getId() ) );		System.out.println( c.getOneToMany() );		System.out.println( c.getManyToMany() );		System.out.println( c.getComponents() );		System.out.println( c.getComposites() );		assertTrue( c.getComponents().size()==1 ); //WAS: 2		assertTrue( c.getComposites().size()==2 );		assertTrue( c.getOneToMany().size()==2 );		assertTrue( c.getManyToMany().size()==3 );		assertTrue( c.getOneToMany().get(0)!=null );		assertTrue( c.getOneToMany().get(1)!=null );		( (Container.ContainerInnerClass) c.getComponents().get(0) ).setName("a different name");		( (Container.ContainerInnerClass) c.getComposites().iterator().next() ).setName("once again");		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.load( Container.class, new Long( c.getId() ) );		System.out.println( c.getOneToMany() );		System.out.println( c.getManyToMany() );		System.out.println( c.getComponents() );		System.out.println( c.getComposites() );		assertTrue( c.getComponents().size()==1 ); //WAS: 2		assertTrue( c.getComposites().size()==2 );		assertTrue( ( (Container.ContainerInnerClass) c.getComponents().get(0) ).getName().equals("a different name") );		Iterator iter = c.getComposites().iterator();		boolean found = false;		while ( iter.hasNext() ) {			if ( ( (Container.ContainerInnerClass) iter.next() ).getName().equals("once again") ) found = true;		}		assertTrue(found);		c.getOneToMany().clear();		c.getManyToMany().clear();		c.getComposites().clear();		c.getComponents().clear();		s.delete("from s in class Simple");		s.delete("from m in class Many");		s.delete("from o in class One");		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.load( Container.class, new Long( c.getId() ) );		assertTrue( c.getComponents().size()==0 );		assertTrue( c.getComposites().size()==0 );		assertTrue( c.getOneToMany().size()==0 );		assertTrue( c.getManyToMany().size()==0 );		s.delete(c);		t.commit();		s.close();	}		public void testCascadeCompositeElements() throws Exception {		Container c = new Container();		List list = new ArrayList();		c.setCascades(list);		Container.ContainerInnerClass cic = new Container.ContainerInnerClass();		cic.setMany( new Many() );		cic.setOne( new One() );		list.add(cic);		Session s = openSession();		s.save(c);		s.flush();		s.connection().commit();		s.close();		s=openSession();		c = (Container) s.iterate("from c in class ContainerX").next();		cic = (Container.ContainerInnerClass) c.getCascades().iterator().next();		assertTrue( cic.getMany()!=null && cic.getOne()!=null );		assertTrue( c.getCascades().size()==1 );		s.delete(c);		s.flush();		s.connection().commit();		s.close();		c = new Container();		s = openSession();		s.save(c);		list = new ArrayList();		c.setCascades(list);		cic = new Container.ContainerInnerClass();		cic.setMany( new Many() );		cic.setOne( new One() );		list.add(cic);		s.flush();		s.connection().commit();		s.close();		s=openSession();		c = (Container) s.iterate("from c in class ContainerX").next();		cic = (Container.ContainerInnerClass) c.getCascades().iterator().next();		assertTrue( cic.getMany()!=null && cic.getOne()!=null );		assertTrue( c.getCascades().size()==1 );		s.delete(c);		s.flush();		s.connection().commit();		s.close();	}		public void testBag() throws Exception {				if (getDialect() instanceof HSQLDialect) return;				Session s = openSession();		Transaction t = s.beginTransaction();		Container c = new Container();		Contained c1 = new Contained();		Contained c2 = new Contained();		c.setBag( new ArrayList() );		c.getBag().add(c1);		c.getBag().add(c2);		c1.getBag().add(c);		c2.getBag().add(c);		s.save(c);		c.getBag().add(c2);		c2.getBag().add(c);		c.getLazyBag().add(c1);		c1.getLazyBag().add(c);		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.find("from c in class ContainerX").get(0);		c.getLazyBag().size();		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.find("from c in class ContainerX").get(0);		Contained c3 = new Contained();		//c.getBag().add(c3);		//c3.getBag().add(c);		c.getLazyBag().add(c3);		c3.getLazyBag().add(c);		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.find("from c in class ContainerX").get(0);		Contained c4 = new Contained();		c.getLazyBag().add(c4);		c4.getLazyBag().add(c);		assertTrue( c.getLazyBag().size()==3 ); //forces initialization		//s.save(c4);		t.commit();		s.close();				s = openSession();		t = s.beginTransaction();		c = (Container) s.find("from c in class ContainerX").get(0);		Iterator i = c.getBag().iterator();		int j=0;		while ( i.hasNext() ) {			assertTrue( i.next()!=null );			j++;		}		assertTrue(j==3);		assertTrue( c.getLazyBag().size()==3 );		s.delete(c);		c.getBag().remove(c2);		Iterator iter = c.getBag().iterator();		j=0;		while ( iter.hasNext() ) {			j++;			s.delete( iter.next() );		}		assertTrue(j==2);		s.delete( s.load(Contained.class, new Long( c4.getId() ) ) );		s.delete( s.load(Contained.class, new Long( c3.getId() ) ) );		t.commit();		s.close();			}		public void testCircularCascade() throws Exception {		Session s = openSession();		Transaction tx = s.beginTransaction();		Circular c = new Circular();		c.setClazz(Circular.class);		c.setOther( new Circular() );		c.getOther().setOther( new Circular() );		c.getOther().getOther().setOther(c);		c.setAnyEntity( c.getOther() );		String id = (String) s.save(c);		tx.commit();		s.close();		s = openSession();		tx = s.beginTransaction();		c = (Circular) s.load(Circular.class, id);		c.getOther().getOther().setClazz(Foo.class);		tx.commit();		s.close();		c.getOther().setClazz(Qux.class);		s = openSession();		tx = s.beginTransaction();		s.saveOrUpdate(c);		tx.commit();		s.close();		c.getOther().getOther().setClazz(Bar.class);		s = openSession();		tx = s.beginTransaction();		s.saveOrUpdate(c);		tx.commit();		s.close();		s = openSession();		tx = s.beginTransaction();		c = (Circular) s.load(Circular.class, id);		assertTrue( c.getOther().getOther().getClazz()==Bar.class);		assertTrue( c.getOther().getClazz()==Qux.class);		assertTrue( c.getOther().getOther().getOther()==c);		assertTrue( c.getAnyEntity()==c.getOther() );		assertTrue( s.delete("from o in class Universe")==3 );		tx.commit();		s.close();	}		public void testDeleteEmpty() throws Exception {		Session s = openSession();		assertTrue( s.delete("from s in class Simple")==0 );		assertTrue( s.delete("from o in class Universe")==0 );		s.close();	}		public void testLocking() throws Exception {		Session s = openSession();		Transaction tx = s.beginTransaction();		Simple s1 = new Simple(); s1.setCount(1);		Simple s2 = new Simple(); s2.setCount(2);		Simple s3 = new Simple(); s3.setCount(3);		Simple s4 = new Simple(); s4.setCount(4);		s.save(s1, new Long(1) );		s.save(s2, new Long(2) );		s.save(s3, new Long(3) );		s.save(s4, new Long(4) );		assertTrue( s.getCurrentLockMode(s1)==LockMode.WRITE );		tx.commit();		s.close();				s = openSession();		tx = s.beginTransaction();		s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.NONE);		assertTrue( s.getCurrentLockMode(s1)==LockMode.READ || s.getCurrentLockMode(s1)==LockMode.NONE ); //depends if cache is enabled		s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.READ);		assertTrue( s.getCurrentLockMode(s2)==LockMode.READ );		s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.UPGRADE);		assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );		s4 = (Simple) s.load(Simple.class, new Long(4), LockMode.UPGRADE_NOWAIT);		assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );				s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.UPGRADE); //upgrade		assertTrue( s.getCurrentLockMode(s1)==LockMode.UPGRADE );		s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.NONE);		assertTrue( s.getCurrentLockMode(s2)==LockMode.READ );		s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.READ);		assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );		s4 = (Simple) s.load(Simple.class, new Long(4), LockMode.UPGRADE);		assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );				s.lock(s2, LockMode.UPGRADE); //upgrade		assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE );		s.lock(s3, LockMode.UPGRADE);		assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );		s.lock(s1, LockMode.UPGRADE_NOWAIT);		s.lock(s4, LockMode.NONE);		assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );				tx.commit();		tx = s.beginTransaction();				assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE );		assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE );		assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE );		assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );				s.lock(s1, LockMode.READ); //upgrade		assertTrue( s.getCurrentLockMode(s1)==LockMode.READ );		s.lock(s2, LockMode.UPGRADE); //upgrade		assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE );		s.lock(s3, LockMode.UPGRADE_NOWAIT); //upgrade		assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE_NOWAIT );		s.lock(s4, LockMode.NONE);		assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );				s4.setName("s4");		s.flush();		assertTrue( s.getCurrentLockMode(s4)==LockMode.WRITE );		tx.commit();				tx = s.beginTransaction();				assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE );		assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE );		assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE );		assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );				s.delete(s1); s.delete(s2); s.delete(s3); s.delete(s4);		tx.commit();		s.close();	}		public void testObjectType() throws Exception {		Session s = openSession();		Parent g = new Parent();		Foo foo = new Foo();		g.setAny(foo);		s.save(g);		s.save(foo);		s.flush();		s.connection().commit();		s.close();		s = openSession();		g = (Parent) s.load( Parent.class, new Long( g.getId() ) );		assertTrue( g.getAny()!=null && g.getAny() instanceof FooProxy );		s.delete( g.getAny() );		s.delete(g);		s.flush();		s.connection().commit();		s.close();	}		public static Test suite() {		return new TestSuite(ParentChildTest.class);	}		public String[] getMappings() {		return new String[] {			"ParentChild.hbm.xml",			"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",	 		"Simple.hbm.xml",	 		"Container.hbm.xml",	 		"Circular.hbm.xml",	 		"Stuff.hbm.xml"		};	}	public static void main(String[] args) throws Exception {		TestRunner.run( suite() );	}}											

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -