下面列出了org.hibernate.classic.Session#refresh ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testRefresh() throws Exception {
Session s = openSession();
Foo foo = new Foo();
s.save(foo);
s.flush();
s.connection().createStatement().executeUpdate("update "+getDialect().openQuote()+"foos"+getDialect().closeQuote()+" set long_ = -3");
s.refresh(foo);
assertTrue( foo.getLong().longValue()==-3l );
assertTrue( s.getCurrentLockMode(foo)==LockMode.READ );
s.refresh(foo, LockMode.UPGRADE);
if ( getDialect().supportsOuterJoinForUpdate() ) {
assertTrue( s.getCurrentLockMode(foo)==LockMode.UPGRADE );
}
s.delete(foo);
s.flush();
s.connection().commit();
s.close();
}
public void testRefreshProxy() throws Exception {
Session s = openSession();
Glarch g = new Glarch();
Serializable gid = s.save(g);
s.flush();
s.clear();
GlarchProxy gp = (GlarchProxy) s.load(Glarch.class, gid);
gp.getName(); //force init
s.refresh(gp);
s.delete(gp);
s.flush();
s.connection().commit();
s.close();
}
public void testListRemove() throws Exception {
Session s = openSession();
Baz b = new Baz();
List stringList = new ArrayList();
List feeList = new ArrayList();
b.setFees(feeList);
b.setStringList(stringList);
feeList.add( new Fee() );
feeList.add( new Fee() );
feeList.add( new Fee() );
feeList.add( new Fee() );
stringList.add("foo");
stringList.add("bar");
stringList.add("baz");
stringList.add("glarch");
s.save(b);
s.flush();
stringList.remove(1);
feeList.remove(1);
s.flush();
s.evict(b);
s.refresh(b);
assertTrue( b.getFees().size()==3 );
stringList = b.getStringList();
assertTrue(
stringList.size()==3 &&
"baz".equals( stringList.get(1) ) &&
"foo".equals( stringList.get(0) )
);
s.delete(b);
s.delete("from Fee");
s.flush();
s.connection().commit();
s.close();
}
public void testCollectionRefresh() 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);
s.refresh(c);
s.flush();
assertTrue( c.getSubcategories().size()==1 );
s.flush();
s.connection().commit();
s.close();
s = openSession();
c = (Category) s.load(Category.class, id);
assertTrue( c.getSubcategories().size()==1 );
s.delete(c);
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 testBlobClob() throws Exception {
Session s = openSession();
Blobber b = new Blobber();
b.setBlob( Hibernate.createBlob( "foo/bar/baz".getBytes() ) );
b.setClob( Hibernate.createClob("foo/bar/baz") );
s.save(b);
//s.refresh(b);
//assertTrue( b.getClob() instanceof ClobImpl );
s.flush();
s.refresh(b);
//b.getBlob().setBytes( 2, "abc".getBytes() );
b.getClob().getSubString(2, 3);
//b.getClob().setString(2, "abc");
s.flush();
s.connection().commit();
s.close();
s = openSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
Blobber b2 = new Blobber();
s.save(b2);
b2.setBlob( b.getBlob() );
b.setBlob(null);
//assertTrue( b.getClob().getSubString(1, 3).equals("fab") );
b.getClob().getSubString(1, 6);
//b.getClob().setString(1, "qwerty");
s.flush();
s.connection().commit();
s.close();
s = openSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
b.setClob( Hibernate.createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") );
s.flush();
s.connection().commit();
s.close();
s = openSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
assertTrue( b.getClob().getSubString(1, 7).equals("xcvfxvc") );
//b.getClob().setString(5, "1234567890");
s.flush();
s.connection().commit();
s.close();
/*InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc20.pdf");
s = sessionsopenSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
System.out.println( is.available() );
int size = is.available();
b.setBlob( Hibernate.createBlob( is, is.available() ) );
s.flush();
s.connection().commit();
ResultSet rs = s.connection().createStatement().executeQuery("select datalength(blob_) from blobber where id=" + b.getId() );
rs.next();
assertTrue( size==rs.getInt(1) );
rs.close();
s.close();
s = sessionsopenSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
File f = new File("C:/foo.pdf");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
Blob blob = b.getBlob();
byte[] bytes = blob.getBytes( 1, (int) blob.length() );
System.out.println( bytes.length );
fos.write(bytes);
fos.flush();
fos.close();
s.close();*/
}
public void testBlobClob() throws Exception {
Session s = openSession();
Blobber b = new Blobber();
b.setBlob( Hibernate.createBlob( "foo/bar/baz".getBytes() ) );
b.setClob( Hibernate.createClob("foo/bar/baz") );
s.save(b);
//s.refresh(b);
//assertTrue( b.getClob() instanceof ClobImpl );
s.flush();
s.refresh(b);
//b.getBlob().setBytes( 2, "abc".getBytes() );
log.debug("levinson: just bfore b.getClob()");
b.getClob().getSubString(2, 3);
//b.getClob().setString(2, "abc");
s.flush();
s.connection().commit();
s.close();
s = openSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
Blobber b2 = new Blobber();
s.save(b2);
b2.setBlob( b.getBlob() );
b.setBlob(null);
//assertTrue( b.getClob().getSubString(1, 3).equals("fab") );
b.getClob().getSubString(1, 6);
//b.getClob().setString(1, "qwerty");
s.flush();
s.connection().commit();
s.close();
s = openSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
b.setClob( Hibernate.createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") );
s.flush();
s.connection().commit();
s.close();
s = openSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
assertTrue( b.getClob().getSubString(1, 7).equals("xcvfxvc") );
//b.getClob().setString(5, "1234567890");
s.flush();
s.connection().commit();
s.close();
/*InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc20.pdf");
s = sessionsopenSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
System.out.println( is.available() );
int size = is.available();
b.setBlob( Hibernate.createBlob( is, is.available() ) );
s.flush();
s.connection().commit();
ResultSet rs = s.connection().createStatement().executeQuery("select datalength(blob_) from blobber where id=" + b.getId() );
rs.next();
assertTrue( size==rs.getInt(1) );
rs.close();
s.close();
s = sessionsopenSession();
b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
File f = new File("C:/foo.pdf");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
Blob blob = b.getBlob();
byte[] bytes = blob.getBytes( 1, (int) blob.length() );
System.out.println( bytes.length );
fos.write(bytes);
fos.flush();
fos.close();
s.close();*/
}
public void testUpdateOnComponent() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human human = new Human();
human.setName( new Name( "Stevee", 'X', "Ebersole" ) );
s.save( human );
s.flush();
t.commit();
String correctName = "Steve";
t = s.beginTransaction();
int count = s.createQuery( "update Human set name.first = :correction where id = :id" )
.setString( "correction", correctName )
.setLong( "id", human.getId().longValue() )
.executeUpdate();
assertEquals( "Incorrect update count", 1, count );
t.commit();
t = s.beginTransaction();
s.refresh( human );
assertEquals( "Update did not execute properly", correctName, human.getName().getFirst() );
s.createQuery( "delete Human" ).executeUpdate();
t.commit();
s.close();
}