在Spring Boot中将记录保存到DB中时出错。请找到以下代码和错误详细信息:
主类:
package *.*.*;
@SpringBootApplication
public class BankApplication {
public static void main(String[] args) {
SpringApplication.run(BankApplication.class, args);
}
}
存储库
package *.*.*.dataaccess.repository;
@Repository
public interface BankRepository extends JpaRepository<BankEntity, String> {
}
package *.*.*.dataaccess.dao;
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Bank")
public class BankEntity {
//fields
}
package *.*.*.dataaccess.utils;
@Component
public class DBUtils {
@Autowired
BankRepository bnRepo;
*/
public void saveBank(BankDto dto) {
//preparing bnEntity -entity
this.bnRepo.saveAndFlush(bnEntity);
}
}
spring.jpa.database=mysql
spring.datasource.url=jdbc:mysql://${DATABASE_SRV}/?&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.datasource.username=${USER}
spring.datasource.password=${PASS}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# HIBERNATE
spring.jpa.properties.hibernate.proc.param_null_passing=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Unsatisfied dependency expressed through method 'dataSource' parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties': Could not bind properties to 'DataSourceProperties' : prefix=spring.datasource, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2a18f23c has not been refreshed yet
在从另一个类的DBUtils中调用saveAndFlush()时得到上述错误。谁能帮我这个忙。
答案 0 :(得分:0)
尝试将方言添加到您的application.properties文件中,如下所示
##Hibernate DDL Auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect