我试图运行一个休眠工具 Ant 任务,但我一直没有成功。帮助。这些罐子来自我的 eclipse(3.7.2) 休眠工具插件(3.4)。 我得到一个org.hibernate.HibernateException:尽管设置了方言,但没有可用的连接时,必须设置"hibernate.dialect"
这是我的构建.xml:
<project>
<target name="genero">
<path id="toolslib">
<path location="mylibs/hibernate-tools.jar" />
<path location="mylibs/hibernate3.jar" />
<path location="mylibs/freemarker.jar" />
<path location="mylibs/postgresql-9.0-802.jdbc4.jar" />
<path location="mylibs/dom4j-1.6.1.jar" />
<path location="mylibs/commons-logging-1.0.4.jar" />
<path location="mylibs/slf4j-api-1.5.8.jar" />
<path location="mylibs/slf4j-log4j12-1.5.8.jar" />
<path location="mylibs/log4j-1.2.15.jar" />
<path location="mylibs/cglib-2.2.jar" />
<path location="mylibs/asm.jar" />
<path location="mylibs/commons-collections-3.1.jar" />
<path location="mylibs/slf4j-api-1.5.8.jar" />
</path>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />
<hibernatetool>
<jdbcconfiguration propertyfile="src/hibernate.cfg.xml" packagename="demo" revengfile="src/hibernate.reveng.xml" />
<hbm2hbmxml destdir="${build.dir}/src" />
<hbm2java jdk5="true" destdir="${build.dir}/src" />
</hibernatetool>
</target>
还有我的冬眠.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!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.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">blablabla</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/projteste</property>
<property name="hibernate.connection.username">postgres</property>
</session-factory>
</hibernate-configuration>
和堆栈跟踪:
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Buildfile: C:Usersmrtykopworkspace2testjavahibbuild.xml
parsing buildfile C:Usersmrtykopworkspace2testjavahibbuild.xml with URI = file:/C:/Users/mrtykop/workspace2/testjavahib/build.xml
Project base dir set to: C:Usersmrtykopworkspace2testjavahib
Build sequence for target(s) `genero' is [genero]
Complete build sequence is [genero, ]
genero:
parsing buildfile jar:file:/E:/jave/eclipse/plugins/org.apache.ant_1.8.2.v20120109-1030/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/E:/jave/eclipse/plugins/org.apache.ant_1.8.2.v20120109-1030/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
Property "build.dir" has not been set
Property "build.dir" has not been set
[hibernatetool] Executing Hibernate Tool with a JDBC Configuration (for reverse engineering)
[hibernatetool] 1. task: hbm2hbmxml (Generates a set of hbm.xml files)
[hibernatetool] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[hibernatetool] log4j:WARN Please initialize the log4j system properly.
[hibernatetool] An exception occurred while running exporter #2:hbm2hbmxml (Generates a set of hbm.xml files)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
BUILD FAILED
C:Usersmrtykopworkspace2testjavahibbuild.xml:26: org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
at org.hibernate.tool.ant.HibernateToolTask.reportException(HibernateToolTask.java:226)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:189)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
Caused by: org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
at org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:107)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:142)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2163)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2155)
at org.hibernate.cfg.JDBCMetaDataConfiguration.readFromJDBC(JDBCMetaDataConfiguration.java:41)
at org.hibernate.tool.ant.JDBCConfigurationTask.doConfiguration(JDBCConfigurationTask.java:83)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:55)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:302)
at org.hibernate.tool.ant.HibernateToolTask.getProperties(HibernateToolTask.java:318)
at org.hibernate.tool.ant.ExporterTask.configureExporter(ExporterTask.java:94)
at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:39)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
... 16 more
F-in不可思议的。阅读文档的一个小错误会毁了你的一整天。
我不得不将propertyfile="src/hibernate.cfg.xml"替换为propertyfile="hibernate.properties"。
(指定我也可以使用 hibernate.cfg 文件,但像这样:配置文件="src/hibernate.cfg.xml")我还加了另一个罐子。新版本.xml
<project>
<target name="genero">
<path id="toolslib">
<path location="mylibs/hibernate-tools.jar" />
<path location="mylibs/hibernate3.jar" />
<path location="mylibs/freemarker.jar" />
<path location="mylibs/postgresql-9.0-802.jdbc3.jar" />
<path location="mylibs/dom4j-1.6.1.jar" />
<path location="mylibs/commons-logging-1.0.4.jar" />
<path location="mylibs/slf4j-api-1.5.8.jar" />
<path location="mylibs/slf4j-log4j12-1.5.8.jar" />
<path location="mylibs/log4j-1.2.15.jar" />
<path location="mylibs/cglib-2.2.jar" />
<path location="mylibs/asm.jar" />
<path location="mylibs/commons-collections-3.1.jar" />
<path location="mylibs/jtidy-r8-20060801.jar"/>
</path>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />
<hibernatetool>
<jdbcconfiguration configurationfile="src/hibernate.cfg.xml" revengfile="hibernate.reveng.xml" packagename="demo" />
<hbm2hbmxml destdir="src" />
<hbm2java jdk5="true" destdir="src" />
</hibernatetool>
</target>
</project>
复仇.xml文件只需使用Hibernatetools插件对其进行编辑
有趣的是没有人注意到这个错误(想知道有多少人看过它)
顺便说一句,我的灵感来自这篇文章:方言没有进入冬眠
试试这个
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
对真正的问题一无所知,但我们很久以前就有了,据我所知,获得更新的 JDBC 驱动程序有所帮助。
编辑:
似乎这不是这个问题的正确答案。不过我不会删除它,因为不久前我们遇到了同样的问题,这个解决方案在那里有所帮助。
对不起@geogeorge我忍不住:(