问题描述
我正在尝试在spring-boot项目中配置休眠状态.
I am trying to configure hibernate in my spring-boot project.
我有application.properties文件
I have application.properties file
spring.datasource.url=jdbc:mysql:url?useSSL=false
spring.datasource.username=name
spring.datasource.password=password
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql:url
hibernate.connection.username = name
hibernate.connection.password = password
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect=org.hibernate.dialect.MySQLDialect
以及在UserDAO类中注册用户的方法:
And method for registering user in UserDAO class:
public void registerUser(User u){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(u);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
System.out.println("yay all done");
}
但是我收到错误:
> Caused by: 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:275)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:179)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:119)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> com.groups.data.UserDAO.<init>(UserDAO.java:18) ~[classes/:na] ... 52
> common frames omitted 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)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final] ... 66 common frames
> omitted
我已按要求配置了方言,我什至尝试创建良好的旧hibernate.cfg.xml
文件,但错误仍然相同(甚至似乎忽略了该文件).
I have configured dialect as it requires, i have even tried creating good old hibernate.cfg.xml
file but the error remained the same ( it even seemed it ignored the file).
为什么会发生ist?从字面上看,昨天它没有问题.今天,我尝试创建cfg文件,但该文件停止工作(已删除).
Why is ist happening? Literally yesterday it worked without problem. Today i tried creating the cfg file and it stopped working ( deleted it already ).
我尝试配置越多的休眠方式,似乎就越激怒了正在使用它的任何人,并且无法使其正常工作.
The more i try to configure hibernate the more it seems it was made to anger anyone who is using it and impossible to make it work.
这可能是什么问题?
感谢帮助!
推荐答案
您是否已将此行添加到application.properties
文件中?
Have you added this line to your application.properties
file ?
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
DB2 AS/400
org.hibernate.dialect.DB2400Dialect
DB2 AS/400
org.hibernate.dialect.DB2400Dialect
DB2 OS390
org.hibernate.dialect.DB2390Dialect
DB2 OS390
org.hibernate.dialect.DB2390Dialect
PostgreSQL
org.hibernate.dialect.PostgreSQLDialect
PostgreSQL
org.hibernate.dialect.PostgreSQLDialect
MySQL
org.hibernate.dialect.MySQLDialect
MySQL
org.hibernate.dialect.MySQLDialect
带有InnoDB的MySQL
org.hibernate.dialect.MySQLInnoDBDialect
MySQL with InnoDB
org.hibernate.dialect.MySQLInnoDBDialect
带有MyISAM的MySQL
org.hibernate.dialect.MySQLMyISAMDialect
MySQL with MyISAM
org.hibernate.dialect.MySQLMyISAMDialect
Oracle(任何版本)
org.hibernate.dialect.OracleDialect
Oracle (any version)
org.hibernate.dialect.OracleDialect
Oracle 9i/10g
org.hibernate.dialect.Oracle9Dialect
Oracle 9i/10g
org.hibernate.dialect.Oracle9Dialect
Sybase
org.hibernate.dialect.SybaseDialect
Sybase
org.hibernate.dialect.SybaseDialect
Sybase Anywhere
org.hibernate.dialect.SybaseAnywhereDialect
Sybase Anywhere
org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server
org.hibernate.dialect.SQLServerDialect
Microsoft SQL Server
org.hibernate.dialect.SQLServerDialect
SAP数据库
org.hibernate.dialect.SAPDBDialect
SAP DB
org.hibernate.dialect.SAPDBDialect
Informix
org.hibernate.dialect.InformixDialect
Informix
org.hibernate.dialect.InformixDialect
HypersonicSQL
org.hibernate.dialect.HSQLDialect
HypersonicSQL
org.hibernate.dialect.HSQLDialect
Ingres
org.hibernate.dialect.IngresDialect
Ingres
org.hibernate.dialect.IngresDialect
进度
org.hibernate.dialect.ProgressDialect
Progress
org.hibernate.dialect.ProgressDialect
Mckoi SQL
org.hibernate.dialect.MckoiDialect
Mckoi SQL
org.hibernate.dialect.MckoiDialect
Interbase
org.hibernate.dialect.InterbaseDialect
Interbase
org.hibernate.dialect.InterbaseDialect
Pointbase
org.hibernate.dialect.PointbaseDialect
Pointbase
org.hibernate.dialect.PointbaseDialect
FrontBase
org.hibernate.dialect.FrontbaseDialect
FrontBase
org.hibernate.dialect.FrontbaseDialect
火鸟
org.hibernate.dialect.FirebirdDialect
Firebird
org.hibernate.dialect.FirebirdDialect
这篇关于如果未使用spring-boot设置"hibernate.dialect",则对DialectResolutionInfo的访问不能为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!