0

我曾尝试运行 hibernatetool Ant 任务,但一直未成功。Help。这些罐子来自我的 eclipse(3.7.2) 休眠工具插件(3.4)。我得到一个 org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available 尽管设置了方言

这是我的 build.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>

还有我的 hibernate.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:\Users\mrtykop\workspace2\testjavahib\build.xml
parsing buildfile C:\Users\mrtykop\workspace2\testjavahib\build.xml with URI = file:/C:/Users/mrtykop/workspace2/testjavahib/build.xml
Project base dir set to: C:\Users\mrtykop\workspace2\testjavahib
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:\Users\mrtykop\workspace2\testjavahib\build.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
4

3 回答 3

4

这太不可思议了。阅读文档时的一个小错误会毁了你一整天。
我不得不用 propertyfile="hibernate.properties" 替换 propertyfile="src/hibernate.cfg.xml"。
(指定我也可以使用 hibernate.cfg 文件,但像这样:configurationfile =“src/hibernate.cfg.xml”)我还添加了另一个 jar。新的 build.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>

reveng.xml 文件只需使用 hibernatetools 插件对其进行编辑

有趣的是没有人注意到这个错误(不知道有多少人看过它)

顺便说一句,我的灵感来自这篇文章:Dialect not getting set in hibernate

于 2012-06-27T14:30:47.240 回答
2

试试这个

Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
于 2012-10-17T17:05:18.940 回答
0

对真正的问题一无所知,但我们很久以前就有了,据我所知,获取更新的 JDBC 驱动程序有所帮​​助。

编辑:

似乎这不是这个问题的正确答案。不过,我不会删除它,因为不久前我们遇到了同样的问题,而这个解决方案对此有所帮助。

对不起@geogeorge,我帮不上忙:(

于 2012-06-27T01:40:43.953 回答