下面列出了org.hibernate.classic.Session#getIdentifier ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testForceOuterJoin() throws Exception {
if ( isOuterJoinFetchingDisabled() ) return;
Session s = openSession();
Glarch g = new Glarch();
FooComponent fc = new FooComponent();
fc.setGlarch(g);
FooProxy f = new Foo();
FooProxy f2 = new Foo();
f.setComponent(fc);
f.setFoo(f2);
s.save(f2);
Serializable id = s.save(f);
Serializable gid = s.getIdentifier( f.getComponent().getGlarch() );
s.flush();
s.connection().commit();
s.close();
getSessions().evict(Foo.class);
s = openSession();
f = (FooProxy) s.load(Foo.class, id);
assertFalse( Hibernate.isInitialized(f) );
assertTrue( Hibernate.isInitialized( f.getComponent().getGlarch() ) ); //outer-join="true"
assertFalse( Hibernate.isInitialized( f.getFoo() ) ); //outer-join="auto"
assertEquals( s.getIdentifier( f.getComponent().getGlarch() ), gid );
s.delete(f);
s.delete( f.getFoo() );
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 testIncomingOutgoing() throws Exception {
Session s = openSession();
Master master1 = new Master();
Master master2 = new Master();
Master master3 = new Master();
s.save(master1);
s.save(master2);
s.save(master3);
master1.addIncoming(master2);
master2.addOutgoing(master1);
master1.addIncoming(master3);
master3.addOutgoing(master1);
Serializable m1id = s.getIdentifier(master1);
assertTrue( s.filter( master1.getIncoming(), "where this.id > 0 and this.name is not null").size()==2 );
s.flush();
s.connection().commit();
s.close();
s = openSession();
master1 = (Master) s.load(Master.class, m1id);
Iterator iter = master1.getIncoming().iterator();
int i=0;
while ( iter.hasNext() ) {
Master m = (Master) iter.next();
assertTrue( "outgoing", m.getOutgoing().size()==1 );
assertTrue( "outgoing", m.getOutgoing().contains(master1) );
s.delete(m);
i++;
}
assertTrue( "incoming-outgoing", i==2 );
s.delete(master1);
s.flush();
s.connection().commit();
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 testVersionedCollections() throws Exception {
Session s = openSession();
GlarchProxy g = new Glarch();
s.save(g);
g.setProxyArray( new GlarchProxy[] { g } );
String gid = (String) s.getIdentifier(g);
ArrayList list = new ArrayList();
list.add("foo");
g.setStrings(list);
HashSet set = new HashSet();
set.add(g);
g.setProxySet(set);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( g.getStrings().size()==1 );
assertTrue( g.getProxyArray().length==1 );
assertTrue( g.getProxySet().size()==1 );
assertTrue( "versioned collection before", g.getVersion()==1 );
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( g.getStrings().get(0).equals("foo") );
assertTrue( g.getProxyArray()[0]==g );
assertTrue( g.getProxySet().iterator().next()==g );
assertTrue( "versioned collection before", g.getVersion()==1 );
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( "versioned collection before", g.getVersion()==1 );
g.getStrings().add("bar");
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( "versioned collection after", g.getVersion()==2 );
assertTrue( "versioned collection after", g.getStrings().size()==2 );
g.setProxyArray(null);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( "versioned collection after", g.getVersion()==3 );
assertTrue( "versioned collection after", g.getProxyArray().length==0 );
g.setFooComponents( new ArrayList() );
g.setProxyArray(null);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( "versioned collection after", g.getVersion()==4 );
s.delete(g);
s.flush();
assertTrue( s.find("from java.lang.Object").size()==0 );
s.connection().commit();
s.close();
}