当前位置: 首页 > 知识库问答 >
问题:

无法使用 MySQL 数据库在Spring启动 JPA 应用程序中运行实体类

上官兴昌
2023-03-14

我是春靴的新手,我正在尝试创建一个简单的示例帐户实体类。

Ma20099449银行申请。java类

@SpringBootApplication
public class Ma20099449BankApplication {

    public static void main(String[] args) {
        // SpringApplication.run(Ma20099449BankApplication.class, args);
        // System.out.println("I am working");
        ApplicationContext context = SpringApplication.run(Ma20099449BankApplication.class, args);

        System.out.println("I have started !!!!");
    }

}

Account.java级

package ma20099449.foundation.bank.ma20099449_bank.Entities;

import javax.persistence.*;

@Entity
@Table(name = "Account")
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    public int accountNumber;
    public String accountType;
    public int balance;
//I have skipped getters, constructores and to string method.
}

AccountRepository。java类

package ma20099449.foundation.bank.ma20099449_bank.Dao;
import ma20099449.foundation.bank.ma20099449_bank.Entities.Account;

import org.springframework.data.repository.CrudRepository;

public interface AccountRepository extends CrudRepository<Account,Integer> {
    
}

应用程序.属性文件


#configuring application name  
spring.application.name=ma20099449_bank  
#configuring port  
server.port=8081
#configurations of data source
        
spring.datasource.name=bank_project# it is name of datasource property, not name of database, we give it unique name
spring.datasource.url=jdbc:mysql://localhost:3306/bank
# jdbc is protocol
# mysql is subprotocol
# and test is name of database to which we want to connect
spring.datasource.username=root
spring.datasource.password=1234
#If we don't have password then don't write this property
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
# if table is not created so we will automatically create a table using hibernate in the database using below command
spring.jpa.hibernate.ddl-auto=update #it will automatically create tables for all declared entities

当我尝试运行此应用程序时,出现以下错误

PS F:\Project2020\Wipro_Project_Bank\ma20099449_bank>  f:; cd 'f:\Project2020\Wipro_Project_Bank\ma20099449_bank'; & 'c:\Users\hp\.vscode\extensions\vscjava.vscode-java-debug-0.35.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.1\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '@C:\Users\hp\AppData\Local\Temp\cp_7z9d3w5z7jwf78fszsmd8j80n.argfile' 'ma20099449.foundation.bank.ma20099449_bank.Ma20099449BankApplication' 

  .   ____          _            __ _ _   
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \  
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / / 
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.3)

2021-08-01 18:57:12.332  INFO 13324 --- [  restartedMain] m.f.b.m.Ma20099449BankApplication        : Starting Ma20099449BankApplication using Java 15.0.1 on LAPTOP-RP0N4VRN with PID 13324 (F:\Project2020\Wipro_Project_Bank\ma20099449_bank\target\classes started by hp in F:\Project2020\Wipro_Project_Bank\ma20099449_bank)
2021-08-01 18:57:12.353  INFO 13324 --- [  restartedMain] m.f.b.m.Ma20099449BankApplication        : No active profile set, falling back to default profiles: default
2021-08-01 18:57:12.504  INFO 13324 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-08-01 18:57:12.506  INFO 13324 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-08-01 18:57:13.895  INFO 13324 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-08-01 18:57:13.987  INFO 13324 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 69 ms. Found 1 JPA repository interfaces.
2021-08-01 18:57:15.224  INFO 13324 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with 
port(s): 8081 (http)
2021-08-01 18:57:15.247  INFO 13324 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-08-01 18:57:15.247  INFO 13324 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-08-01 18:57:15.424  INFO 13324 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-08-01 18:57:15.426  INFO 13324 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2903 ms
2021-08-01 18:57:15.707  INFO 13324 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-08-01 18:57:15.795  INFO 13324 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-08-01 18:57:16.022  INFO 13324 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-08-01 18:57:16.186  INFO 13324 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : bank_project# it is name of datasource property, not name of database, we give it unique name - Starting...
2021-08-01 18:57:16.685  INFO 13324 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : bank_project# it is name of datasource property, not name of database, we give it unique name - Start completed.
2021-08-01 18:57:16.714  INFO 13324 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
2021-08-01 18:57:17.194  WARN 13324 --- [  restartedMain] o.h.b.i.SessionFactoryOptionsBuilder     : Unrecognized hibernate.hbm2ddl.auto value: 'update #it will automatically create tables for all declared entities'.  Supported values include 'create', 'create-drop', 'create-only', 'drop', 'update', 'none' and 'validate'.  Ignoring
2021-08-01 18:57:17.720 ERROR 13324 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: Unrecognized legacy `hibernate.hbm2ddl.auto` value : `update #it will automatically create tables for 
all declared entities`
2021-08-01 18:57:17.722  WARN 13324 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: 
[PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: 
Unrecognized legacy `hibernate.hbm2ddl.auto` value : `update #it will automatically create tables for all declared entities` 
2021-08-01 18:57:17.727  INFO 13324 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : bank_project# it is name of datasource property, not name of database, we give it unique name - Shutdown initiated...
2021-08-01 18:57:17.751  INFO 13324 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : bank_project# it is name of datasource property, not name of database, we give it unique name - Shutdown completed.
2021-08-01 18:57:17.758  INFO 13324 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-08-01 18:57:17.778  INFO 13324 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-01 18:57:17.813 ERROR 13324 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: Unrecognized legacy `hibernate.hbm2ddl.auto` value : `update #it will automatically create tables for all declared entities`
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.9.jar:5.3.9]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.9.jar:5.3.9]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.3.jar:2.5.3]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.3.jar:2.5.3]     
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.3.jar:2.5.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.3.jar:2.5.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.3.jar:2.5.3]        
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.3.jar:2.5.3]        
        at ma20099449.foundation.bank.ma20099449_bank.Ma20099449BankApplication.main(Ma20099449BankApplication.java:13) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] 
        at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.3.jar:2.5.3]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: Unrecognized legacy `hibernate.hbm2ddl.auto` value : `update #it will automatically create tables for all declared entities`
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-5.3.9.jar:5.3.9]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.9.jar:5.3.9]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.9.jar:5.3.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.9.jar:5.3.9]
        ... 21 common frames omitted
Caused by: java.lang.IllegalArgumentException: Unrecognized legacy `hibernate.hbm2ddl.auto` value : `update #it will automatically create tables for all declared entities`
        at org.hibernate.tool.schema.Action.interpretHbm2ddlSetting(Action.java:181) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator$ActionGrouping.interpret(SchemaManagementToolCoordinator.java:490) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:53) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:318) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.9.jar:5.3.9]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.9.jar:5.3.9]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.9.jar:5.3.9]
        ... 25 common frames omitted

PS F:\Project2020\Wipro_Project_Bank\ma20099449_bank>

请帮我处理这个代码。我是springboot的新手,无法找到正确的错误。我正在使用MySQL数据库。

共有2个答案

鲍宁
2023-03-14

将注释移动到新行而不是同一行,它就起作用了。

章学义
2023-03-14

似乎你的Application.propertiesfile有一个错误的键值,更改这一行:spring.jpa.hibernate.ddl-auto=update#它将自动为所有声明的实体创建表

对于它:

spring.jpa.hibernate.ddl-auto=update

#it将自动为所有已声明的实体创建表

尽量不要在值前面做注释,我觉得可能是问题因为你的错误日志:导致原因:Java . lang . illegalargumentexception:无法识别的legacy < code > hibernate . hbm 2 DDL . auto 值:< code > update #它会自动为所有声明的实体创建表

 类似资料:
  • 无法使用maven运行spring启动应用程序。当我运行命令mvn spring boot:run时,我收到:,但在pom中。我写的xml,其中是使用该示例的主要类: 我有这个项目结构: 我希望使用spring boot和maven运行我的应用程序。但现在它不起作用了。

  • 我下面介绍了spring的RESTAPI,但我无法让它在tomcat服务器上运行(尝试了tomcat 7和9)。在eclipse中,我确实以- 编辑 尝试在项目上安装mvn,然后java-jarproject.jar 编辑2 我将项目导出到一个可运行的jar(并将所需的库打包到生成的jar中),但是 提供 编辑3 我没有删除我的m2,因为那里有很多东西,但我现在通过eclipse的spring工具

  • 尝试完成Spring Boot教程:https://Spring.io/guides/gs/spring-boot/#initial 以下是我的课程: 和pom.xml: 当我尝试使用以下命令运行它时:“mvn package&&java-jar target/gs-spring-boot-0.1.0.jar”,我得到的是: “-dmaven.home=C:\program files\jetbr

  • application.yml: 我尝试从运行mysql的命名空间上的另一个pod访问该服务,因为它已经预先安装了mysql-client,并从主机访问该服务。两人都有访问数据库的权限。我还在运行应用程序的pod上tring ping。它发现服务有任何问题。 然后我尝试使用NodePort而不是ClusterIP。什么都没变。 我完全卡住了,不知道出了什么问题。如有任何帮助,不胜感激。

  • 我对Spring靴不熟悉。任何时候我运行我的Spring启动应用程序,我得到错误。需要帮助运行我的Spring启动应用程序。 错误信息:白标签错误页 此应用程序没有/error的显式映射,因此您将其视为回退。 2016年10月10日星期一10:39:54 WAT出现意外错误(类型=未找到,状态=404)。没有可用的消息 代码: