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

📄 parentchildtest.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		baz = (Baz) s.createCriteria(Baz.class).uniqueResult();		assertTrue( s.createFilter( baz.getMoreParts(), "" ).list().size()==2 );		//assertTrue( baz.getParts().size()==1 );		s.delete( s.get( Part.class, p1.getId() ));		s.delete( s.get( Part.class, p2.getId() ));		s.delete(baz);		t.commit();		s.close();	}	public void testCollectionQuery() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Simple s1 = new Simple();		s1.setName("s");		s1.setCount(0);		Simple s2 = new Simple();		s2.setCount(2);		Simple s3 = new Simple();		s3.setCount(3);		s.save( s1, new Long(1) ); s.save( s2, new Long(2) ); s.save( s3, new Long(3) );		Container c = new Container();		Contained cd = new Contained();		List bag = new ArrayList();		bag.add(cd);		c.setBag(bag);		List l = new ArrayList();		l.add(s1);		l.add(s3);		l.add(s2);		c.setOneToMany(l);		l = new ArrayList();		l.add(s1);		l.add(null);		l.add(s2);		c.setManyToMany(l);		s.save(c);		Container cx = new Container();		s.save(cx);		Simple sx = new Simple();		sx.setCount(5);		sx.setName("s");		s.save( sx, new Long(5) );		assertTrue(			s.find("select c from ContainerX c, Simple s where c.oneToMany[2] = s")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c, Simple s where c.manyToMany[2] = s")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c, Simple s where s = c.oneToMany[2]")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c, Simple s where s = c.manyToMany[2]")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c where c.oneToMany[0].name = 's'")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c where c.manyToMany[0].name = 's'")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c where 's' = c.oneToMany[2 - 2].name")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c where 's' = c.manyToMany[(3+1)/4-1].name")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c where c.oneToMany[ c.manyToMany[0].count ].name = 's'")			.size() == 1		);		assertTrue(			s.find("select c from ContainerX c where c.manyToMany[ c.oneToMany[0].count ].name = 's'")			.size() == 1		);		if ( ! ( getDialect() instanceof MySQLDialect ) && !(getDialect() instanceof org.hibernate.dialect.TimesTenDialect) ) {			assertTrue(				s.find("select c from ContainerX c where c.manyToMany[ maxindex(c.manyToMany) ].count = 2")				.size() == 1			);		}		assertTrue( s.contains(cd) );		if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) )  {			s.filter( c.getBag(), "where 0 in elements(this.bag)" );			s.filter( c.getBag(), "where 0 in elements(this.lazyBag)" );		}		s.find("select count(comp.name) from ContainerX c join c.components comp");		s.delete(cd);		s.delete(c);		s.delete(s1);		s.delete(s2);		s.delete(s3);		s.delete(cx);		s.delete(sx);		t.commit();		s.close();	}	public void testParentChild() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Parent p = new Parent();		Child c = new Child();		c.setParent(p);		p.setChild(c);		s.save(p);		s.save(c);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		c = (Child) s.load( Child.class, new Long( c.getId() ) );		p = c.getParent();		assertTrue( "1-1 parent", p!=null );		c.setCount(32);		p.setCount(66);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		c = (Child) s.load( Child.class, new Long( c.getId() ) );		p = c.getParent();		assertTrue( "1-1 update", p.getCount()==66 );		assertTrue( "1-1 update", c.getCount()==32 );		assertTrue(			"1-1 query",			s.find("from Child c where c.parent.count=66").size()==1		);		assertTrue(			"1-1 query",			( (Object[]) s.find("from Parent p join p.child c where p.count=66").get(0) ).length==2		);		s.find("select c, c.parent from Child c order by c.parent.count");		s.find("select c, c.parent from Child c where c.parent.count=66 order by c.parent.count");		s.iterate("select c, c.parent, c.parent.count from Child c order by c.parent.count");		assertTrue(			"1-1 query",			s.find("FROM Parent AS p WHERE p.count = ?", new Integer(66), Hibernate.INTEGER).size()==1		);		s.delete(c); s.delete(p);		t.commit();		s.close();	}	public void testParentNullChild() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Parent p = new Parent();		s.save(p);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		p = (Parent) s.load( Parent.class, new Long( p.getId() ) );		assertTrue( p.getChild()==null );		p.setCount(66);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		p = (Parent) s.load( Parent.class, new Long( p.getId() ) );		assertTrue( "null 1-1 update", p.getCount()==66 );		assertTrue( p.getChild()==null );		s.delete(p);		t.commit();		s.close();	}	public void testManyToMany() throws Exception {		if (getDialect() instanceof HSQLDialect) return;		Session s = openSession();		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 ContainerX c")==1 );		assertTrue( s.delete("from Contained")==1 );		assertTrue( s.delete("from 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 Simple");		s.delete("from Many");		s.delete("from 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 );

⌨️ 快捷键说明

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