masterdetailtest.java

来自「好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.」· Java 代码 · 共 1,163 行 · 第 1/3 页

JAVA
1,163
字号
//$Id: MasterDetailTest.java 9994 2006-06-06 20:48:37Z steve.ebersole@jboss.com $
package org.hibernate.test.legacy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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 junit.framework.TestSuite;
import junit.textui.TestRunner;

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.Expression;
import org.hibernate.dialect.DerbyDialect;
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.dialect.SybaseDialect;
import org.hibernate.mapping.MetaAttribute;
import org.hibernate.mapping.PersistentClass;


public class MasterDetailTest extends LegacyTestCase {

	public MasterDetailTest(String arg) {
		super(arg);
	}

	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( Expression.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) );

⌨️ 快捷键说明

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