下面列出了org.hibernate.classic.Session#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testOnCascadeDelete() throws Exception {
if ( ! supportsCircularCascadeDelete() ) {
return;
}
Session s = openSession();
Baz baz = new Baz();
baz.subs = new ArrayList();
Baz sub = new Baz();
sub.superBaz = baz;
baz.subs.add(sub);
s.save(baz);
s.flush();
assertTrue( s.createQuery("from Baz").list().size()==2 );
s.connection().commit();
s.delete(baz);
s.flush();
s.connection().commit();
assertTrue( s.createQuery("from Baz").list().size()==0 );
s.connection().commit();
s.close();
}
public void testCriteria() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Lower l = new Lower();
s.save(l);
assertTrue( l==s.createCriteria(Top.class).uniqueResult() );
s.delete(l);
s.flush();
Criteria c = s.createCriteria(Lower.class);
c.createCriteria("yetanother")
.add( Expression.isNotNull("id") )
.createCriteria("another");
c.createCriteria("another").add( Expression.isNotNull("id") );
c.list();
t.commit();
s.close();
}
public void testCriteriaCollection() throws Exception {
Session s = openSession();
Baz bb = (Baz) s.createCriteria(Baz.class).uniqueResult();
assertTrue(bb==null);
Baz baz = new Baz();
s.save(baz);
s.flush();
s.connection().commit();
s.close();
s = openSession();
Baz b = (Baz) s.createCriteria(Baz.class).uniqueResult();
assertTrue( Hibernate.isInitialized( b.getTopGlarchez() ) );
assertTrue( b.getTopGlarchez().size()==0 );
s.delete(b);
s.flush();
s.connection().commit();
s.close();
}
public void testNoUpdateManyToOne() throws Exception {
Session s = openSession();
W w1 = new W();
W w2 = new W();
Z z = new Z();
z.setW(w1);
s.save(z);
s.flush();
z.setW(w2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
s.update(z);
s.flush();
s.delete(z);
s.delete("from W");
s.flush();
s.connection().commit();
s.close();
}
public void testLateCollectionAdd() throws Exception {
Session s = openSession();
Baz baz = new Baz();
List l = new ArrayList();
baz.setStringList(l);
l.add("foo");
Serializable id = s.save(baz);
l.add("bar");
s.flush();
l.add("baz");
s.flush();
s.connection().commit();
s.close();
s = openSession();
baz = (Baz) s.load(Baz.class, id);
assertTrue( baz.getStringList().size()==3 && baz.getStringList().contains("bar") );
s.delete(baz);
s.flush();
s.connection().commit();
s.close();
}
public void testUpdateOnImplicitJoinFails() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human human = new Human();
human.setName( new Name( "Steve", 'E', null ) );
Human mother = new Human();
mother.setName( new Name( "Jane", 'E', null ) );
human.setMother( mother );
s.save( human );
s.save( mother );
s.flush();
t.commit();
t = s.beginTransaction();
try {
s.createQuery( "update Human set mother.name.initial = :initial" ).setString( "initial", "F" ).executeUpdate();
fail( "update allowed across implicit join" );
}
catch( QueryException e ) {
log.debug( "TEST (OK) : " + e.getMessage() );
// expected condition
}
s.createQuery( "delete Human where mother is not null" ).executeUpdate();
s.createQuery( "delete Human" ).executeUpdate();
t.commit();
s.close();
}
public void testUpdateOrder() throws Exception {
Session s = openSession();
Fee fee1 = new Fee();
s.save(fee1);
Fee fee2 = new Fee();
fee1.setFee(fee2);
fee2.setFee(fee1);
fee2.setFees( new HashSet() );
Fee fee3 = new Fee();
fee3.setFee(fee1);
fee3.setAnotherFee(fee2);
fee2.setAnotherFee(fee3);
s.save(fee3);
s.save(fee2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
fee1.setCount(10);
fee2.setCount(20);
fee3.setCount(30);
s.update(fee1);
s.update(fee2);
s.update(fee3);
s.flush();
s.delete(fee1);
s.delete(fee2);
s.delete(fee3);
s.flush();
s.connection().commit();
s.close();
s = openSession();
Transaction tx = s.beginTransaction();
assertTrue( s.find("from Fee fee").size()==0 );
tx.commit();
s.close();
}
private void prepareTestData(Session s) {
Employee john = new Employee("John Doe");
john.setCompany( "JBoss" );
john.setDepartment( "hr" );
john.setTitle( "hr guru" );
john.setRegion( "US" );
Employee polli = new Employee("Polli Wog");
polli.setCompany( "JBoss" );
polli.setDepartment( "hr" );
polli.setTitle( "hr novice" );
polli.setRegion( "US" );
polli.setManager( john );
john.getMinions().add( polli );
Employee suzie = new Employee( "Suzie Q" );
suzie.setCompany( "JBoss" );
suzie.setDepartment( "hr" );
suzie.setTitle( "hr novice" );
suzie.setRegion( "EMEA" );
suzie.setManager( john );
john.getMinions().add( suzie );
Customer cust = new Customer( "John Q Public" );
cust.setCompany( "Acme" );
cust.setRegion( "US" );
cust.setContactOwner( john );
Person ups = new Person( "UPS guy" );
ups.setCompany( "UPS" );
ups.setRegion( "US" );
s.save( john );
s.save( cust );
s.save( ups );
s.flush();
}
public void testCollectionWhere() throws Exception {
Foo foo1 = new Foo();
Foo foo2 = new Foo();
Baz baz = new Baz();
Foo[] arr = new Foo[10];
arr[0] = foo1;
arr[9] = foo2;
Session s = openSession();
s.save(foo1);
s.save(foo2);
baz.setFooArray(arr);
s.save(baz);
s.flush();
s.connection().commit();
s.close();
s = openSession();
baz = (Baz) s.load( Baz.class, baz.getCode() );
assertTrue( baz.getFooArray().length==1 );
assertTrue( s.find("from Baz baz join baz.fooArray foo").size()==1 );
assertTrue( s.find("from Foo foo").size()==2 );
assertTrue( s.filter( baz.getFooArray(), "" ).size()==1 );
//assertTrue( s.delete("from java.lang.Object o")==9 );
s.delete("from Foo foo");
String bazid = baz.getCode();
s.delete(baz);
int rows=s.connection().createStatement().executeUpdate(
"delete from fooArray where id_='" + bazid + "' and i>=8"
);
assertTrue(rows==1);
s.flush();
s.connection().commit();
s.close();
}
public void testProxyArray() throws Exception {
Session s = openSession();
GlarchProxy g = new Glarch();
Glarch g1 = new Glarch();
Glarch g2 = new Glarch();
g.setProxyArray( new GlarchProxy[] { g1, g2 } );
Glarch g3 = new Glarch();
s.save(g3);
g2.setProxyArray( new GlarchProxy[] {null, g3, g} );
Set set = new HashSet();
set.add(g1);
set.add(g2);
g.setProxySet(set);
s.save(g);
s.save(g1);
s.save(g2);
Serializable id = s.getIdentifier(g);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, id);
assertTrue( "array of proxies", g.getProxyArray().length==2 );
assertTrue( "array of proxies", g.getProxyArray()[0]!=null );
assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[0]==null );
assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[2]==g );
assertTrue( "set of proxies", g.getProxySet().size()==2 );
Iterator iter = s.iterate("from Glarch g");
while ( iter.hasNext() ) {
iter.next();
iter.remove();
}
s.flush();
s.connection().commit();
s.disconnect();
SerializationHelper.deserialize( SerializationHelper.serialize(s) );
s.close();
}
public void testReuseDeletedCollection() throws Exception {
Session s = openSession();
Baz baz = new Baz();
baz.setDefaults();
s.save(baz);
s.flush();
s.delete(baz);
Baz baz2 = new Baz();
baz2.setStringArray( new String[] {"x-y-z"} );
s.save(baz2);
s.flush();
s.connection().commit();
s.close();
baz2.setStringSet( baz.getStringSet() );
baz2.setStringArray( baz.getStringArray() );
baz2.setFooArray( baz.getFooArray() );
s = openSession();
s.update(baz2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
assertTrue( baz2.getStringArray().length==3 );
assertTrue( baz2.getStringSet().size()==3 );
s.delete(baz2);
s.flush();
s.connection().commit();
s.close();
}
public void testUpdateLazyCollections() throws Exception {
Session s = openSession();
Master m = new Master();
Serializable mid = s.save(m);
Detail d1 = new Detail();
Detail d2 = new Detail();
d2.setX(14);
d1.setMaster(m);
d2.setMaster(m);
s.save(d1);
s.save(d2);
m.addDetail(d1);
m.addDetail(d2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
m = (Master) s.load(Master.class, mid);
s.connection().commit();
s.close();
m.setName("New Name");
s = openSession();
s.update(m, mid);
Iterator iter = m.getDetails().iterator();
int i=0;
while ( iter.hasNext() ) {
assertTrue( iter.next()!=null );
i++;
}
assertTrue(i==2);
iter = m.getDetails().iterator();
while ( iter.hasNext() ) s.delete( iter.next() );
s.delete(m);
s.flush();
s.connection().commit();
s.close();
}
public void testCachedCollectionRefresh() throws Exception {
Session s = openSession();
Category c = new Category();
List list = new ArrayList();
c.setSubcategories(list);
list.add( new Category() );
c.setName("root");
Serializable id = s.save(c);
s.flush();
s.connection().commit();
s.close();
s = openSession();
c = (Category) s.load(Category.class, id);
c.getSubcategories().size(); //force load and cache
s.connection().commit();
s.close();
s = openSession();
if ( (getDialect() instanceof MySQLDialect) ) s.connection().setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
c = (Category) s.load(Category.class, id);
c.getSubcategories().size(); //force load
Session ss = openSession();
Category c2 = (Category) ss.load(Category.class, id);
ss.delete( c2.getSubcategories().get(0) );
c2.getSubcategories().clear();
ss.flush();
ss.connection().commit();
ss.close();
s.refresh(c);
assertTrue( c.getSubcategories().size()==0 );
ss = openSession();
c2 = (Category) ss.load(Category.class, id);
c2.getSubcategories().add( new Category() );
c2.getSubcategories().add( new Category() );
ss.flush();
ss.connection().commit();
ss.close();
s.refresh(c);
assertEquals( 2, c.getSubcategories().size() );
s.flush();
s.connection().commit();
s.close();
s = openSession();
c = (Category) s.load(Category.class, id);
assertEquals( 2, c.getSubcategories().size() );
s.delete(c);
s.flush();
s.connection().commit();
s.close();
}
public void testEmbeddedCompositeID() throws Exception {
Session s = openSession();
Location l = new Location();
l.setCountryCode("AU");
l.setDescription("foo bar");
l.setLocale( Locale.getDefault() );
l.setStreetName("Brunswick Rd");
l.setStreetNumber(300);
l.setCity("Melbourne");
s.save(l);
s.flush();
s.connection().commit();
s.close();
s = openSession();
s.setFlushMode(FlushMode.NEVER);
l = (Location) s.find("from Location l where l.countryCode = 'AU' and l.description='foo bar'").get(0);
assertTrue( l.getCountryCode().equals("AU") );
assertTrue( l.getCity().equals("Melbourne") );
assertTrue( l.getLocale().equals( Locale.getDefault() ) );
assertTrue( s.createCriteria(Location.class).add( Expression.eq( "streetNumber", new Integer(300) ) ).list().size()==1 );
s.connection().commit();
s.close();
s = openSession();
l.setDescription("sick're");
s.update(l);
s.flush();
s.connection().commit();
s.close();
s = openSession();
l = new Location();
l.setCountryCode("AU");
l.setDescription("foo bar");
l.setLocale(Locale.ENGLISH);
l.setStreetName("Brunswick Rd");
l.setStreetNumber(300);
l.setCity("Melbourne");
assertTrue( l==s.load(Location.class, l) );
assertTrue( l.getLocale().equals( Locale.getDefault() ) );
s.delete(l);
s.flush();
s.connection().commit();
s.close();
}
public void testQueuedBagAdds() throws Exception {
Session s = openSession();
Assignable a = new Assignable();
a.setId("foo");
a.setCategories( new ArrayList() );
Category c = new Category();
c.setAssignable(a);
a.getCategories().add(c);
s.save(a);
s.flush();
s.connection().commit();
s.close();
getSessions().evictCollection("org.hibernate.test.legacy.Assignable.categories");
s = openSession();
a = (Assignable) s.get(Assignable.class, "foo");
c = new Category();
c.setAssignable(a);
a.getCategories().add(c);
assertFalse( Hibernate.isInitialized( a.getCategories() ) );
assertTrue( a.getCategories().size()==2 );
s.flush();
s.connection().commit();
s.close();
getSessions().evictCollection("org.hibernate.test.legacy.Assignable.categories");
s = openSession();
a = (Assignable) s.get(Assignable.class, "foo");
c = new Category();
c.setAssignable(a);
a.getCategories().add(c);
assertFalse( Hibernate.isInitialized( a.getCategories() ) );
s.flush();
assertFalse( Hibernate.isInitialized( a.getCategories() ) );
assertTrue( a.getCategories().size()==3 );
s.connection().commit();
s.close();
getSessions().evictCollection("org.hibernate.test.legacy.Assignable.categories");
s = openSession();
a = (Assignable) s.get(Assignable.class, "foo");
assertTrue( a.getCategories().size()==3 );
s.delete(a);
s.flush();
s.connection().commit();
s.close();
}
public void testFindBySQLAssociatedObjects() throws HibernateException, SQLException {
Session s = openSession();
s.delete("from Assignable");
s.delete("from Category");
Category c = new Category();
c.setName("NAME");
Assignable assn = new Assignable();
assn.setId("i.d.");
List l = new ArrayList();
l.add(c);
assn.setCategories(l);
c.setAssignable(assn);
s.save(assn);
s.flush();
s.connection().commit();
s.close();
s = openSession();
List list = s.createSQLQuery("select {category.*} from category {category}", "category", Category.class).list();
list.get(0);
s.connection().commit();
s.close();
if ( getDialect() instanceof MySQLDialect ) return;
if ( getDialect() instanceof OracleDialect ) return; // todo : this fails on Oracle8 also
s = openSession();
Query query = s.getNamedQuery("namedsql");
assertNotNull(query);
list = query.list();
assertNotNull(list);
Object[] values = (Object[]) list.get(0);
assertNotNull(values[0]);
assertNotNull(values[1]);
assertTrue("wrong type: " + values[0].getClass(), values[0] instanceof Category);
assertTrue("wrong type: " + values[1].getClass(), values[1] instanceof Assignable);
s.connection().commit();
s.close();
}
public void testSubclassCollection() throws Exception {
//if ( getDialect() instanceof HSQLDialect ) return; //TODO: figure out why!?
Session s = openSession();
SubMulti sm = new SubMulti();
SubMulti sm1 = new SubMulti();
SubMulti sm2 = new SubMulti();
ArrayList list = new ArrayList();
ArrayList anotherList = new ArrayList();
sm.setChildren(list);
sm.setMoreChildren(anotherList);
sm.setExtraProp("foo");
list.add(sm1);
list.add(sm2);
anotherList.add(sm1);
anotherList.add(sm2);
sm1.setParent(sm);
sm2.setParent(sm);
Serializable id = s.save(sm);
s.save(sm1);
s.save(sm2);
s.flush();
s.connection().commit();
s.close();
getSessions().evict(SubMulti.class);
s = openSession();
s.connection().createStatement().executeQuery(
"select * from leafsubsubclass sm, nonleafsubclass m, rootclass s where sm.sid=m.sid and sm.sid=s.id1_ and sm.sid=1"
).next();
assertTrue( s.find("select s from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==2 );
s.find("select c from SubMulti sm join sm.children c");
assertTrue( s.find("select elements(sm.children) from SubMulti as sm").size()==2 );
assertTrue( s.find("select distinct sm from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==1 );
sm = (SubMulti) s.load(SubMulti.class, id);
assertTrue( sm.getChildren().size()==2 );
assertEquals(
s.filter( sm.getMoreChildren(), "select count(*) where this.amount>-1 and this.name is null" ).iterator().next(),
new Long(2)
);
assertEquals( "FOO", sm.getDerived() );
assertSame(
s.iterate("select distinct s from SubMulti s where s.moreChildren[1].amount < 1.0").next(),
sm
);
assertTrue( sm.getMoreChildren().size()==2 );
s.delete(sm);
Iterator iter = sm.getChildren().iterator();
while ( iter.hasNext() ) s.delete( iter.next() );
s.flush();
s.connection().commit();
s.close();
}
public void testMoveLazyCollection() throws Exception {
Session s = openSession();
Baz baz = new Baz();
Baz baz2 = new Baz();
baz.setFooSet( new HashSet() );
Foo foo = new Foo();
baz.getFooSet().add(foo);
s.save(foo);
s.save(baz);
s.save(baz2);
foo.setBytes( "foobar".getBytes() );
s.flush();
s.connection().commit();
s.close();
s = openSession();
foo = (Foo) s.get( Foo.class, foo.getKey() );
assertTrue( Hibernate.isInitialized( foo.getBytes() ) );
assertTrue( foo.getBytes().length==6 );
baz = (Baz) s.get( Baz.class, baz.getCode() );
assertTrue( baz.getFooSet().size()==1 );
s.flush();
s.connection().commit();
s.close();
getSessions().evictCollection("org.hibernate.test.legacy.Baz.fooSet");
s = openSession();
baz = (Baz) s.get( Baz.class, baz.getCode() );
assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
baz2 = (Baz) s.get( Baz.class, baz2.getCode() );
baz2.setFooSet( baz.getFooSet() );
baz.setFooSet(null);
assertFalse( Hibernate.isInitialized( baz2.getFooSet() ) );
s.flush();
s.connection().commit();
s.close();
s = openSession();
foo = (Foo) s.get( Foo.class, foo.getKey() );
assertTrue( foo.getBytes().length==6 );
baz = (Baz) s.get( Baz.class, baz.getCode() );
baz2 = (Baz) s.get( Baz.class, baz2.getCode() );
assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
assertTrue( baz.getFooSet().size()==0 );
assertTrue( Hibernate.isInitialized( baz2.getFooSet() ) ); //fooSet has batching enabled
assertTrue( baz2.getFooSet().size()==1 );
s.delete(baz);
s.delete(baz2);
s.delete(foo);
s.flush();
s.connection().commit();
s.close();
}
public void testJdbcOnly() throws Exception {
ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );
for ( int n=2; n<4000; n*=2 ) {
Session s = openSession();
Simple[] simples = new Simple[n];
s.delete("from Simple");
s.flush();
Serializable[] ids = new Serializable[n];
for ( int i=0; i<n; i++ ) {
simples[i] = new Simple();
simples[i].init();
simples[i].setCount(i);
ids[i] = new Long(i);
s.save(simples[i], ids[i]);
}
s.flush();
s.connection().commit();
s.close();
//Now do timings
Connection c = cp.getConnection();
long time = System.currentTimeMillis();
directJDBC( c, simples, ids, n, "j1" );
long jdbc = System.currentTimeMillis() - time;
cp.closeConnection(c);
c = cp.getConnection();
time = System.currentTimeMillis();
directJDBC( c, simples, ids, n, "j2" );
jdbc += System.currentTimeMillis() - time;
cp.closeConnection(c);
c = cp.getConnection();
time = System.currentTimeMillis();
directJDBC( c, simples, ids, n, "j2" );
jdbc += System.currentTimeMillis() - time;
cp.closeConnection(c);
System.out.println( "Objects: " + n + " Direct JDBC: " + jdbc );
}
cp.close();
System.gc();
}
public void testIdBag() throws Exception {
Session s = openSession();
Baz baz = new Baz();
s.save(baz);
List l = new ArrayList();
List l2 = new ArrayList();
baz.setIdFooBag(l);
baz.setByteBag(l2);
l.add( new Foo() );
l.add( new Bar() );
byte[] bytes = "ffo".getBytes();
l2.add(bytes);
l2.add( "foo".getBytes() );
s.flush();
l.add( new Foo() );
l.add( new Bar() );
l2.add( "bar".getBytes() );
s.flush();
s.delete( l.remove(3) );
bytes[1]='o';
s.flush();
s.connection().commit();
s.close();
s = openSession();
baz = (Baz) s.load(Baz.class, baz.getCode());
assertTrue( baz.getIdFooBag().size()==3 );
assertTrue( baz.getByteBag().size()==3 );
bytes = "foobar".getBytes();
Iterator iter = baz.getIdFooBag().iterator();
while ( iter.hasNext() ) s.delete( iter.next() );
baz.setIdFooBag(null);
baz.getByteBag().add(bytes);
baz.getByteBag().add(bytes);
assertTrue( baz.getByteBag().size()==5 );
s.flush();
s.connection().commit();
s.close();
s = openSession();
baz = (Baz) s.load(Baz.class, baz.getCode());
assertTrue( baz.getIdFooBag().size()==0 );
assertTrue( baz.getByteBag().size()==5 );
baz.getIdFooBag().add( new Foo() );
iter = baz.getByteBag().iterator();
iter.next();
iter.remove();
s.flush();
s.connection().commit();
s.close();
s = openSession();
baz = (Baz) s.load(Baz.class, baz.getCode());
assertTrue( baz.getIdFooBag().size()==1 );
assertTrue( baz.getByteBag().size()==4 );
s.delete(baz);
s.flush();
s.connection().commit();
s.close();
}