我正在尝试通过Gavin King的hibernate从Java Persistence学习Hibernate。
我刚刚从此位置http://downloads.jboss.org/hibernate/caveatemptor/jpwh-gettingstarted-070401.zip下载了书中给出的示例
我使用ant startdb启动了HSQL数据库,数据库成功启动。
尝试运行目标schemaexport时,出现以下错误。
schemaexport:[hibernatetool]
使用以下命令执行休眠工具
标准配置[hibernatetool]
1.任务:hbm2ddl(生成数据库架构)[hibernatetool] 18:07:56,555
INFO环境:500-休眠
3.2.0.cr5 [hibernatetool] 18:07:56,555 INFO环境:533-
找不到hibernate.properties
[hibernatetool] 18:07:56,555信息
环境:667-字节码提供者
名称:cg lib [hibernatetool]
18:07:56,571 INFO环境:584-
使用JDK 1.4 java.sql.Time Stamp
处理[hibernatetool] 18:07:56,617
INFO配置:1384-配置
来自文件:h ibernate.cfg.xml
[hibernatetool] 18:07:56,680信息
配置:507-读取映射
来自resource:hello / Message.hbm.xml
[hibernatetool] 18:07:56,758信息
HbmBinder:300-映射类:
你好消息
->消息[hibernatetool] 18:07:56,851信息配置:1465
-配置的SessionFactor y:null [hibernatetool] 18:07:56,851信息
方言:141-使用方言:
org.hibernate.di alect.HSQLDialect
[hibernatetool] 18:07:56,914信息
SchemaExport:154-运行hbm2ddl
模式展示[hibernatetool]
18:07:56,914信息架构导出:174-
将生成的模式写入文件:
C:\ Users \ Ramesh \ Desktop \ Tutorials \ hibernate \ Hibernate
代码示例\ jpwh-
Gettingstarted-070401 \ helloworld-native \ helloworld-ddl.sql
[hibernatetool] 18:07:56,914信息
SchemaExport:179-生成导出
数据库架构[hibernatetool]
18:07:56,914信息
C3P0ConnectionProvider:50-使用C3P0
驱动程序:URL上的org.hsqldb.jdbcDriver:
jdbc:hsqldb:hsql:// localhost
[hibernatetool] 18:07:56,914信息
C3P0ConnectionProvider:51-连接
适当的纽带:{user = sa} [hibernatetool]
18:07:56,914信息
C3P0ConnectionProvider:54-自动提交
模式:false [hibernatetool]
18:08:26,554警告
BasicResourcePool:1222-
com.mchange.v2.resou
rcepool.BasicResourcePool$AcquireTask@61f1680f
-尝试获取失败!!!清算未完成的收购。而
试图获得所需的新
资源,我们辉导致更多成功
比允许的最大数量
尝试获取(30)。
[hibernatetool] 18:08:26,554错误
SchemaExport:202-模式导出
不成功
[hibernatetool] java.sql.SQLException:
无法从获得连接
基础数据库!
[hibernatetool]位于
com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.j
ava:104)[hibernatetool]在
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.che
ckoutPooledConnection(C3P0PooledConnectionPool.java:236)
[hibernatetool]位于
com.mchange.v2.c3p0.PoolBackedDataSource.getConnectio
n(PoolBackedDataSource.java:94)
[hibernatetool]位于
org.hibernate.connection.C3P0ConnectionProvider.getCo
nnection(C3P0ConnectionProvider.java:35)
[hibernatetool]位于
org.hibernate.tool.hbm2ddl.ManagedProviderConnectionH
elper.prepare(ManagedProviderConnectionHelper.java:28)
[hibernatetool]位于
org.hibernate.tool.hbm2ddl.SchemaExport.execute(Schem
aExport.java:180)[hibernatetool]
在
org.hibernate.tool.hbm2ddl.SchemaExport.create(Schema
Export.java:133)[hibernatetool]
在
org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hb
m2DDLExporterTask.java:55)
[hibernatetool]位于
org.hibernate.tool.ant.HibernateToolTask.execute(Hibe
rnateToolTask.java:171)
[hibernatetool]位于
org.apache.tools.ant.UnknownElement.execute(UnknownEl
ement.java:291)[hibernatetool]
在
sun.reflect.NativeMethodAccessorImpl.invoke0(本机
M ethod)[hibernatetool]在
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMet
hodAccessorImpl.java:39)
[hibernatetool]位于
sun.reflect.DelegatingMethodAccessorImpl.invoke(Deleg
atingMethodAccessorImpl.java:25)
[hibernatetool]位于
java.lang.reflect.Method.invoke(Method.java:597)
[hibernatetool]位于
org.apache.tools.ant.dispatch.DispatchUtils.execute(D
ispatchUtils.java:106)[hibernatetool]
在
org.apache.tools.ant.Task.perform(Task.java:348)
[hibernatetool]位于
org.apache.tools.ant.Target.execute(Target.java:390)
[hibernatetool]位于
org.apache.tools.ant.Target.performTasks(Target.java:
411)[hibernatetool]在
org.apache.tools.ant.Project.executeSortedTargets(Pro
ject.java:1397)[hibernatetool]
在
org.apache.tools.ant.Project.executeTarget(Project.ja
va:1366)[hibernatetool]在
org.apache.tools.ant.helper.DefaultExecutor.executeTa
rgets(DefaultExecutor.java:41)
[hibernatetool]位于
org.apache.tools.ant.Project.executeTargets(Project.j
ava:1249)[hibernatetool]在
org.apache.tools.ant.Main.runBuild(Main.java:801)
[hibernatetool]位于
org.apache.tools.ant.Main.startAnt(Main.java:218)
[hibernatetool]位于
org.apache.tools.ant.launch.Launcher.run(Launcher.jav
a:280)[hibernatetool]在
org.apache.tools.ant.launch.Launcher.main(Launcher.ja
va:109)[hibernatetool]原因:
com.mchange.v2.resourcepool.CannotAcquireResourceExce
说明:ResourcePool无法
从其主要资源获取资源
工厂或我们的来源。 [hibernatetool]
在
com.mchange.v2.resourcepool.BasicResourcePool.awaitAc
quire(BasicResourcePool.java:970)
[hibernatetool]位于
com.mchange.v2.resourcepool.BasicResourcePool.checkou
tResource(BasicResourcePool.java:208)
[hibernatetool]位于
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.che
ckoutPooledConnection(C3P0PooledConnectionPool.java:232)
[hibernatetool] ...另外24个
[hibernatetool]发生1个错误
表演时。
[hibernatetool]错误1:
java.sql.SQLException:连接
无法从
基础数据库! [hibernatetool]
18:08:26,569警告
BasicResourcePool:1222-
com.mchange.v2.resou
rcepool.BasicResourcePool$AcquireTask@72f864fe
-尝试获取失败!!!清算未完成的收购。而
试图获得所需的新
资源,我们辉导致更多成功
比允许的最大数量
尝试获取(30)。
我对冬眠或HSQL没有太多的了解。
有人请在这里解释怎么了?
谢谢
编辑:我正在尝试运行项目helloworld-native
。
ant build.xml中为startdb提供的目标如下:
<!-- Start the HSQL DB server -->
<target name="startdb" description="Run HSQL database server with clean DB">
<!-- Delete database files -->
<delete dir="${database.dir}"/>
<java classname="org.hsqldb.Server"
fork="yes"
classpathref="project.classpath"
failonerror="true">
<arg value="-database.0"/>
<arg value="file:${database.dir}/db"/>
</java>
</target>
当我运行命令
ant startdb
时,我看到以下输出:startdb:
[delete] Deleting directory C:\Users\Ramesh\Desktop\Tutorials\hibernate\Hibernate Code samples\jpwh-gettingstarted-070401\helloworld-native\database
[java] [Server@15ded0fd]: [Thread[main,5,main]]: checkRunning(false) entered
[java] [Server@15ded0fd]: [Thread[main,5,main]]: checkRunning(false) exited
[java] [Server@15ded0fd]: Startup sequence initiated from main() method
[java] [Server@15ded0fd]: Loaded properties from [C:\Users\Ramesh\Desktop\Tutorials hibernate\Hibernate Code samples\jpwh-gettingstarted-070401\helloworld-native\server.properties]
[java] [Server@15ded0fd]: Initiating startup sequence...
[java] [Server@15ded0fd]: Server socket opened successfully in 29 ms.
[java] [Server@15ded0fd]: Database [index=0, id=0, db=file:database/db, alias=] opened sucessfully in 469 ms.
[java] [Server@15ded0fd]: Startup sequence completed in 523 ms.
[java] [Server@15ded0fd]: 2010-11-08 22:11:08.027 HSQLDB server 1.8.0 is online
[java] [Server@15ded0fd]: To close normally, connect and execute SHUTDOWN SQL
[java] [Server@15ded0fd]: From command line, use [Ctrl]+[C] to abort abruptly
然后,我想我只在服务器模式下运行HSQL DB(从上面的输出)。
请告诉我我还缺少什么。
最佳答案
更新:经过重新查看,看来我错了,您可以使用ant脚本在服务器模式下启动HSQL,而不必强制使用命令行。实际上,我尝试使用helloworld-native
示例重现该问题,但对我来说效果很好。
首先,我使用ant startdb
在第一个shell中启动HSQLDB:
$ ant startdb
Buildfile: /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build.xml
startdb:
[java] [Server@a97b0b]: [Thread[main,5,main]]: checkRunning(false) entered
[java] [Server@a97b0b]: [Thread[main,5,main]]: checkRunning(false) exited
[java] [Server@a97b0b]: Startup sequence initiated from main() method
[java] [Server@a97b0b]: Loaded properties from [/home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/server.properties]
[java] [Server@a97b0b]: Initiating startup sequence...
[java] [Server@a97b0b]: Server socket opened successfully in 39 ms.
[java] [Server@a97b0b]: Database [index=0, id=0, db=file:database/db, alias=] opened sucessfully in 1148 ms.
[java] [Server@a97b0b]: Startup sequence completed in 1281 ms.
[java] [Server@a97b0b]: 2010-11-08 18:29:20.094 HSQLDB server 1.8.0 is online
[java] [Server@a97b0b]: To close normally, connect and execute SHUTDOWN SQL
[java] [Server@a97b0b]: From command line, use [Ctrl]+[C] to abort abruptly
然后,我从另一个shell运行
ant schemaexport
:$ ant schemaexport
Buildfile: /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build.xml
compile:
[mkdir] Created dir: /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build
[javac] /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build.xml:39: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 3 source files to /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build
copymetafiles:
[copy] Copying 3 files to /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build
schemaexport:
[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2ddl (Generates database schema)
[hibernatetool]
[hibernatetool] alter table MESSAGES
[hibernatetool] drop constraint FK_NEXT_MESSAGE;
[hibernatetool]
[hibernatetool] drop table MESSAGES if exists;
[hibernatetool]
[hibernatetool] create table MESSAGES (
[hibernatetool] MESSAGE_ID bigint generated by default as identity (start with 1),
[hibernatetool] MESSAGE_TEXT varchar(255),
[hibernatetool] NEXT_MESSAGE_ID bigint,
[hibernatetool] primary key (MESSAGE_ID)
[hibernatetool] );
[hibernatetool]
[hibernatetool] alter table MESSAGES
[hibernatetool] add constraint FK_NEXT_MESSAGE
[hibernatetool] foreign key (NEXT_MESSAGE_ID)
[hibernatetool] references MESSAGES;
[hibernatetool] 1 errors occurred while performing <hbm2ddl>.
[hibernatetool] Error #1: java.sql.SQLException: Table not found: MESSAGES in statement [alter table MESSAGES]
BUILD SUCCESSFUL
Total time: 14 seconds
确实,在运行
ant dbmanager
时,可以按预期看到MESSAGES
表。因此,一切正常,开箱即用。如果不适合您,则可能开始考虑诸如网络问题,个人防火墙等问题。
根据跟踪,看起来HSQL尚未启动(至少不是在服务器模式下),我怀疑您没有按预期的方式启动它。要以服务器模式启动它,请从
helloworld-reverse
目录运行(如启动HSQL数据库系统,第98页中所述):$ java -cp lib/hsqldb.jar org.hsqldb.Server
然后,您可以使用
jdbc:hsqldb:hsql://localhost
连接字符串连接到它。