这几天搞hibernate ,遇到了很奇怪的问题,怎么弄不行。
从网上找了很多答案,都不能解决问题。最后发现是配置文件hibernate.cfg.xml 在配置属性的时候“”双引号多填了一个空格,所以这个好坑爹,记录一下。
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.deve.beans.employee.main.ManageEmployee.main(ManageEmployee.java:44)
Caused by: java.lang.UnsupportedOperationException: The application must supply JDBC connections
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:44)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)
at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43)
at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcConnection(ImprovedExtractionContextImpl.java:60)
at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcDatabaseMetaData(ImprovedExtractionContextImpl.java:67)
at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.getTable(InformationExtractorJdbcDatabaseMetaDataImpl.java:271)
at org.hibernate.tool.schema.internal.exec.ImprovedDatabaseInformationImpl.getTableInformation(ImprovedDatabaseInformationImpl.java:110)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.performMigration(SchemaMigratorImpl.java:247)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:132)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:105)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:176)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:65)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:459)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
at org.hibernate.boot.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:170)
at org.deve.beans.employee.main.ManageEmployee.main(ManageEmployee.java:36)
现在贴上我的错误配置文件:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url ">jdbc:mysql://10.81.231.85:3306/example</property>
<property name="connection.username">mq</property>
<property name="connection.password">password</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="org/deve/beans/employee/employee.hbm.xml"/>
</session-factory>
大家可以看到我的“connection.url ”是有空格的。
记录这个问题给自己记忆。
hibernate version: 5.1.3
database :mysql
还有是一个问题,是出现在这个问题的时候,我在网上找答案有人说是少了jar包,
所以我就把hibernate所有的jar都导进去了。
于是出现了下面这个异常:
Caused by: java.util.ServiceConfigurationError: org.hibernate.boot.registry.selector.StrategyRegistrationProvider: Provider
org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl not found
解决这个问题的方法是,我重新把所有的包都移除,然后只添加hibernate 下载文件里面 required 文件夹下的包和数据库的包。在晚上找到别人的配置文件。突然发现OK了。
两个问题都没有了。神奇吧!
在我查问题的过程中和我测试的代码中,试了有三种加载配置文件的方法都可以运行。但是网上有人说hibernate5只支持
- factory = new Configuration().configure().buildSessionFactory(); (说是hibernate4的方法)
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
factory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
(说这个才是hibernate5的)- Configuration configuration = new Configuration();
// 默认使用src文件夹下的hibernate.cfg.xml进行配置,若更改了路径,要附加上包路径如:"/com/example/hibernate.cfg.xml"
factory = configuration.configure().buildSessionFactory();
Session session = factory.openSession();
上面三个方法我有试了,没有问题!OK