学习就从官方的测试开始,但不知道怎么回事官方的测试有问题,这我就把我配置的简单junit环境共享给大家把!
1.下载 hibernate-annotations-3.4.0.GA 并解压 ,cd 到解压后目录下
2. $> ant junit (天知道,怎么官方出的东西测试不能通过!!)
不要急,我们来看错误 (错误提示有删减)
错误-1:$>build.xml:95: ./jdbc not found.
解决办法-1: $>mkdir ./jdbc
3. $> ant junit
错误-2:
./build.xml:124: The following error occurred while executing this line:
./build.xml:126: The following error occurred while executing this line:
./common-build.xml:349: .\src\filters\hsqldb.filter as it doesn't exist.
解决办法-2:
$> mkdir ./src/filters ;
$> echo "" > ./src/filters/hsqldb.filter;
4. $>ant junit
错误-3:
./build.xml:124: The following error occurred while executing this line:
./build.xml:126: The following error occurred while executing this line:
./common-build.xml:351: .\src\test-resources not found.
$> mkdir ./src/test-resources ;
5. $> ant junit
错误-4:没有ant 错误了!,但测试都为错误,那我们就看看 第一个测试吧,找下原因!
test-resources:
[mkdir] Created dir: hibernate-anntations-3.4.0.GA\build\test-reports\hsqldb
[echo] Running against db: hsqldb
[junit] Running org.hibernate.test.annotations.ConfigurationTest
[junit] Tests run: 7, Failures: 0, Errors: 6, Time elapsed: 1.218 sec
.............
$> find . -name "ConfigurationTest.java" #pwd /hibernate-anntations-3.4.0.GA
./test/org/hibernate/test/annotations/configuration/ConfigurationTest.java
./test/org/hibernate/test/annotations/ConfigurationTest.java
根据路径 org.hibernate.test.annotations.ConfigurationTest
gvim ./test/org/hibernate/test/annotations/ConfigurationTest.java
发现第20行: cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
感觉应该没有配置 jdbc
gvim ./test/org/hibernate/test/annotations/hibernate.cfg.xml #果然 没配置
我这就放个我的简单配置上来(使用 hbm hsqldb 内存数据库测 试 ):
<!
DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< hibernate-configuration >
< session-factory >
< property name ="hibernate.dialect" > org.hibernate.dialect.HSQLDialect </ property >
< property name ="connection.driver_class" > org.hsqldb.jdbcDriver </ property >
< property name ="connection.url" > jdbc:hsqldb:mem:hbn_ann </ property >
< property name ="connection.username" > sa </ property >
< property name ="connection.password" ></ property >
< mapping resource ="org/hibernate/test/annotations/Boat.hbm.xml" />
< mapping package ="org.hibernate.test.annotations" />
< mapping class ="org.hibernate.test.annotations.Plane" />
</ session-factory >
</ hibernate-configuration >
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< hibernate-configuration >
< session-factory >
< property name ="hibernate.dialect" > org.hibernate.dialect.HSQLDialect </ property >
< property name ="connection.driver_class" > org.hsqldb.jdbcDriver </ property >
< property name ="connection.url" > jdbc:hsqldb:mem:hbn_ann </ property >
< property name ="connection.username" > sa </ property >
< property name ="connection.password" ></ property >
< mapping resource ="org/hibernate/test/annotations/Boat.hbm.xml" />
< mapping package ="org.hibernate.test.annotations" />
< mapping class ="org.hibernate.test.annotations.Plane" />
</ session-factory >
</ hibernate-configuration >
我这就是要hsqldb吧,既然上面错误1有 jdbc 文件夹,那就是放 jdbc jar 的地方
$> cp ../hsqldb.jar ./jdbc
6. $> ant junit
呵呵 测试 第一个正确了 ! (目前配置后,测试环境并不是全部都正确,比如第2个测试)
其他测试错误日志在 .\build\test-reports\hsqldb ...
比如第2个错误 异常部分
org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
at org.hibernate.dialect.Dialect.instantiateDialect(Dialect.java:256)
at org.hibernate.dialect.Dialect.getDialect(Dialect.java:234)
........ 大家一边学习,一边配置测试 环境吧。
错误日记 都会在 build\test-reports\hsqldb下