Hibernate:与DB2数据库的连接失败(方言?)

时间:2014-08-14 16:02:26

标签: java hibernate db2 dialect

我迫切希望通过hibernate连接到DB2数据库。问题在这里,我知道,错误来自哪里,但我无法解决它,或者说不理解它。也许你可以帮我解决这个问题。我将非常感激!

以下代码显示了我的hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
        <property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
        <property name="hibernate.connection.url">jdbc:db2://db2.example.com:50001/databasename</property>
        <property name="hibernate.connection.username">mydbuser</property>
        <property name="hibernate.connection.password">mydbpass</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="media.definitions.Product" />

    </session-factory>
</hibernate-configuration>

以下代码是我通过教程构建的类:

package media.frontend;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");         
        StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
        sb.applySettings(cfg.getProperties());
        StandardServiceRegistry standardServiceRegistry = sb.build();        
        sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

当我启动程序时,会立即出现以下错误(仅尝试捕获单个列的信息)。我已粘贴整个输出:

Aug 14, 2014 5:45:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Aug 14, 2014 5:45:45 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.ibm.db2.jcc.DB2Driver] at URL [jdbc:db2://anger.informatik.uni-leipzig.de:50001/PRAK14B]
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=dbprak12, password=****}
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000341: Could not obtain connection metadata : DatabaseMetaData information is not known for server DB2/SUNX8664SQL10010 by this version of JDBC driver
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Exception in thread "main" java.lang.ExceptionInInitializerError
at media.frontend.HibernateTest.main(HibernateTest.java:15)
Caused by: java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at media.frontend.HibernateUtil.<clinit>(HibernateUtil.java:17)
... 1 more

提前感谢您的帮助!

电贺, 路加

1 个答案:

答案 0 :(得分:0)

我的代码中遇到了类似的问题。然后我尝试将最新版本的DB2 jar更新到我的服务器中,代码开始工作。尝试更新DB2 jar。