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

📄 multitabletest.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//$Id: MultiTableTest.java,v 1.7 2005/02/23 03:38:47 oneovthafew Exp $package org.hibernate.test.legacy;import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import junit.framework.Test;import junit.framework.TestSuite;import junit.textui.TestRunner;import org.hibernate.Criteria;import org.hibernate.FetchMode;import org.hibernate.LockMode;import org.hibernate.classic.Session;import org.hibernate.criterion.Expression;import org.hibernate.Transaction;import org.hibernate.dialect.HSQLDialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.dialect.SybaseDialect;import org.hibernate.test.TestCase;public class MultiTableTest extends TestCase {	public MultiTableTest(String arg0) {		super(arg0);	}	public void testCriteria() throws Exception {		Session s = openSession();		Lower l = new Lower();		s.save(l);		assertTrue( l==s.createCriteria(Top.class).uniqueResult() );		s.delete(l);		s.flush();		Criteria c = s.createCriteria(Lower.class);		c.createCriteria("yetanother")			.add( Expression.isNotNull("id") )			.createCriteria("another");		c.createCriteria("another").add( Expression.isNotNull("id") );		c.list();		s.connection().commit();		s.close();	}	public void testFetchOneToMany() throws Exception {		Session s = openSession();		s.createCriteria(Po.class).setFetchMode("set", FetchMode.EAGER).list();		s.createCriteria(Po.class).setFetchMode("list", FetchMode.EAGER).list();		s.connection().commit();		s.close();	}	public void testNarrow() throws Exception {		Session s = openSession();		s.find("from Po po, Lower low where low.mypo = po");		s.find("from Po po join po.set as sm where sm.amount > 0");		s.find("from Po po join po.top as low where low.foo = 'po'");		s.connection().commit();		s.close();	}	public void testJoins() throws Exception {		Session s = openSession();		s.find("from Lower l join l.yetanother l2 where lower(l2.name) > 'a'");		s.find("from Lower l where lower(l.yetanother.top.name) > 'a'");		s.find("from SubMulti sm join sm.children smc where smc.name > 'a'");		s.find("select s, ya from Lower s join s.yetanother ya");		s.find("from Lower s1 join s1.bag s2");		s.find("from Lower s1 left join s1.bag s2");		s.find("select s, a from Lower s join s.another a");		s.find("select s, a from Lower s left join s.another a");		s.find("from Top s, Lower ls");		s.find("from Lower ls join ls.set s where s.name > 'a'");		s.find("from Po po join po.list sm where sm.name > 'a'");		s.find("from Lower ls inner join ls.another s where s.name is not null");		s.find("from Lower ls where ls.other.another.name is not null");		s.find("from Multi m where m.derived like 'F%'");		s.find("from SubMulti m where m.derived like 'F%'");		s.connection().commit();		s.close();	}	public void testSubclassCollection() throws Exception {		//if ( getDialect() instanceof HSQLDialect ) return; //TODO: figure out why!?		Session s = openSession();		SubMulti sm = new SubMulti();		SubMulti sm1 = new SubMulti();		SubMulti sm2 = new SubMulti();		ArrayList list = new ArrayList();		ArrayList anotherList = new ArrayList();		sm.setChildren(list);		sm.setMoreChildren(anotherList);		sm.setExtraProp("foo");		list.add(sm1);		list.add(sm2);		anotherList.add(sm1);		anotherList.add(sm2);		sm1.setParent(sm);		sm2.setParent(sm);		Serializable id = s.save(sm);		s.save(sm1);		s.save(sm2);		s.flush();		s.connection().commit();		s.close();		getSessions().evict(SubMulti.class);		s = openSession();		s.connection().createStatement().executeQuery(			"select * from leafsubsubclass sm, nonleafsubclass m, rootclass s where sm.sid=m.sid and sm.sid=s.id1_ and sm.sid=1"		).next();		assertTrue( s.find("select s from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==2 );		s.find("select c from SubMulti sm join sm.children c");		assertTrue( s.find("select elements(sm.children) from SubMulti as sm").size()==2 );		assertTrue( s.find("select distinct sm from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==1 );		sm = (SubMulti) s.load(SubMulti.class, id);		assertTrue( sm.getChildren().size()==2 );		assertEquals(			s.filter( sm.getMoreChildren(), "select count(*) where this.amount>-1 and this.name is null" ).iterator().next(),			new Integer(2)		);		assertEquals( "FOO", sm.getDerived() );		assertSame(			s.iterate("select distinct s from SubMulti s where s.moreChildren[1].amount < 1.0").next(),			sm		);		assertTrue( sm.getMoreChildren().size()==2 );		s.delete(sm);		Iterator iter = sm.getChildren().iterator();		while ( iter.hasNext() ) s.delete( iter.next() );		s.flush();		s.connection().commit();		s.close();	}	public void testCollectionOnly() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Mono m = new Mono();		Long id = (Long) s.save(m);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		s.update(m, id);		s.flush();		m.setAddress("foo bar");		s.flush();		s.delete(m);		t.commit();		s.close();	}	public void testQueries() throws Exception {		Session s = openSession();		Long id = new Long(1);		if (getDialect() instanceof SybaseDialect) {			id = (Long) s.save( new TrivialClass() );		}		else{			s.save( new TrivialClass(), id );		}		s.flush();		s.connection().commit();		s.close();		s = openSession();		TrivialClass tc = (TrivialClass) s.load(TrivialClass.class, id);		s.find("from TrivialClass s where s.id = 2");		s.find("select t.count from Top t");		s.find("from Lower s where s.another.name='name'");		s.find("from Lower s where s.yetanother.name='name'");		s.find("from Lower s where s.yetanother.name='name' and s.yetanother.foo is null");		s.find("from Top s where s.count=1");		s.find("select s.count from Top s, Lower ls where ls.another=s");		s.find("select elements(ls.bag), elements(ls.set) from Lower ls");		s.iterate("from Lower");		s.iterate("from Top");		s.delete(tc);		s.flush();		s.connection().commit();		s.close();	}	public void testConstraints() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		SubMulti sm = new SubMulti();		sm.setAmount(66.5f);		if ( getDialect() instanceof SybaseDialect ) {			s.save(sm);		}		else {			s.save( sm, new Long(2) );		}		t.commit();		s.close();		s = openSession();		s.delete( "from SubMulti" );		t = s.beginTransaction();		t.commit();		s.close();	}	public void testMultiTable() 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 mid;		Serializable sid;		if ( getDialect() instanceof SybaseDialect ) {			mid = s.save(multi);			sid = s.save(simp);		}		else {			mid = new Long(123);			s.save(multi, mid);			sid = new Long(1234);			s.save(simp, sid);		}		SubMulti sm = new SubMulti();		sm.setAmount(66.5f);		Serializable smid;		if (getDialect() instanceof SybaseDialect) {			smid = s.save(sm);		}		else {			smid = new Long(2);			s.save(sm, smid);		}		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, mid);		simp.setName("new name");		s.update(simp, sid);		sm.setAmount(456.7f);		s.update(sm, smid);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		multi = (Multi) s.load(Multi.class, mid);		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, mid);		simp = (Top) s.load(Top.class, sid);		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 Multi m where m.class = SubMulti").size()==1		);		assertTrue(			s.find("from Top m 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");		s.find("from Lower ls join ls.set s where s.id is not null");		if ( !(getDialect() instanceof MySQLDialect) ) s.find("from SubMulti sm where exists elements(sm.children)");		List l = s.createCriteria(Top.class).list();		assertTrue( l.size()==3 );		assertTrue( s.createCriteria(SubMulti.class).list().size()==1 );		assertTrue(			s.createCriteria(SubMulti.class)				.add( Expression.lt("amount", new Float(0)) )				.list()				.size()==0		);		assertTrue(			s.createCriteria(SubMulti.class)				.add( Expression.ge("amount", new Float(0)) )				.list()				.size()==1		);		t.commit();		s.close();

⌨️ 快捷键说明

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