📄 parentchildtest.java
字号:
//$Id: ParentChildTest.java 11089 2007-01-24 14:34:22Z max.andersen@jboss.com $package org.hibernate.test.legacy;import java.io.Serializable;import java.sql.Connection;import java.sql.SQLException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import junit.framework.Test;import org.hibernate.Criteria;import org.hibernate.FetchMode;import org.hibernate.Hibernate;import org.hibernate.HibernateException;import org.hibernate.LockMode;import org.hibernate.ObjectNotFoundException;import org.hibernate.ReplicationMode;import org.hibernate.Transaction;import org.hibernate.classic.Session;import org.hibernate.criterion.Restrictions;import org.hibernate.dialect.DB2Dialect;import org.hibernate.dialect.HSQLDialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.engine.EntityEntry;import org.hibernate.impl.SessionImpl;import org.hibernate.junit.functional.FunctionalTestClassTestSuite;public class ParentChildTest extends LegacyTestCase { public ParentChildTest(String x) { super(x); } 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 Test suite() { return new FunctionalTestClassTestSuite( ParentChildTest.class ); } public void testReplicate() throws Exception { Session s = openSession(); Container baz = new Container(); Contained f = new Contained(); List list = new ArrayList(); list.add(baz); f.setBag(list); List list2 = new ArrayList(); list2.add(f); baz.setBag(list2); s.save(f); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.replicate(baz, ReplicationMode.OVERWRITE); // HHH-2378 SessionImpl x = (SessionImpl)s; EntityEntry entry = x.getPersistenceContext().getEntry( baz ); assertNull(entry.getVersion()); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.replicate(baz, ReplicationMode.IGNORE); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.delete(baz); s.delete(f); s.flush(); s.connection().commit(); s.close(); } public void testQueryOneToOne() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Serializable id = s.save( new Parent() ); assertTrue( s.find("from Parent p left join fetch p.child").size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Parent p = (Parent) s.createQuery("from Parent p left join fetch p.child").uniqueResult(); assertTrue( p.getChild()==null ); s.find("from Parent p join p.child c where c.x > 0"); s.find("from Child c join c.parent p where p.x > 0"); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.delete( s.get(Parent.class, id) ); t.commit(); s.close(); } public void testProxyReuse() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); FooProxy foo = new Foo(); FooProxy foo2 = new Foo(); Serializable id = s.save(foo); Serializable id2 = s.save(foo2); foo2.setInt(1234567); foo.setInt(1234); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); foo = (FooProxy) s.load(Foo.class, id); foo2 = (FooProxy) s.load(Foo.class, id2); assertFalse( Hibernate.isInitialized(foo) ); Hibernate.initialize(foo2); Hibernate.initialize(foo); assertTrue( foo.getComponent().getImportantDates().length==4 ); assertTrue( foo2.getComponent().getImportantDates().length==4 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); foo.setFloat( new Float(1.2f) ); foo2.setFloat( new Float(1.3f) ); foo2.getDependent().setKey(null); foo2.getComponent().getSubcomponent().getFee().setKey(null); assertFalse( foo2.getKey().equals(id) ); s.save(foo, "xyzid"); s.update(foo2, id); //intentionally id, not id2! assertEquals( foo2.getKey(), id ); assertTrue( foo2.getInt()==1234567 ); assertEquals( foo.getKey(), "xyzid" ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); foo = (FooProxy) s.load(Foo.class, id); assertTrue( foo.getInt()==1234567 ); assertTrue( foo.getComponent().getImportantDates().length==4 ); String feekey = foo.getDependent().getKey(); String fookey = foo.getKey(); s.delete(foo); s.delete( s.get(Foo.class, id2) ); s.delete( s.get(Foo.class, "xyzid") ); assertTrue( s.delete("from java.lang.Object")==3 ); t.commit(); s.close(); //to account for new id rollback shit foo.setKey(fookey); foo.getDependent().setKey(feekey); foo.getComponent().setGlarch(null); foo.getComponent().setSubcomponent(null); s = openSession(); t = s.beginTransaction(); //foo.getComponent().setGlarch(null); //no id property! s.replicate(foo, ReplicationMode.OVERWRITE); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Foo refoo = (Foo) s.get(Foo.class, id); assertEquals( feekey, refoo.getDependent().getKey() ); s.delete(refoo); t.commit(); s.close(); } public void testComplexCriteria() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Baz baz = new Baz(); s.save(baz); baz.setDefaults(); Map topGlarchez = new HashMap(); baz.setTopGlarchez(topGlarchez); Glarch g1 = new Glarch(); g1.setName("g1"); s.save(g1); Glarch g2 = new Glarch(); g2.setName("g2"); s.save(g2); g1.setProxyArray( new GlarchProxy[] {g2} ); topGlarchez.put( new Character('1'),g1 ); topGlarchez.put( new Character('2'), g2); Foo foo1 = new Foo(); Foo foo2 = new Foo(); s.save(foo1); s.save(foo2); baz.getFooSet().add(foo1); baz.getFooSet().add(foo2); baz.setFooArray( new FooProxy[] { foo1 } ); LockMode lockMode = (getDialect() instanceof DB2Dialect) ? LockMode.READ : LockMode.UPGRADE; Criteria crit = s.createCriteria(Baz.class); crit.createCriteria("topGlarchez") .add( Restrictions.isNotNull("name") ) .createCriteria("proxyArray") .add( Restrictions.eqProperty("name", "name") ) .add( Restrictions.eq("name", "g2") ) .add( Restrictions.gt("x", new Integer(-666) ) ); crit.createCriteria("fooSet") .add( Restrictions.isNull("null") ) .add( Restrictions.eq("string", "a string") ) .add( Restrictions.lt("integer", new Integer(-665) ) ); crit.createCriteria("fooArray") .add( Restrictions.eq("string", "a string") ) .setLockMode(lockMode); List list = crit.list(); assertTrue( list.size()==2 ); s.createCriteria(Glarch.class).setLockMode(LockMode.UPGRADE).list(); s.createCriteria(Glarch.class).setLockMode(Criteria.ROOT_ALIAS, LockMode.UPGRADE).list(); g2.setName(null); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); list = s.createCriteria(Baz.class).add( Restrictions.isEmpty("fooSet") ).list(); assertEquals( list.size(), 0 ); list = s.createCriteria(Baz.class).add( Restrictions.isNotEmpty("fooSet") ).list(); assertEquals( new HashSet(list).size(), 1 ); list = s.createCriteria(Baz.class).add( Restrictions.sizeEq("fooSet", 2) ).list(); assertEquals( new HashSet(list).size(), 1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); crit = s.createCriteria(Baz.class) .setLockMode(lockMode); crit.createCriteria("topGlarchez") .add( Restrictions.gt( "x", new Integer(-666) ) ); crit.createCriteria("fooSet") .add( Restrictions.isNull("null") ); list = crit.list(); assertTrue( list.size()==4 ); baz = (Baz) crit.uniqueResult(); assertTrue( Hibernate.isInitialized(baz.getTopGlarchez()) ); //cos it is nonlazy assertTrue( !Hibernate.isInitialized(baz.getFooSet()) ); list = s.createCriteria(Baz.class) .createCriteria("fooSet") .createCriteria("foo") .createCriteria("component.glarch") .add( Restrictions.eq("name", "xxx") ) .list(); assertTrue( list.size()==0 ); list = s.createCriteria(Baz.class) .createAlias("fooSet", "foo") .createAlias("foo.foo", "foo2") .setLockMode("foo2", lockMode) .add( Restrictions.isNull("foo2.component.glarch") ) .createCriteria("foo2.component.glarch") .add( Restrictions.eq("name", "xxx") ) .list(); assertTrue( list.size()==0 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); crit = s.createCriteria(Baz.class); crit.createCriteria("topGlarchez") .add( Restrictions.isNotNull("name") ); crit.createCriteria("fooSet") .add( Restrictions.isNull("null") ); list = crit.list(); assertTrue( list.size()==2 ); baz = (Baz) crit.uniqueResult(); assertTrue( Hibernate.isInitialized(baz.getTopGlarchez()) ); //cos it is nonlazy assertTrue( !Hibernate.isInitialized(baz.getFooSet()) ); list = s.createCriteria(Child.class).setFetchMode("parent", FetchMode.JOIN).list(); s.delete("from Glarch g"); s.delete( s.get(Foo.class, foo1.getKey() ) ); s.delete( s.get(Foo.class, foo2.getKey() ) ); s.delete(baz); t.commit(); s.close(); } public void testClassWhere() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Baz baz = new Baz(); baz.setParts( new ArrayList() ); Part p1 = new Part(); p1.setDescription("xyz"); Part p2 = new Part(); p2.setDescription("abc"); baz.getParts().add(p1); baz.getParts().add(p2); s.save(baz); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); assertTrue( s.createCriteria(Part.class).list().size()==1 ); //there is a where condition on Part mapping assertTrue( s.createCriteria(Part.class).add( Restrictions.eq( "id", p1.getId() ) ).list().size()==1 ); assertTrue( s.createQuery("from Part").list().size()==1 ); assertTrue( s.createQuery("from Baz baz join baz.parts").list().size()==2 ); baz = (Baz) s.createCriteria(Baz.class).uniqueResult(); assertTrue( s.createFilter( baz.getParts(), "" ).list().size()==2 ); //assertTrue( baz.getParts().size()==1 ); s.delete( s.get( Part.class, p1.getId() ));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -