hibernate的单步测试。
看了hibernate的大部分文档,像把\src\net\sf\hibernate\test
里的例子运行一遍,刚开始我是用main写的,后来觉得太麻烦,于是就学了
junit。可是运行它自带的例子就出错了。
如NewPerformanceTest:
import java.io.Serializable;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import net.sf.hibernate.Session;
public class NewPerformanceTest extends TestCase {
public NewPerformanceTest(String arg0) {
super(arg0);
}
public void testPerformance() throws Exception {
for ( int n=2; n<4000; n*=2 ) {
Simple[] simples = new Simple[n];
Serializable[] ids = new Serializable[n];
for ( int i=0; i<n; i++ ) {
simples = new Simple();
simples.init();
simples.setCount(i);
ids = new Long(i);
}
Session s = sessions.openSession();
prepare(s, simples, ids, n);
s.close();
long find = 0;
long flush = 0;
for ( int i=0; i<100; i++ ) {
s = sessions.openSession();
long time = System.currentTimeMillis();
List list = s.find("from s in class Simple where not s.name='osama bin laden' and s.other is null");
find += System.currentTimeMillis() - time;
assertTrue( list.size()==n );
time = System.currentTimeMillis();
s.flush();
flush += System.currentTimeMillis() - time;
time = System.currentTimeMillis();
s.connection().commit();
find += System.currentTimeMillis() - time;
s.close();
}
System.out.println( "Objects: " + n + " - find(): " + find + "ms / flush(): " + flush + "ms / Ratio: " + ( (float) flush )/find );
s = sessions.openSession();
delete(s);
s.close();
}
}
private void prepare(Session s, Simple[] simples, Serializable[] ids, int N) throws Exception {
for ( int i=0; i<N; i++ ) {
s.save( simples, ids );
}
s.flush();
s.connection().commit();
}
private void delete(Session s) throws Exception {
s.delete("from s in class Simple");
s.flush();
s.connection().commit();
}
public static Test suite() throws Exception {
TestCase.exportSchema( new String[] { "Simple.hbm.xml" } );
return new TestSuite(NewPerformanceTest.class);
}
}
/////////////////////////////////////
jbuilder 提示错误:
sessions(没有定义),TestCase.exportSchema( new String[] { "Simple.hbm.xml" } );(无该方法)。
晕。