"' hibernate.dialect'当没有连接可用时必须设置"尽管设置了方言,但错误

时间:2012-06-27 01:33:18

标签: hibernate ant code-generation hibernate-tools hbm2java

我试过运行一个hibernatetool Ant任务,但我已经unuccesfull.Help。这些罐子来自我的eclipse(3.7.2)hibernate工具插件(3.4)。我得到一个org.hibernate.HibernateException:尽管设置了方言,但没有连接可用时必须设置'hibernate.dialect'

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

3 个答案:

答案 0 :(得分:4)

这是F-in令人难以置信的。阅读文档时的一个小错误将毁掉你的整个一天 我不得不用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插件编辑它

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

BTW我的灵感来自这篇文章:Dialect not getting set in hibernate

答案 1 :(得分:2)

尝试使用

Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");

答案 2 :(得分:0)

对于真正的问题没有任何线索,但是我们很久以前就已经知道了这一点,并且在我记忆的情况下获得了更多最新的JDBC驱动程序。

编辑:

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

对不起@geogeorge我无法帮助:(