使用hibernate.xml连接到mysql数据库的问题

时间:2017-12-11 18:29:13

标签: java mysql hibernate

我无法使用xml连接到mysql数据库。我使用hibernate.properties文件没有问题,但是当我用xml替换它时它不起作用。 main/resources中的xml是

<?xml version='1.0' encoding='utf-8'?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/ifinances</property>
    <property name="connection.username">infinite</property>
    <property name="connection.password">skills</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

    <mapping class="com.infiniteskills.data.entities.User" />
</session-factory>
</hibernate-configuration>

我有一个返回此

的实用程序SessionFactory类
private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        return configuration
                .buildSessionFactory(new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Error in building the factory");
    }
}

我检索它后,我调用方法.openSession()我有以下错误

WARN - HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
WARN - HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at com.infiniteskills.data.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at com.infiniteskills.data.HibernateUtil.<clinit>(HibernateUtil.java:11)
at com.infiniteskills.data.Application.main(Application.java:18)
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
... 15 more
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.infiniteskills.data.Application.main(Application.java:18)
Caused by: java.lang.RuntimeException: Error in building the factory
at com.infiniteskills.data.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
at com.infiniteskills.data.HibernateUtil.<clinit>(HibernateUtil.java:11)
... 1 more

3 个答案:

答案 0 :(得分:0)

在库路径中添加用于MySQL的MySQL连接器JDBC驱动程序(JAR)

链接[https://dev.mysql.com/downloads/connector/j]

答案 1 :(得分:0)

您不是使用hibernate.cfg.xml来配置Configuration对象 所以它没有拿起那些属性。

Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");  //Resource path or File

答案 2 :(得分:0)

您的hibernate配置文件使用了错误的参数名称。正确的参数名称如下:

hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.dialect
hibernate.show_sql

更正您的参数名称,然后重试。

有关参数名称的详细列表,请参阅此链接:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html