首页 > 解决方案 > 在休眠配置中没有创建表

问题描述

我编写了一个休眠实用程序类来加载配置文件并打开会话,我同时使用 xml 和属性文件,似乎属性文件已加载但一切顺利,但未创建表。

这是 HibernateUtilNew.java

package com.javarnd.cip.db;

import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.javarnd.cip.util.PropertiesUtil;

public class HibernateUtilNew {

    private static final SessionFactory sessionFactory;
    private static Properties properties = new Properties();
    private static Configuration configuration=new Configuration();


    static {
        try {
            properties = PropertiesUtil.propertyLoad();
            configuration.configure("hibernate.cfg.xml").addProperties(properties);
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                        .applySettings(configuration.getProperties())
                        .build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session openSession() {
        return sessionFactory.openSession();
    }

    public static void shutdown() {
        sessionFactory.close();
    }

    public static void main(String[] argss) {
        openSession();
        System.out.println("connection open");
//      shutdown();
//      System.out.println("connection closed successfully");
    }
}

hibernate_config.properties

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/testcip
hibernate.connection.username=root
hibernate.connection.password=root
#hibernate.connection.pool_size=10
hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
show_sql=true
format_sql=true
use_sql_comments=true
hbm2ddl.auto=create

休眠.cfg.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <property name="hibernate.dialect"></property>
        <property name="hibernate.connection.driver_class"></property>
        <property name="hibernate.connection.url"></property>
        <property name="hibernate.connection.username"></property>
        <property name="hibernate.connection.password"></property>
        <property name="show_sql"></property>
        <property name="format_sql"></property>
        <property name="use_sql_comments"></property>
        <property name="hbm2ddl.auto"></property>

        <!-- mapping files -->
        <mapping class="com.javarnd.cip.model.Country" />
        <mapping class="com.javarnd.cip.model.City" />
        <mapping class="com.javarnd.cip.model.Language" />
        <mapping class="com.javarnd.cip.model.Sports" />
        <mapping class="com.javarnd.cip.model.Userdetail" />
    </session-factory>
</hibernate-configuration>

我还尝试过的另一件事mergeProperties()引发了我的异常

Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

使用 addProperties() 进行堆栈跟踪

Mar 05, 2019 11:44:53 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.1.Final}
Mar 05, 2019 11:44:54 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
Mar 05, 2019 11:44:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Mar 05, 2019 11:44:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/testcip]
Mar 05, 2019 11:44:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 05, 2019 11:44:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 05, 2019 11:44:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Mar 05, 2019 11:44:54 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
Mar 05, 2019 11:44:55 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
connection open

属性实用程序

package com.javarnd.cip.util;

import java.util.Properties;

public class PropertiesUtil {
    public static Properties propertyLoad() {
        Properties properties = null;
        if (properties == null) {
            properties = new Properties();
            try {
                properties.load(PropertiesUtil.class
                        .getResourceAsStream("/hibernate_config.properties"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return properties;
    }
}

目录结构和文件位置 在此处输入图像描述

请建议我解决方案

标签: javahibernate

解决方案


推荐阅读