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

📄 multitabletest.java

📁 好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.2.0
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		s.close();

		s = openSession();
		t = s.beginTransaction();
		multi = (Multi) s.load(Top.class, mid, LockMode.UPGRADE);
		simp = (Top) s.load(Top.class, sid);
		s.lock(simp, LockMode.UPGRADE_NOWAIT);
		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();
		s.update(multi, mid);
		s.delete(multi);
		assertTrue( s.delete("from Top")==2);
		t.commit();
		s.close();

	}

	public void testMultiTableGeneratedId() throws Exception {

		Session s = openSession();
		Transaction t = s.beginTransaction();
		Multi multi = new Multi();
		multi.setExtraProp("extra");
		//multi.setCount(666);
		multi.setName("name");
		Top simp = new Top();
		simp.setDate( new Date() );
		simp.setName("simp");
		//simp.setCount(132);
		Serializable multiId = s.save( multi );
		Serializable simpId = s.save( simp );
		SubMulti sm = new SubMulti();
		sm.setAmount(66.5f);
		Serializable smId = s.save( sm );
		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();
		multi.setExtraProp( multi.getExtraProp() + "2" );
		//multi.setCount( multi.getCount() + 1 );
		multi.setName("new name");
		s.update( multi, multiId );
		simp.setName("new name");
		s.update( simp, simpId );
		sm.setAmount(456.7f);
		s.update( sm, smId );
		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();
		multi = (Multi) s.load( Multi.class, multiId );
		assertTrue( multi.getExtraProp().equals("extra2") );
		multi.setExtraProp( multi.getExtraProp() + "3" );
		//multi.setCount( multi.getCount() + 1 );
		assertTrue( multi.getName().equals("new name") );
		multi.setName("newer name");
		sm = (SubMulti) s.load( SubMulti.class, smId );
		assertTrue( sm.getAmount()==456.7f );
		sm.setAmount(23423f);
		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();
		multi = (Multi) s.load( Top.class, multiId );
		simp = (Top) s.load( Top.class, simpId );
		assertTrue( ! (simp instanceof Multi) );
		assertTrue( multi.getExtraProp().equals("extra23") );
		//multi.setCount( multi.getCount() + 1 );
		assertTrue( multi.getName().equals("newer name") );
		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();
		Iterator iter = s.iterate("select\n\nt from Top t where t.count>0");
		boolean foundSimp = false;
		boolean foundMulti = false;
		boolean foundSubMulti = false;
		while ( iter.hasNext() ) {
			Object o = iter.next();
			if ( ( o instanceof Top ) && !( o instanceof Multi) ) foundSimp = true;
			if ( o instanceof Multi && !(o instanceof SubMulti) ) foundMulti = true;
			if ( o instanceof SubMulti ) foundSubMulti = true;
		}
		assertTrue( foundSimp&&foundMulti&&foundSubMulti );
		s.find("from Multi m where m.count>0 and m.extraProp is not null");
		s.find("from Top m where m.count>0 and m.name is not null");
		s.find("from Lower m where m.other is not null");
		s.find("from Multi m where m.other.id = 1");
		s.find("from SubMulti m where m.amount > 0.0");

		assertTrue(
			s.find("from Multi").size()==2
		);
		/*assertTrue(
			s.find("from m in class Multi where m.class = Multi").size()==1
		);*/
		assertTrue(
			s.find("from Top").size()==3
		);
		assertTrue(
			s.find("from Lower").size()==0
		);
		assertTrue(
			s.find("from SubMulti").size()==1
		);

		s.find("from Lower ls join ls.bag s where s.id is not null");
		if ( !(getDialect() instanceof MySQLDialect) ) s.find("from SubMulti sm where exists elements(sm.children)");

		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();
		multi = (Multi) s.load( Top.class, multiId, LockMode.UPGRADE );
		simp = (Top) s.load( Top.class, simpId );
		s.lock(simp, LockMode.UPGRADE_NOWAIT);
		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();
		s.update( multi, multiId );
		s.delete(multi);
		assertTrue( s.delete("from Top")==2);
		t.commit();
		s.close();

	}

	public void testMultiTableCollections() throws Exception {

		Session s = openSession();
		Transaction t = s.beginTransaction();
		assertTrue( s.find("from Top").size()==0 );
		Multi multi = new Multi();
		multi.setExtraProp("extra");
		//multi.setCount(666);
		multi.setName("name");
		Top simp = new Top();
		simp.setDate( new Date() );
		simp.setName("simp");
		//simp.setCount(132);
		Serializable mid;
		Serializable sid;
		if ( getDialect() instanceof SybaseDialect ) {
			mid = s.save(multi);
			sid = s.save(simp);
		}
		else {
			mid = new Long(123);
			sid = new Long(1234);
			s.save(multi, mid);
			s.save(simp, sid);
		}
		Lower ls = new Lower();
		ls.setOther(ls);
		ls.setAnother(ls);
		ls.setYetanother(ls);
		ls.setName("Less Simple");
		Set set = new HashSet();
		ls.setSet(set);
		set.add(multi);
		set.add(simp);
		Serializable id;
		if ( getDialect() instanceof SybaseDialect ) {
			id = s.save(ls);
		}
		else {
			id = new Long(2);
			s.save( ls, new Long(2) );
		}
		t.commit();
		s.close();
		assertTrue( ls.getOther()==ls && ls.getAnother()==ls && ls.getYetanother()==ls );

		s = openSession();
		t = s.beginTransaction();
		ls = (Lower) s.load(Lower.class, id);
		assertTrue( ls.getOther()==ls && ls.getAnother()==ls && ls.getYetanother()==ls );
		assertTrue( ls.getSet().size()==2 );
		Iterator iter = ls.getSet().iterator();
		int foundMulti = 0;
		int foundSimple = 0;
		while ( iter.hasNext() ) {
			Object o = iter.next();
			if ( o instanceof Top ) foundSimple++;
			if ( o instanceof Multi ) foundMulti++;
		}
		assertTrue( foundSimple==2 && foundMulti==1 );
		assertTrue( s.delete("from Top")==3 );
		t.commit();
		s.close();
	}

	public void testMultiTableManyToOne() throws Exception {

		Session s = openSession();
		Transaction t = s.beginTransaction();
		assertTrue( s.find("from Top").size()==0 );
		Multi multi = new Multi();
		multi.setExtraProp("extra");
		//multi.setCount(666);
		multi.setName("name");
		Top simp = new Top();
		simp.setDate( new Date() );
		simp.setName("simp");
		//simp.setCount(132);
		Serializable mid;
		if ( getDialect() instanceof SybaseDialect ) {
			mid = s.save(multi);
		}
		else {
			mid = new Long(123);
			s.save(multi, mid);
		}
		Lower ls = new Lower();
		ls.setOther(ls);
		ls.setAnother(multi);
		ls.setYetanother(ls);
		ls.setName("Less Simple");
		Serializable id;
		if ( getDialect() instanceof SybaseDialect ) {
			id = s.save(ls);
		}
		else {
			id = new Long(2);
			s.save( ls, new Long(2) );
		}
		t.commit();
		s.close();
		assertTrue( ls.getOther()==ls && ls.getAnother()==multi && ls.getYetanother()==ls );

		s = openSession();
		t = s.beginTransaction();
		ls = (Lower) s.load(Lower.class, id);
		assertTrue( ls.getOther()==ls && ls.getYetanother()==ls );
		assertTrue( ls.getAnother().getName().equals("name") && ls.getAnother() instanceof Multi );
		s.delete(ls);
		s.delete( ls.getAnother() );
		t.commit();
		s.close();
	}

	public void testMultiTableNativeId() throws Exception {
		Session s = openSession();
		Transaction t = s.beginTransaction();
		Multi multi = new Multi();
		multi.setExtraProp("extra");
		Long id = (Long) s.save(multi);
		assertTrue( id!=null );
		s.delete(multi);
		t.commit();
		s.close();
	}

	public void testCollection() throws Exception {
		Session s = openSession();
		Transaction t = s.beginTransaction();
		Multi multi1 = new Multi();
		multi1.setExtraProp("extra1");
		Multi multi2 = new Multi();
		multi2.setExtraProp("extra2");
		Po po = new Po();
		multi1.setPo(po); multi2.setPo(po);
		po.setSet( new HashSet() );
		po.getSet().add(multi1);
		po.getSet().add(multi2);
		po.setList( new ArrayList() );
		//po.getList().add(null);
		po.getList().add( new SubMulti() );
		Serializable id = s.save(po);
		assertTrue( id!=null );
		t.commit();
		s.close();
		s = openSession();
		t = s.beginTransaction();
		po = (Po) s.load(Po.class, id);
		assertTrue( po.getSet().size()==2 );
		assertTrue( po.getList().size()==1 );
		s.delete(po);
		assertTrue( s.find("from Top").size()==0 );
		t.commit();
		s.close();
	}

	public void testOneToOne() throws Exception {
		Session s = openSession();
		Lower ls = new Lower();
		Serializable id = s.save(ls);
		s.flush();
		s.connection().commit();
		s.close();
		s = openSession();
		s.load(Lower.class, id);
		s.connection().commit();
		s.close();
		s = openSession();
		s.delete( s.load(Lower.class, id) );
		s.flush();
		s.connection().commit();
		s.close();
	}

	public void testCollectionPointer() throws Exception {
		Session sess = openSession();
		Lower ls = new Lower();
		List list = new ArrayList();
		ls.setBag(list);
		Top s = new Top();
		Serializable id = sess.save(ls);
		sess.save(s);
		sess.flush();
		list.add(s);
		sess.flush();
		sess.connection().commit();
		sess.close();

		sess = openSession();
		ls = (Lower) sess.load(Lower.class, id);
		assertTrue( ls.getBag().size()==1 );
		sess.delete("from java.lang.Object");
		sess.flush();
		sess.connection().commit();
		sess.close();
	}

	public String[] getMappings() {
		return new String[] { "legacy/Multi.hbm.xml", "legacy/MultiExtends.hbm.xml" };
	}

	public static Test suite() {
		return new TestSuite(MultiTableTest.class);
	}

	public static void main(String[] args) throws Exception {
		TestRunner.run( suite() );
	}
}

⌨️ 快捷键说明

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