📄 masterdetailtest.java
字号:
//$Id: MasterDetailTest.java,v 1.1.2.10 2004/02/04 20:08:54 oneovthafew Exp $package org.hibernate.test;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;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 net.sf.hibernate.Hibernate;import net.sf.hibernate.LockMode;import net.sf.hibernate.ObjectNotFoundException;import net.sf.hibernate.Query;import net.sf.hibernate.Session;import net.sf.hibernate.Transaction;import net.sf.hibernate.dialect.HSQLDialect;import net.sf.hibernate.dialect.MckoiDialect;import net.sf.hibernate.dialect.MySQLDialect;import net.sf.hibernate.dialect.SAPDBDialect;import net.sf.hibernate.dialect.SybaseDialect;import net.sf.hibernate.expression.Example;import net.sf.hibernate.expression.Expression;import net.sf.hibernate.mapping.MetaAttribute;import net.sf.hibernate.mapping.PersistentClass;public class MasterDetailTest extends TestCase { 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); MetaAttribute meta = clazz.getMetaAttribute("foo"); assertTrue( "foo".equals( meta.getValue() ) ); meta = clazz.getProperty("name").getMetaAttribute("bar"); assertTrue( meta.isMultiValued() ); } public void testPolymorphicCriteria() throws Exception { Session s = openSession(); Category f = new Category(); Single b = new Single(); b.setId("asdfa"); b.setString("asdfasdf"); s.save(f); s.save(b); List list = s.createCriteria(Object.class).list(); assertTrue( list.size()==2 ); assertTrue( list.contains(f) && list.contains(b) ); s.delete(f); s.delete(b); s.flush(); s.connection().commit(); s.close(); } 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(); s.saveOrUpdateCopy(cat); s.flush(); s.connection().commit(); s.close(); s = openSession(); cat = (Category) s.createQuery("from Category cat where cat.name='new foo'").uniqueResult(); newSubCat = (Category) s.createQuery("from Category cat where cat.name='new sub'").uniqueResult(); newSubCat.getSubcategories().add(cat); subCatBaz = (Category) s.saveOrUpdateCopy( newSubCat, new Long( subCatBaz.getId() ) ); assertTrue( subCatBaz.getName().equals("new sub") ); assertTrue( subCatBaz.getSubcategories().size()==1 && subCatBaz.getSubcategories().get(0)==cat ); 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 m in class 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() ) .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) ) .createCriteria("otherMaster") .add( Example.create(m).excludeZeroes() ) .uniqueResult(); assertTrue( m1.getOtherMaster()==m1 ); Master m2 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeNone() ) .uniqueResult(); assertTrue( m2==m1 ); m.setName(null); m2 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeNone() ) .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 s in class Several"); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.find("from s in class 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 m IN CLASS Master WHERE NOT EXISTS ( FROM d IN m.details.elements WHERE NOT d.i=5 )"); s.iterate("FROM m IN CLASS Master WHERE NOT 5 IN ( SELECT d.i FROM d IN m.details.elements )"); } s.iterate("SELECT m FROM m IN CLASS Master, d IN m.details.elements WHERE d.i=5"); s.find("SELECT m FROM m IN CLASS Master, d IN m.details.elements WHERE d.i=5"); s.find("SELECT m.id FROM m IN CLASS Master, d IN m.details.elements 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 SAPDBDialect) && !(getDialect() instanceof MckoiDialect) ) { assertTrue( "query", s.find("from d in class Detail, m in class Master where m = d.master and m.outgoing.size = 0 and m.incoming.size = 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 ); 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"); list = s.find("select m.id from Master m inner join fetch m.details"); t.commit(); s.close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -