即使使用注释或XML进行映射,我也面临同样的错误,调试时我注意到了这一行
factory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
永远不会执行,我的打印显示在休眠日志中:
这是休眠日志:
2017年12月2日下午1:14:55 org.hibernate.Version logVersion INFO: HHH000412:Hibernate Core {5.2.12.Final} 2017年12月2日下午1:14:55 org.hibernate.cfg.Environment信息:HHH000205:已加载 来自资源hibernate.properties的属性: {hibernate.connection.driver_class = org.h2.Driver, hibernate.service.allow_crawling = false,hibernate.max_fetch_depth = 5, hibernate.dialect = org.hibernate.dialect.H2Dialect, hibernate.format_sql = true,hibernate.generate_statistics = true, hibernate.connection.username = SA, hibernate.connection.url = JDBC:H2:MEM:DB1; DB_CLOSE_DELAY = -1; LOCK_TIMEOUT = 10000, hibernate.bytecode.use_reflection_optimizer =假, hibernate.connection.password = ****,hibernate.connection.pool_size = 5} 2017年12月2日下午1:14:55 org.hibernate.spatial.integration.SpatialService INFO: HHH80000001:启用了hibernate-spatial集成:真正的Criou arquivodeconfiguraçãoXML! 2017年12月2日下午1:14:55 org.hibernate.annotations.common.reflection.java.JavaReflectionManager 信息:HCANN000001:Hibernate Commons注释 {5.0.1.Final} 2017年12月2日下午1:14:55 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 配置WARN:HHH10001002:使用Hibernate内置连接池 (不用于生产用途!)2017年12月2日下午1:14:55 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO:HHH10001005:使用驱动程序[com.mysql.jdbc.Driver] 在URL [jdbc:mysql:// localhost:3306 / aps] 2017年12月2日下午1:14:55 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO:HHH10001001:连接属性:{user = victor, 密码= ****} 2017年12月2日下午1:14:55 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator信息:HHH10001003:自动提交模式:false 2017年12月2日 下午1:14:56 org.hibernate.engine.jdbc.connections.internal.PooledConnections 信息:HHH000115:休眠连接池大小:5(min = 1)Dec 02,2017 1:14:56 PM org.hibernate.dialect.Dialect INFO: HHH000400:使用方言:org.hibernate.dialect.MySQL5Dialect Falha aocriarsessão 2017年12月2日下午1:14:57 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 停止信息:HHH10001008:清理连接池 [JDBC:MySQL的://本地主机:3306 / APS]
hibernate.cfg.xml中:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/aps</property>
<property name="connection.username">victor</property>
<property name="connection.password">123456</property>
<property name ="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<mapping class="br.unip.beans.Atividade"/>
<mapping class="br.unip.beans.Projeto"/>
<mapping class="br.unip.beans.Usuario"/>
<mapping class="br.unip.beans.Projeto_has_usuario"/>
</session-factory>
发生的功能
private static SessionFactory factory;
protected void setUp() throws Exception {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
factory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
System.out.println("Falha ao criar sessão");
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
}