📄 parentchildtest.java
字号:
//$Id: ParentChildTest.java,v 1.1.2.9 2004/01/10 08:17:14 oneovthafew Exp $package org.hibernate.test;import java.io.Serializable;import java.util.ArrayList;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 junit.framework.TestSuite;import junit.textui.TestRunner;import net.sf.hibernate.Criteria;import net.sf.hibernate.Hibernate;import net.sf.hibernate.LockMode;import net.sf.hibernate.ReplicationMode;import net.sf.hibernate.Session;import net.sf.hibernate.Transaction;import net.sf.hibernate.dialect.DB2Dialect;import net.sf.hibernate.dialect.HSQLDialect;import net.sf.hibernate.dialect.MySQLDialect;import net.sf.hibernate.dialect.PostgreSQLDialect;import net.sf.hibernate.expression.Expression;public class ParentChildTest extends TestCase { public ParentChildTest(String x) { super(x); } public void testReplicate() throws Exception { if ( getDialect() instanceof HSQLDialect ) return; 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); 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(); Serializable id = s.save( new Parent() ); assertTrue( s.find("from Parent p left join fetch p.child").size()==1 ); s.flush(); s.connection().commit(); s.close(); s = openSession(); 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"); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.delete( s.get(Parent.class, id) ); s.flush(); s.connection().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 ); 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(); String feekey = foo.getDependent().getKey(); 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( Expression.isNotNull("name") ) .createCriteria("proxyArray") .add( Expression.eqProperty("name", "name") ) .add( Expression.eq("name", "g2") ) .add( Expression.gt("x", new Integer(-666) ) ); crit.createCriteria("fooSet") .add( Expression.isNull("null") ) .add( Expression.eq("string", "a string") ) .add( Expression.lt("integer", new Integer(-665) ) ); crit.createCriteria("fooArray") .add( Expression.eq("string", "a string") ) .setLockMode(lockMode); List list = crit.list(); assertTrue( list.size()==2 ); g2.setName(null); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); crit = s.createCriteria(Baz.class) .setLockMode(lockMode); crit.createCriteria("topGlarchez") .add( Expression.gt("x", new Integer(-666) ) ); crit.createCriteria("fooSet") .add( Expression.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.foo.component.glarch") .add( Expression.eq("name", "xxx") ) .add( Expression.eq("fooSet.foo.component.glarch.name", "xxx") ) .list(); assertTrue( list.size()==0 );*/ list = s.createCriteria(Baz.class) .createCriteria("fooSet") .createCriteria("foo") .createCriteria("component.glarch") .add( Expression.eq("name", "xxx") ) .list(); assertTrue( list.size()==0 ); list = s.createCriteria(Baz.class) .createAlias("fooSet", "foo") .createAlias("foo.foo", "foo2") .setLockMode("foo2", lockMode) .add( Expression.isNull("foo2.component.glarch") ) .createCriteria("foo2.component.glarch") .add( Expression.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( Expression.isNotNull("name") ); crit.createCriteria("fooSet") .add( Expression.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()) ); 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 { if (getDialect() instanceof PostgreSQLDialect) return; 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( Expression.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()==1 ); baz = (Baz) s.createCriteria(Baz.class).uniqueResult(); assertTrue( s.createFilter( baz.getParts(), "" ).list().size()==1 ); //assertTrue( baz.getParts().size()==1 ); s.delete( s.get( Part.class, p1.getId() )); s.delete( s.get( Part.class, p2.getId() )); s.delete(baz); t.commit(); s.close(); } public void testCollectionQuery() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple s1 = new Simple(); s1.setName("s"); s1.setCount(0); Simple s2 = new Simple(); s2.setCount(2); Simple s3 = new Simple(); s3.setCount(3); s.save( s1, new Long(1) ); s.save( s2, new Long(2) ); s.save( s3, new Long(3) ); Container c = new Container(); Contained cd = new Contained(); List bag = new ArrayList(); bag.add(cd); c.setBag(bag); List l = new ArrayList(); l.add(s1); l.add(s3); l.add(s2); c.setOneToMany(l); l = new ArrayList(); l.add(s1); l.add(null); l.add(s2); c.setManyToMany(l); s.save(c); Container cx = new Container(); s.save(cx); Simple sx = new Simple(); sx.setCount(5); sx.setName("s"); s.save( sx, new Long(5) ); assertTrue( s.find("select c from c in class ContainerX, s in class Simple where c.oneToMany[2] = s") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX, s in class Simple where c.manyToMany[2] = s") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX, s in class Simple where s = c.oneToMany[2]") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX, s in class Simple where s = c.manyToMany[2]") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX where c.oneToMany[0].name = 's'") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX where c.manyToMany[0].name = 's'") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX where 's' = c.oneToMany[2 - 2].name") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX where 's' = c.manyToMany[(3+1)/4-1].name") .size() == 1 ); if ( ! ( getDialect() instanceof MySQLDialect ) ) { assertTrue( s.find("select c from c in class ContainerX where c.manyToMany[ c.manyToMany.maxIndex ].count = 2") .size() == 1 ); assertTrue( s.find("select c from c in class ContainerX where c.manyToMany[ maxindex(c.manyToMany) ].count = 2") .size() == 1 ); } assertTrue( s.find("select c from c in class ContainerX where c.oneToMany[ c.manyToMany[0].count ].name = 's'") .size() == 1 ); assertTrue( s.find("select c from ContainerX c where c.manyToMany[ c.oneToMany[0].count ].name = 's'") .size() == 1 ); assertTrue( s.contains(cd) ); if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) ) { s.filter( c.getBag(), "where 0 in elements(this.bag)" ); s.filter( c.getBag(), "where 0 in elements(this.lazyBag)" ); } s.find("select count(comp.name) from ContainerX c join c.components comp"); s.delete(cd); s.delete(c); s.delete(s1); s.delete(s2); s.delete(s3); s.delete(cx); s.delete(sx); t.commit(); s.close(); } public void testParentChild() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Parent p = new Parent(); Child c = new Child(); c.setParent(p); p.setChild(c); s.save(p); s.save(c); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c = (Child) s.load( Child.class, new Long( c.getId() ) ); p = c.getParent(); assertTrue( "1-1 parent", p!=null ); c.setCount(32); p.setCount(66); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c = (Child) s.load( Child.class, new Long( c.getId() ) ); p = c.getParent(); assertTrue( "1-1 update", p.getCount()==66 ); assertTrue( "1-1 update", c.getCount()==32 ); assertTrue( "1-1 query", s.find("from c in class Child where c.parent.count=66").size()==1 ); assertTrue( "1-1 query", ( (Object[]) s.find("from Parent p join p.child c where p.count=66").get(0) ).length==2 ); s.find("select c, c.parent from c in class Child order by c.parent.count"); s.find("select c, c.parent from c in class Child where c.parent.count=66 order by c.parent.count"); s.iterate("select c, c.parent, c.parent.count from c in class Child order by c.parent.count"); assertTrue( "1-1 query", s.find("FROM p IN CLASS Parent WHERE p.count = ?", new Integer(66), Hibernate.INTEGER).size()==1 ); s.delete(c); s.delete(p); t.commit(); s.close(); } public void testParentNullChild() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Parent p = new Parent(); s.save(p); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); p = (Parent) s.load( Parent.class, new Long( p.getId() ) ); assertTrue( p.getChild()==null ); p.setCount(66); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); p = (Parent) s.load( Parent.class, new Long( p.getId() ) ); assertTrue( "null 1-1 update", p.getCount()==66 ); assertTrue( p.getChild()==null ); s.delete(p); t.commit(); s.close(); } public void testManyToMany() throws Exception { if (getDialect() instanceof HSQLDialect) return; Session s = openSession();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -