下面列出了org.hibernate.classic.Session#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testReturnPropertyComponentRenameFailureExpected() throws HibernateException, SQLException {
// failure expected because this was a regression introduced previously which needs to get tracked down.
Session session = openSession();
Componentizable componentizable = setupComponentData(session);
Query namedQuery = session.getNamedQuery("queryComponentWithOtherColumn");
List list = namedQuery.list();
assertEquals(1, list.size());
assertEquals( "flakky comp", ( (Componentizable) list.get(0) ).getComponent().getName() );
session.clear();
session.delete(componentizable);
session.flush();
session.connection().commit();
session.close();
}
private Componentizable setupComponentData(Session session) throws SQLException {
Componentizable c = new Componentizable();
c.setNickName("Flacky");
Component component = new Component();
component.setName("flakky comp");
SubComponent subComponent = new SubComponent();
subComponent.setSubName("subway");
component.setSubComponent(subComponent);
c.setComponent(component);
session.save(c);
session.flush();
session.connection().commit();
session.clear();
return c;
}
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 testFindBySQLDiscriminatedSameSession() throws Exception {
Session session = openSession();
session.delete("from A");
A savedA = new A();
session.save(savedA);
B savedB = new B();
session.save(savedB);
session.flush();
Query query = session.createSQLQuery("select identifier_column as {a.id}, clazz_discriminata as {a.class}, name as {a.name}, count_ as {a.count} from TA {a}", "a", A.class);
List list = query.list();
assertNotNull(list);
assertEquals(2, list.size());
A a1 = (A) list.get(0);
A a2 = (A) list.get(1);
assertTrue((a2 instanceof B) || (a1 instanceof B));
assertFalse(a1 instanceof B && a2 instanceof B);
if (a1 instanceof B) {
assertSame(a1, savedB);
assertSame(a2, savedA);
}
else {
assertSame(a2, savedB);
assertSame(a1, savedA);
}
session.clear();
List list2 = session.getNamedQuery("propertyResultDiscriminator").list();
assertEquals(2, list2.size());
session.connection().commit();
session.close();
}
public void testFormulaAssociation() throws Throwable {
Session s = openSession();
Transaction t = s.beginTransaction();
D d = new D();
Long did = new Long(12);
s.save(d, did);
A a = new A();
a.setName("a");
s.save(a, did);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
d = (D) s.get(D.class, did);
assertTrue(d.getReverse().getId().equals(did));
s.clear();
getSessions().evict(D.class);
getSessions().evict(A.class);
d = (D) s.get(D.class, did);
assertTrue(d.inverse.getId().equals(did));
assertTrue(d.inverse.getName().equals("a"));
s.clear();
getSessions().evict(D.class);
getSessions().evict(A.class);
assertTrue( s.find("from D d join d.reverse r join d.inverse i where i = r").size()==1 );
t.commit();
s.close();
}
public void testBooleanHandling() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction t = s.beginTransaction();
// currently, we need the three different binds because they are different underlying types...
int count = s.createQuery( "update BooleanLiteralEntity set yesNoBoolean = :b1, trueFalseBoolean = :b2, zeroOneBoolean = :b3" )
.setBoolean( "b1", true )
.setBoolean( "b2", true )
.setBoolean( "b3", true )
.executeUpdate();
assertEquals( 1, count );
BooleanLiteralEntity entity = ( BooleanLiteralEntity ) s.createQuery( "from BooleanLiteralEntity" ).uniqueResult();
assertTrue( entity.isYesNoBoolean() );
assertTrue( entity.isTrueFalseBoolean() );
assertTrue( entity.isZeroOneBoolean() );
s.clear();
count = s.createQuery( "update BooleanLiteralEntity set yesNoBoolean = true, trueFalseBoolean = true, zeroOneBoolean = true" )
.executeUpdate();
assertEquals( 1, count );
entity = ( BooleanLiteralEntity ) s.createQuery( "from BooleanLiteralEntity" ).uniqueResult();
assertTrue( entity.isYesNoBoolean() );
assertTrue( entity.isTrueFalseBoolean() );
assertTrue( entity.isZeroOneBoolean() );
t.commit();
s.close();
data.cleanup();
}
public void testEmbeddedCompositeProperties() throws HibernateException, SQLException {
Session session = openSession();
Single s = new Single();
s.setId("my id");
s.setString("string 1");
session.save(s);
session.flush();
session.connection().commit();
session.clear();
Query query = session.createSQLQuery("select {sing.*} from Single {sing}", "sing", Single.class);
List list = query.list();
assertTrue(list.size()==1);
session.clear();
query = session.createSQLQuery("select {sing.*} from Single {sing} where sing.id = ?", "sing", Single.class);
query.setString(0, "my id");
list = query.list();
assertTrue(list.size()==1);
session.clear();
query = session.createSQLQuery("select s.id as {sing.id}, s.string_ as {sing.string}, s.prop as {sing.prop} from Single s where s.id = ?", "sing", Single.class);
query.setString(0, "my id");
list = query.list();
assertTrue(list.size()==1);
session.clear();
query = session.createSQLQuery("select s.id as {sing.id}, s.string_ as {sing.string}, s.prop as {sing.prop} from Single s where s.id = ?", "sing", Single.class);
query.setString(0, "my id");
list = query.list();
assertTrue(list.size()==1);
session.connection().commit();
session.close();
}