下面列出了org.hibernate.classic.Session#load ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testPersistentLifecycle() throws Exception {
Session s = openSession();
Qux q = new Qux();
s.save(q);
q.setStuff("foo bar baz qux");
s.flush();
s.connection().commit();
s.close();
s = openSession();
q = (Qux) s.load( Qux.class, q.getKey() );
assertTrue( "lifecycle create", q.getCreated() );
assertTrue( "lifecycle load", q.getLoaded() );
assertTrue( "lifecycle subobject", q.getFoo()!=null );
s.delete(q);
assertTrue( "lifecycle delete", q.getDeleted() );
s.flush();
s.connection().commit();
s.close();
s = openSession();
assertTrue( "subdeletion", s.find("from Foo foo").size()==0);
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 testFindLoad() throws Exception {
Session s = openSession();
FooProxy foo = new Foo();
s.save(foo);
s.flush();
s.connection().commit();
s.close();
s = openSession();
foo = (FooProxy) s.find("from Foo foo").get(0);
FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
assertTrue("find returns same object as load", foo==foo2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
foo = (FooProxy) s.find("from Foo foo").get(0);
assertTrue("find returns same object as load", foo==foo2);
s.delete("from Foo foo");
s.flush();
s.connection().commit();
s.close();
}
public void testSaveFlush() throws Exception {
Session s = openSession();
Fee fee = new Fee();
s.save( fee, "key" );
fee.setFi("blah");
s.flush();
s.connection().commit();
s.close();
s = openSession();
fee = (Fee) s.load( Fee.class, fee.getKey() );
assertTrue( "blah".equals( fee.getFi() ) );
assertTrue( "key".equals( fee.getKey() ) );
s.delete(fee);
s.flush();
s.connection().commit();
s.close();
}
public void testCache() throws Exception {
Session s = openSession();
Immutable im = new Immutable();
s.save(im);
s.flush();
s.connection().commit();
s.close();
s = openSession();
s.load( im, im.getId() );
s.connection().commit();
s.close();
s = openSession();
s.load( im, im.getId() );
assertTrue(
"cached object identity",
s.find(
"from Immutable im where im = ?",
im,
Hibernate.entity(Immutable.class)
).get(0)==im &&
im == s.load( Immutable.class, im.getId() )
);
s.connection().createStatement().executeUpdate("delete from immut");
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 testManyToOne() throws Exception {
Session s = openSession();
One one = new One();
s.save(one);
one.setValue("yada");
Many many = new Many();
many.setOne(one);
s.save(many);
s.flush();
s.connection().commit();
s.close();
s = openSession();
one = (One) s.load( One.class, one.getKey() );
one.getManies().size();
s.connection().commit();
s.close();
s = openSession();
many = (Many) s.load( Many.class, many.getKey() );
assertTrue( "many-to-one assoc", many.getOne()!=null );
s.delete( many.getOne() );
s.delete(many);
s.flush();
s.connection().commit();
s.close();
}
public void testMultiTableManyToOne() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
assertTrue( s.find("from Top").size()==0 );
Multi multi = new Multi();
multi.setExtraProp("extra");
multi.setName("name");
Top simp = new Top();
simp.setDate( new Date() );
simp.setName("simp");
s.save(multi);
Lower ls = new Lower();
ls.setOther(ls);
ls.setAnother(multi);
ls.setYetanother(ls);
ls.setName("Less Simple");
Serializable id = s.save(ls);
t.commit();
s.close();
assertTrue( ls.getOther()==ls && ls.getAnother()==multi && ls.getYetanother()==ls );
s = openSession();
t = s.beginTransaction();
ls = (Lower) s.load(Lower.class, id);
assertTrue( ls.getOther()==ls && ls.getYetanother()==ls );
assertTrue( ls.getAnother().getName().equals("name") && ls.getAnother() instanceof Multi );
s.delete(ls);
s.delete( ls.getAnother() );
t.commit();
s.close();
}
public void testFetchInitializedCollectionDupe() throws Exception {
Session s = openSession();
Baz baz = new Baz();
Collection fooBag = new ArrayList();
fooBag.add( new Foo() );
fooBag.add( new Foo() );
baz.setFooBag(fooBag);
s.save(baz);
s.flush();
fooBag = baz.getFooBag();
s.find("from Baz baz left join fetch baz.fooBag");
assertTrue( Hibernate.isInitialized(fooBag) );
assertTrue( fooBag==baz.getFooBag() );
assertTrue( baz.getFooBag().size()==2 );
s.connection().commit();
s.close();
s = openSession();
baz = (Baz) s.load( Baz.class, baz.getCode() );
Object bag = baz.getFooBag();
assertFalse( Hibernate.isInitialized(bag) );
s.find("from Baz baz left join fetch baz.fooBag");
assertTrue( Hibernate.isInitialized(bag) );
assertTrue( bag==baz.getFooBag() );
assertTrue( baz.getFooBag().size()==2 );
s.delete(baz);
s.flush();
s.connection().commit();
s.close();
}
public void testInterface() throws Exception {
Session s = openSession();
Serializable id = s.save( new BasicNameable() );
s.flush();
s.connection().commit();
s.close();
s = openSession();
Nameable n = (Nameable) s.load(Nameable.class, id);
s.delete(n);
s.flush();
s.connection().commit();
s.close();
}
public void testCustom() throws Exception {
GlarchProxy g = new Glarch();
Multiplicity m = new Multiplicity();
m.count = 12;
m.glarch = (Glarch) g;
g.setMultiple(m);
Session s = openSession();
Serializable gid = s.save(g);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (Glarch) s.find("from Glarch g where g.multiple.count=12").get(0);
s.connection().commit();
s.close();
s = openSession();
g = (Glarch) s.find("from Glarch g where g.multiple.glarch=g and g.multiple.count=12").get(0);
assertTrue( g.getMultiple()!=null );
assertEquals( g.getMultiple().count, 12 );
assertSame(g.getMultiple().glarch, g);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( g.getMultiple()!=null );
assertEquals( g.getMultiple().count, 12 );
assertSame(g.getMultiple().glarch, g);
s.delete(g);
s.flush();
s.connection().commit();
s.close();
}
public void testUpdateOnAnimal() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction t = s.beginTransaction();
int count = s.createQuery( "update Animal set description = description where description = :desc" )
.setString( "desc", data.frog.getDescription() )
.executeUpdate();
assertEquals( "Incorrect entity-updated count", 1, count );
count = s.createQuery( "update Animal set description = :newDesc where description = :desc" )
.setString( "desc", data.polliwog.getDescription() )
.setString( "newDesc", "Tadpole" )
.executeUpdate();
assertEquals( "Incorrect entity-updated count", 1, count );
Animal tadpole = ( Animal ) s.load( Animal.class, data.polliwog.getId() );
assertEquals( "Update did not take effect", "Tadpole", tadpole.getDescription() );
count = s.createQuery( "update Animal set bodyWeight = bodyWeight + :w1 + :w2" )
.setDouble( "w1", 1 )
.setDouble( "w2", 2 )
.executeUpdate();
assertEquals( "incorrect count on 'complex' update assignment", count, 6 );
if ( ! ( getDialect() instanceof MySQLDialect ) ) {
// MySQL does not support (even un-correlated) subqueries against the update-mutating table
s.createQuery( "update Animal set bodyWeight = ( select max(bodyWeight) from Animal )" )
.executeUpdate();
}
t.commit();
s.close();
data.cleanup();
}
public void testCreateUpdate() throws Exception {
Session s = openSession();
Foo foo = new Foo();
s.save(foo);
foo.setString("dirty");
s.flush();
s.connection().commit();
s.close();
s = openSession();
Foo foo2 = new Foo();
s.load( foo2, foo.getKey() );
// There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
assertTrue( "create-update", foo.equalsFoo(foo2) );
//System.out.println( s.print(foo2) );
s.delete(foo2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
foo = new Foo();
s.save(foo, "assignedid");
foo.setString("dirty");
s.flush();
s.connection().commit();
s.close();
s = openSession();
s.load(foo2, "assignedid");
// There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
assertTrue( "create-update", foo.equalsFoo(foo2) );
//System.out.println( s.print(foo2) );
s.delete(foo2);
s.flush();
s.connection().commit();
s.close();
}
public void testCompositeID() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
Fum fum = new Fum( fumKey("fum") );
fum.setFum("fee fi fo");
s.save(fum);
assertTrue( "load by composite key", fum==s.load( Fum.class, fumKey("fum") ) );
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
fum = (Fum) s.load( Fum.class, fumKey("fum"), LockMode.UPGRADE );
assertTrue( "load by composite key", fum!=null );
Fum fum2 = new Fum( fumKey("fi") );
fum2.setFum("fee fo fi");
fum.setFo(fum2);
s.save(fum2);
assertTrue(
"find composite keyed objects",
s.find("from Fum fum where not fum.fum='FRIEND'").size()==2
);
assertTrue(
"find composite keyed object",
s.find("select fum from Fum fum where fum.fum='fee fi fo'").get(0)==fum
);
fum.setFo(null);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
Iterator iter = s.iterate("from Fum fum where not fum.fum='FRIEND'");
int i = 0;
while ( iter.hasNext() ) {
fum = (Fum) iter.next();
//iter.remove();
s.delete(fum);
i++;
}
assertTrue( "iterate on composite key", i==2 );
txn.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 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 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 testVersioning() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
GlarchProxy g = new Glarch();
s.save(g);
GlarchProxy g2 = new Glarch();
s.save(g2);
Serializable gid = s.getIdentifier(g);
Serializable g2id = s.getIdentifier(g2);
g.setName("glarch");
txn.commit();
s.close();
getSessions().evict(Glarch.class);
s = openSession();
txn = s.beginTransaction();
g = (GlarchProxy) s.load( Glarch.class, gid );
s.lock(g, LockMode.UPGRADE);
g2 = (GlarchProxy) s.load( Glarch.class, g2id );
assertTrue( "version", g.getVersion()==1 );
assertTrue( "version", g.getDerivedVersion()==1 );
assertTrue( "version", g2.getVersion()==0 );
g.setName("foo");
assertTrue(
"find by version",
s.find("from Glarch g where g.version=2").size()==1
);
g.setName("bar");
txn.commit();
s.close();
getSessions().evict(Glarch.class);
s = openSession();
txn = s.beginTransaction();
g = (GlarchProxy) s.load( Glarch.class, gid );
g2 = (GlarchProxy) s.load( Glarch.class, g2id );
assertTrue( "version", g.getVersion()==3 );
assertTrue( "version", g.getDerivedVersion()==3 );
assertTrue( "version", g2.getVersion()==0 );
g.setNext(null);
g2.setNext(g);
s.delete(g2);
s.delete(g);
txn.commit();
s.close();
}
public void testSubclassing() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
C1 c1 = new C1();
D d = new D();
d.setAmount(213.34f);
c1.setAddress("foo bar");
c1.setCount(23432);
c1.setName("c1");
c1.setBName("a funny name");
c1.setD(d);
s.save(c1);
d.setId( c1.getId() );
s.save(d);
assertTrue( s.find("from C2 c where 1=1 or 1=1").size()==0 );
t.commit();
s.close();
getSessions().evict(A.class);
s = openSession();
t = s.beginTransaction();
c1 = (C1) s.get( A.class, c1.getId() );
assertTrue(
c1.getAddress().equals("foo bar") &&
(c1.getCount()==23432) &&
c1.getName().equals("c1") &&
c1.getD().getAmount()>213.3f
);
assertEquals( "a funny name", c1.getBName() );
t.commit();
s.close();
getSessions().evict(A.class);
s = openSession();
t = s.beginTransaction();
c1 = (C1) s.get( B.class, c1.getId() );
assertTrue(
c1.getAddress().equals("foo bar") &&
(c1.getCount()==23432) &&
c1.getName().equals("c1") &&
c1.getD().getAmount()>213.3f
);
assertEquals( "a funny name", c1.getBName() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
c1 = (C1) s.load( C1.class, c1.getId() );
assertTrue(
c1.getAddress().equals("foo bar") &&
(c1.getCount()==23432) &&
c1.getName().equals("c1") &&
c1.getD().getAmount()>213.3f
);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
List bs = s.createQuery("from B").list();
for (int i=0; i<bs.size(); i++) {
C1 b = (C1) bs.get(i);
s.delete(b);
s.delete( b.getD() );
}
t.commit();
s.close();
}
public void ntestAssociationId() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Bar bar = new Bar();
String id = (String) s.save(bar);
MoreStuff more = new MoreStuff();
more.setName("More Stuff");
more.setIntId(12);
more.setStringId("id");
Stuff stuf = new Stuff();
stuf.setMoreStuff(more);
more.setStuffs( new ArrayList() );
more.getStuffs().add(stuf);
stuf.setFoo(bar);
stuf.setId(1234);
stuf.setProperty( TimeZone.getDefault() );
s.save(more);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
assertTrue( s.find(
"from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.id.intId = ? and s.moreStuff.id.stringId = ?",
new Object[] { bar, new Long(1234), new Integer(12), "id" },
new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.INTEGER, Hibernate.STRING }
).size()==1 );
assertTrue( s.find(
"from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.name = ?",
new Object[] { bar, new Long(1234), "More Stuff" },
new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.STRING }
).size()==1 );
s.find("from Stuff as s where s.foo.string is not null");
assertTrue(
s.find("from Stuff as s where s.foo > '0' order by s.foo").size()==1
);
//s.createCriteria(Stuff.class).createCriteria("id.foo").add( Expression.isNull("foo") ).list();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
FooProxy foo = (FooProxy) s.load(Foo.class, id);
s.load(more, more);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Stuff stuff = new Stuff();
stuff.setFoo(foo);
stuff.setId(1234);
stuff.setMoreStuff(more);
s.load(stuff, stuff);
assertTrue( stuff.getProperty().equals( TimeZone.getDefault() ) );
assertTrue( stuff.getMoreStuff().getName().equals("More Stuff") );
s.delete("from MoreStuff");
s.delete("from Foo foo");
t.commit();
s.close();
}