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

📄 parentchildtest.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		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 ContainerX c").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 ContainerX c").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 ContainerX c").get(0);		c.getLazyBag().size();		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		c = (Container) s.find("from ContainerX c").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 ContainerX c").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 ContainerX c").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 Universe")==3 );		tx.commit();		s.close();	}	public void testDeleteEmpty() throws Exception {		Session s = openSession();		assertTrue( s.delete("from Simple")==0 );		assertTrue( s.delete("from 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.get(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 void testLoadAfterNonExists() throws HibernateException, SQLException {		Session session = openSession();		if ( (getDialect() instanceof MySQLDialect) ) {			session.connection().setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);		}		// First, prime the fixture session to think the entity does not exist		try {			session.load( Simple.class, new Long(-1) );			fail();		}		catch(ObjectNotFoundException onfe) {			// this is correct		}		// Next, lets create that entity under the covers		Session anotherSession = getSessions().openSession();		Simple myNewSimple = new Simple();		myNewSimple.setName("My under the radar Simple entity");		myNewSimple.setAddress("SessionCacheTest.testLoadAfterNonExists");		myNewSimple.setCount(1);		myNewSimple.setDate( new Date() );		myNewSimple.setPay( new Float(100000000) );		anotherSession.save( myNewSimple, new Long(-1) );		anotherSession.flush();		anotherSession.connection().commit();		anotherSession.close();		// Verify that the original session is still unable to see the new entry...		try {			session.load( Simple.class, new Long(-1) );			fail();		}		catch(ObjectNotFoundException onfe) {		}		// Now, lets clear the original session at which point it should be able to see		// the new entity		session.clear();		try {			Simple dummy = (Simple) session.load( Simple.class, new Long(-1) );			assertNotNull("Unable to locate entity Simple with id = -1", dummy);		}		catch(ObjectNotFoundException onfe) {			fail("Unable to locate entity Simple with id = -1");		}		session.connection().commit();		session.close();	}	public static Test suite() {		return new TestSuite(ParentChildTest.class);	}	public String[] getMappings() {		return new String[] {			"legacy/ParentChild.hbm.xml",			"legacy/FooBar.hbm.xml",		 	"legacy/Baz.hbm.xml",		 	"legacy/Qux.hbm.xml",		 	"legacy/Glarch.hbm.xml",		 	"legacy/Fum.hbm.xml",		 	"legacy/Fumm.hbm.xml",		 	"legacy/Fo.hbm.xml",		 	"legacy/One.hbm.xml",		 	"legacy/Many.hbm.xml",		 	"legacy/Immutable.hbm.xml",		 	"legacy/Fee.hbm.xml",		 	"legacy/Vetoer.hbm.xml",		 	"legacy/Holder.hbm.xml",		 	"legacy/Simple.hbm.xml",		 	"legacy/Container.hbm.xml",		 	"legacy/Circular.hbm.xml",		 	"legacy/Stuff.hbm.xml"		};	}	public static void main(String[] args) throws Exception {		TestRunner.run( suite() );	}}

⌨️ 快捷键说明

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