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

📄 masterdetailtest.java

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//$Id: MasterDetailTest.java 10981 2006-12-13 00:14:17Z steve.ebersole@jboss.com $package org.hibernate.test.legacy;import java.io.Serializable;import java.sql.Connection;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;import java.util.List;import junit.framework.Test;import org.hibernate.Hibernate;import org.hibernate.LockMode;import org.hibernate.ObjectNotFoundException;import org.hibernate.Query;import org.hibernate.Transaction;import org.hibernate.classic.Session;import org.hibernate.criterion.Example;import org.hibernate.criterion.Restrictions;import org.hibernate.dialect.HSQLDialect;import org.hibernate.dialect.MckoiDialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.dialect.Oracle9Dialect;import org.hibernate.dialect.SAPDBDialect;import org.hibernate.junit.functional.FunctionalTestClassTestSuite;import org.hibernate.mapping.MetaAttribute;import org.hibernate.mapping.PersistentClass;public class MasterDetailTest extends LegacyTestCase {	public MasterDetailTest(String arg) {		super(arg);	}	public String[] getMappings() {		return new String[] {			"legacy/MasterDetail.hbm.xml",			"legacy/Custom.hbm.xml",			"legacy/Category.hbm.xml",			"legacy/Nameable.hbm.xml",			"legacy/SingleSeveral.hbm.xml",			"legacy/WZ.hbm.xml",			"legacy/UpDown.hbm.xml",			"legacy/Eye.hbm.xml"		};	}	public static Test suite() {		return new FunctionalTestClassTestSuite( MasterDetailTest.class );	}	public void testOuterJoin() throws Exception {		Session s = openSession();		Eye e = new Eye();		e.setName("Eye Eye");		Jay jay = new Jay(e);		e.setJay(jay);		s.saveOrUpdate(e);		s.flush();		s.connection().commit();		s.close();		s = openSession();		e = (Eye) s.createCriteria(Eye.class).uniqueResult();		assertTrue( Hibernate.isInitialized( e.getJay() ) );		assertTrue( Hibernate.isInitialized( e.getJays() ) );		s.connection().commit();		s.close();		s = openSession();		jay = (Jay) s.createQuery("select new Jay(eye) from Eye eye").uniqueResult();		assertTrue( "Eye Eye".equals( jay.getEye().getName() ) );		s.delete( jay.getEye() );		s.flush();		s.connection().commit();		s.close();	}	public void testMeta() throws Exception {		PersistentClass clazz = getCfg().getClassMapping( Master.class.getName() );		MetaAttribute meta = clazz.getMetaAttribute("foo");		assertTrue( "foo".equals( meta.getValue() ) );		meta = clazz.getProperty("name").getMetaAttribute("bar");		assertTrue( meta.isMultiValued() );	}	public void testCopy() throws Exception {		Category catWA = new Category();		catWA.setName("HSQL workaround");		Category cat = new Category();		cat.setName("foo");		Category subCatBar = new Category();		subCatBar.setName("bar");		Category subCatBaz = new Category();		subCatBaz.setName("baz");		cat.getSubcategories().add(subCatBar);		cat.getSubcategories().add(subCatBaz);		Session s = openSession();		s.save(catWA);		s.save(cat);		s.flush();		s.connection().commit();		s.close();		cat.setName("new foo");		subCatBar.setName("new bar");		cat.getSubcategories().remove(subCatBaz);		Category newCat = new Category();		newCat.setName("new");		cat.getSubcategories().add(newCat);		Category newSubCat = new Category();		newSubCat.setName("new sub");		newCat.getSubcategories().add(newSubCat);		s = openSession();		Category copiedCat = (Category) s.saveOrUpdateCopy(cat);		s.flush();		s.connection().commit();		s.close();		assertFalse( copiedCat==cat );		//assertFalse( copiedCat.getSubcategories().contains(newCat) );		assertTrue( cat.getSubcategories().contains(newCat) );		s = openSession();		cat = (Category) s.createQuery("from Category cat where cat.name='new foo'").uniqueResult();		newSubCat = (Category) s.createQuery("from Category cat left join fetch cat.subcategories where cat.name='new sub'").uniqueResult();		assertTrue( newSubCat.getName().equals("new sub") );		s.close();		newSubCat.getSubcategories().add(cat);		cat.setName("new new foo");		s = openSession();		newSubCat = (Category) s.saveOrUpdateCopy( newSubCat, new Long( newSubCat.getId() ) );		assertTrue( newSubCat.getName().equals("new sub") );		assertTrue( newSubCat.getSubcategories().size()==1 );		cat = (Category) newSubCat.getSubcategories().get(0);		assertTrue( cat.getName().equals("new new foo") );		newSubCat.getSubcategories().remove(cat);		s.delete(cat);		s.delete(subCatBaz);		s.delete(catWA);		s.flush();		s.connection().commit();		s.close();	}	public void testNotNullDiscriminator() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Up up = new Up();		up.setId1("foo");		up.setId2(123l);		Down down = new Down();		down.setId1("foo");		down.setId2(321l);		down.setValue(12312312l);		s.save(up);		s.save(down);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		List list = s.find("from Up up order by up.id2 asc");		assertTrue( list.size()==2 );		assertFalse( list.get(0) instanceof Down );		assertTrue( list.get(1) instanceof Down );		list = s.find("from Down down");		assertTrue( list.size()==1 );		assertTrue( list.get(0) instanceof Down );		//list = s.find("from Up down where down.class = Down");		assertTrue( list.size()==1 );		assertTrue( list.get(0) instanceof Down );		s.delete("from Up up");		t.commit();		s.close();	}	public void testSelfManyToOne() throws Exception {		//if (dialect instanceof HSQLDialect) return;		Session s = openSession();		Transaction t = s.beginTransaction();		Master m = new Master();		m.setOtherMaster(m);		s.save(m);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		Iterator i = s.iterate("from Master");		m = (Master) i.next();		assertTrue( m.getOtherMaster()==m );		if (getDialect() instanceof HSQLDialect) { m.setOtherMaster(null); s.flush(); }		s.delete(m);		t.commit();		s.close();	}	public void testExample() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Master m = new Master();		m.setName("name");		m.setX(5);		m.setOtherMaster(m);		s.save(m);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		Master m1 = (Master) s.createCriteria(Master.class)			.add( Example.create(m).enableLike().ignoreCase().excludeProperty("bigDecimal") )			.uniqueResult();		assertTrue( m1.getOtherMaster()==m1 );		m1 = (Master) s.createCriteria(Master.class)			.add( Restrictions.eq("name", "foobar") )			.uniqueResult();		assertTrue( m1==null );		m1 = (Master) s.createCriteria(Master.class)			.add( Example.create(m).excludeProperty("bigDecimal") )			.createCriteria("otherMaster")				.add( Example.create(m).excludeZeroes().excludeProperty("bigDecimal") )			.uniqueResult();		assertTrue( m1.getOtherMaster()==m1 );		Master m2 = (Master) s.createCriteria(Master.class)			.add( Example.create(m).excludeNone().excludeProperty("bigDecimal") )			.uniqueResult();		assertTrue( m2==m1 );		m.setName(null);		m2 = (Master) s.createCriteria(Master.class)			.add( Example.create(m).excludeNone().excludeProperty("bigDecimal") )			.uniqueResult();		assertTrue( null==m2 );		if (getDialect() instanceof HSQLDialect) { m1.setOtherMaster(null); s.flush(); }		s.delete(m1);		t.commit();		s.close();	}	public void testNonLazyBidirectional() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Single sin = new Single();		sin.setId("asdfds");		sin.setString("adsa asdfasd");		Several sev = new Several();		sev.setId("asdfasdfasd");		sev.setString("asd ddd");		sin.getSeveral().add(sev);		sev.setSingle(sin);		s.save(sin);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		sin = (Single) s.load( Single.class, sin );		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		sev = (Several) s.load( Several.class, sev );		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		s.find("from Several");		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		s.delete("from Single");		t.commit();		s.close();	}	public void testCollectionQuery() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof MckoiDialect) ) {			s.iterate("FROM Master m WHERE NOT EXISTS ( FROM m.details d WHERE NOT d.i=5 )");			s.iterate("FROM Master m WHERE NOT 5 IN ( SELECT d.i FROM m.details AS d )");		}		s.iterate("SELECT m FROM Master m JOIN m.details d WHERE d.i=5");		s.find("SELECT m FROM Master m JOIN m.details d WHERE d.i=5");		s.find("SELECT m.id FROM Master AS m JOIN m.details AS d WHERE d.i=5");		t.commit();		s.close();	}	public void testMasterDetail() throws Exception {		if (getDialect() instanceof HSQLDialect) return;		Session s = openSession();		Transaction t = s.beginTransaction();		Master master = new Master();		assertTrue( "save returned native id", s.save(master)!=null );		Serializable mid = s.getIdentifier(master);		Detail d1 = new Detail();		d1.setMaster(master);		Serializable did = s.save(d1);		Detail d2 = new Detail();		d2.setI(12);		d2.setMaster(master);		assertTrue( "generated id returned", s.save(d2)!=null);		master.addDetail(d1);		master.addDetail(d2);		if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof Oracle9Dialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof org.hibernate.dialect.TimesTenDialect)) {			assertTrue(				"query",				s.find("from Detail d, Master m where m = d.master and size(m.outgoing) = 0 and size(m.incoming) = 0").size()==2			);		}		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		master = new Master();		s.load(master, mid);		assertTrue( master.getDetails().size()==2 );		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		master = (Master) s.load(Master.class, mid);		Iterator iter = master.getDetails().iterator();		int i=0;		while ( iter.hasNext() ) {			Detail d = (Detail) iter.next();			assertTrue( "master-detail", d.getMaster()==master );			i++;		}		assertTrue( "master-detail", i==2 );		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		assertTrue( s.find("select elements(master.details) from Master master").size()==2 );		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		List list = s.find("from Master m left join fetch m.details");		Master m = (Master) list.get(0);		assertTrue( Hibernate.isInitialized( m.getDetails() ) );		assertTrue( m.getDetails().size()==2 );		list = s.find("from Detail d inner join fetch d.master");		Detail dt = (Detail) list.get(0);		Serializable dtid = s.getIdentifier(dt);		assertTrue( dt.getMaster()==m );		//assertTrue(m.getAllDetails().size()==2);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		list = s.find("select m from Master m1, Master m left join fetch m.details where m.name=m1.name");		assertTrue( Hibernate.isInitialized( ( (Master) list.get(0) ).getDetails() ) );		dt = (Detail) s.load(Detail.class, dtid);		assertTrue( ( (Master) list.get(0) ).getDetails().contains(dt) );		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		list = s.find("select m, m1.name from Master m1, Master m left join fetch m.details where m.name=m1.name");		assertTrue( Hibernate.isInitialized( ( (Master) ( (Object[]) list.get(0) )[0] ).getDetails() ) );		dt = (Detail) s.load(Detail.class, dtid);		assertTrue( ( (Master) ( (Object[]) list.get(0) )[0] ).getDetails().contains(dt) );		//list = s.find("select m from Master m, Master m2 left join fetch m.details");// depracted syntax//		list = s.find("select m.id from Master m inner join fetch m.details");		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		Detail dd = (Detail) s.load(Detail.class, did);		master = dd.getMaster();		assertTrue( "detail-master", master.getDetails().contains(dd) );		assertTrue( s.filter( master.getDetails(), "order by this.i desc").size()==2 );		assertTrue( s.filter( master.getDetails(), "select this where this.id > -1").size()==2 );		Query q = s.createFilter( master.getDetails(), "where this.id > :id" );		q.setInteger("id", -1);		assertTrue( q.list().size()==2 );		q = s.createFilter( master.getDetails(), "where this.id > :id1 and this.id < :id2" );		q.setInteger("id1", -1);		q.setInteger("id2", 99999999);		assertTrue( q.list().size()==2 );		q.setInteger("id2", -1);		assertTrue( q.list().size()==0 );		q = s.createFilter( master.getDetails(), "where this.id in (:ids)" );		list = new ArrayList();		list.add(did);		list.add( new Long(-1) );		q.setParameterList("ids", list);		assertTrue( q.list().size()==1 );		assertTrue( q.iterate().hasNext() );		assertTrue( s.filter( master.getDetails(), "where this.id > -1").size()==2 );		assertTrue( s.filter( master.getDetails(), "select this.master where this.id > -1").size()==2 );		assertTrue( s.filter( master.getDetails(), "select m from Master m where this.id > -1 and this.master=m").size()==2 );		assertTrue( s.filter( master.getIncoming(), "where this.id > -1 and this.name is not null").size()==0 );		assertTrue( s.createFilter( master.getDetails(), "select max(this.i)" ).iterate().next() instanceof Integer );		assertTrue( s.createFilter( master.getDetails(), "select max(this.i) group by this.id" ).iterate().next() instanceof Integer );		assertTrue( s.createFilter( master.getDetails(), "select count(*)" ).iterate().next() instanceof Long );		assertTrue( s.createFilter( master.getDetails(), "select this.master" ).list().size()==2 );		assertTrue( s.filter( master.getMoreDetails(), "" ).size()==0 );		assertTrue( s.filter( master.getIncoming(), "" ).size()==0 );		Query f = s.createFilter( master.getDetails(), "select max(this.i) where this.i < :top and this.i>=:bottom" );		f.setInteger("top", 100);		f.setInteger("bottom", 0);		assertEquals( f.iterate().next(), new Integer(12) );		f.setInteger("top", 2);		assertEquals( f.iterate().next(), new Integer(0) );		f = s.createFilter( master.getDetails(), "select max(this.i) where this.i not in (:list)" );		Collection coll = new ArrayList();		coll.add( new Integer(-666) );		coll.add( new Integer(22) );		coll.add( new Integer(0) );		f.setParameterList("list", coll);		assertEquals( f.iterate().next(), new Integer(12) );		f = s.createFilter( master.getDetails(), "select max(this.i) where this.i not in (:list) and this.master.name = :listy2" );		f.setParameterList("list", coll);		f.setParameter( "listy2", master.getName() );		assertEquals( f.iterate().next(), new Integer(12) );		iter = master.getDetails().iterator();		i=0;		while ( iter.hasNext() ) {			Detail d = (Detail) iter.next();			assertTrue( "master-detail", d.getMaster()==master );			s.delete(d);			i++;		}		assertTrue( "master-detail", i==2 );		s.delete(master);		t.commit();		s.close();	}	public void testIncomingOutgoing() throws Exception {		Session s = openSession();		Master master1 = new Master();		Master master2 = new Master();		Master master3 = new Master();		s.save(master1);		s.save(master2);		s.save(master3);		master1.addIncoming(master2);		master2.addOutgoing(master1);		master1.addIncoming(master3);		master3.addOutgoing(master1);		Serializable m1id = s.getIdentifier(master1);		assertTrue( s.filter( master1.getIncoming(), "where this.id > 0 and this.name is not null").size()==2 );		s.flush();		s.connection().commit();		s.close();		s = openSession();		master1 = (Master) s.load(Master.class, m1id);		Iterator iter = master1.getIncoming().iterator();		int i=0;		while ( iter.hasNext() ) {			Master m = (Master) iter.next();			assertTrue( "outgoing", m.getOutgoing().size()==1 );			assertTrue( "outgoing", m.getOutgoing().contains(master1) );			s.delete(m);			i++;		}		assertTrue( "incoming-outgoing", i==2 );		s.delete(master1);		s.flush();		s.connection().commit();		s.close();	}	public void testCascading() throws Exception {		Session s = openSession();		Detail d1 = new Detail();		Detail d2 = new Detail();		d2.setI(22);		Master m = new Master();		Master m0 = new Master();		Serializable m0id = s.save(m0);		m0.addDetail(d1); m0.addDetail(d2);		d1.setMaster(m0); d2.setMaster(m0);		m.getMoreDetails().add(d1);		m.getMoreDetails().add(d2);		Serializable mid = s.save(m);		s.flush();		s.connection().commit();		s.close();		s = openSession();		m = (Master) s.load(Master.class, mid);		assertTrue( "cascade save", m.getMoreDetails().size()==2 );		assertTrue( "cascade save", ( (Detail) m.getMoreDetails().iterator().next() ).getMaster().getDetails().size()==2 );

⌨️ 快捷键说明

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