我写了一个休眠的util类来加载配置文件并在其中打开会话,我同时使用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
hibernate.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
PropertiesUtil
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;
}
}
请给我建议解决方法