微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Springboot Hibernate不会在远程mysql数据库中创建表

如何解决Springboot Hibernate不会在远程mysql数据库中创建表

我正在尝试在远程数据库上创建表,但是它没有对它进行任何查询。 实际上,当我输入错误的db密码时,它给了我一个错误,因此我认为它以某种方式正在连接但未序列化任何实体。

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>


<groupId>com.palmieri</groupId>
<artifactId>InstaPalm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>InstaPalm</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>14</java.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
        <version>2.3.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/MysqL/mysql-connector-java -->
    <dependency>
        <groupId>MysqL</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <version>2.3.4.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这是我的实体类用户

package com.palmieri.InstaPalm.entities;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Date;
import java.util.List;
@Entity
@Table(name = "photo")
public class Photo implements Serializable {

private static final long serialVersionUID = 6470094278127188729L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "data",nullable = false)
private Date data;
@ManyToOne( fetch = FetchType.EAGER)
private User user;
@Column(name="link",nullable = false)
private String link;
@OnetoMany(mappedBy = "photo",fetch = FetchType.EAGER)
private List<Like> likes;
@OnetoMany(mappedBy = "photo",fetch = FetchType.EAGER)
private List<Comment> comments;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Date getData() {
    return data;
}

public void setData(Date data) {
    this.data = data;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public List<Like> getLikes() {
    return likes;
}

public void setLikes(List<Like> likes) {
    this.likes = likes;
}

public List<Comment> getComments() {
    return comments;
}

public void setComments(List<Comment> comments) {
    this.comments = comments;
}
}

还有我的application.properties

## Database Properties
spring.datasource.driver-class-name=com.MysqL.cj.jdbc.Driver
spring.datasource.url = jdbc:MysqL://sql2.freeMysqLhosting.net:3306/XXX?useSSL=false
spring.datasource.username = XXXXX
spring.datasource.password = XXXXX
## Hibernate Properties
# The sql dialect makes Hibernate generate better sql for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MysqL5InnoDBDialect
spring.jpa.open-in-view=false
spring.jpa.show-sql=true
# Hibernate ddl auto (create,create-drop,validate,update)
spring.jpa.hibernate.ddl-auto = none

我确定我缺少某些配置或某些属性,但是我无法弄清楚哪个,或者我可能不匹配这些软件包?谢谢您的回复

编辑1 这是我的日志

2020-09-23 14:54:49.807  INFO 14620 --- [  restartedMain] c.p.InstaPalm.InstaPalmApplication       : Starting InstaPalmApplication on SI-W-00446 with PID 14620 (C:\Users\SI2001\IdeaProjects\InstaPalm\InstaPalm\target\classes started by SI2001 in C:\Users\SI2001\IdeaProjects\InstaPalm\InstaPalm)
2020-09-23 14:54:49.809  INFO 14620 --- [  restartedMain] c.p.InstaPalm.InstaPalmApplication       : No active profile set,falling back to default profiles: default
2020-09-23 14:54:49.809 DEBUG 14620 --- [  restartedMain] o.s.boot.SpringApplication               : Loading source class com.palmieri.InstaPalm.InstaPalmApplication
2020-09-23 14:54:49.919 DEBUG 14620 --- [  restartedMain] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'file:/C:/Users/SI2001/IdeaProjects/InstaPalm/InstaPalm/target/classes/application.properties' (classpath:/application.properties)
2020-09-23 14:54:49.921 DEBUG 14620 --- [  restartedMain] o.s.b.devtools.restart.Changeableurls    : Matching URLs for reloading : [file:/C:/Users/SI2001/IdeaProjects/InstaPalm/InstaPalm/target/classes/]
2020-09-23 14:54:49.921 DEBUG 14620 --- [  restartedMain] o.s.b.d.settings.DevToolsSettings        : Included patterns for restart : []
2020-09-23 14:54:49.921 DEBUG 14620 --- [  restartedMain] o.s.b.d.settings.DevToolsSettings        : Excluded patterns for restart : [/spring-boot-starter-[\w-]+/,/spring-boot/(bin|build|out)/,/spring-boot-starter/(bin|build|out)/,/spring-boot-devtools/(bin|build|out)/,/spring-boot-actuator/(bin|build|out)/,/spring-boot-autoconfigure/(bin|build|out)/]
2020-09-23 14:54:49.922  INFO 14620 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-09-23 14:54:49.922  INFO 14620 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-09-23 14:54:49.922 DEBUG 14620 --- [  restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4d5a23dd
2020-09-23 14:54:51.024  INFO 14620 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-09-23 14:54:51.046  INFO 14620 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 14ms. Found 0 JPA repository interfaces.
2020-09-23 14:54:51.574 DEBUG 14620 --- [  restartedMain] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: C:\Users\SI2001\.m2\repository\org\springframework\boot\spring-boot\2.3.4.RELEASE\spring-boot-2.3.4.RELEASE.jar
2020-09-23 14:54:51.575 DEBUG 14620 --- [  restartedMain] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: C:\Users\SI2001\.m2\repository\org\springframework\boot\spring-boot\2.3.4.RELEASE\spring-boot-2.3.4.RELEASE.jar
2020-09-23 14:54:51.575 DEBUG 14620 --- [  restartedMain] .s.b.w.e.t.TomcatServletWebServerFactory : None of the document roots [src/main/webapp,public,static] point to a directory and will be ignored.
2020-09-23 14:54:51.641  INFO 14620 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-09-23 14:54:51.651  INFO 14620 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-09-23 14:54:51.651  INFO 14620 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-09-23 14:54:51.797  INFO 14620 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-09-23 14:54:51.798 DEBUG 14620 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2020-09-23 14:54:51.798  INFO 14620 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1876 ms
2020-09-23 14:54:51.927 DEBUG 14620 --- [  restartedMain] o.s.b.w.s.ServletContextinitializerBeans : Mapping filters: filterRegistrationBean urls=[/*] order=-2147483647,characterEncodingFilter urls=[/*] order=-2147483648,formContentFilter urls=[/*] order=-9900,requestContextFilter urls=[/*] order=-105
2020-09-23 14:54:51.927 DEBUG 14620 --- [  restartedMain] o.s.b.w.s.ServletContextinitializerBeans : Mapping servlets: dispatcherServlet urls=[/]
2020-09-23 14:54:51.945 DEBUG 14620 --- [  restartedMain] o.s.b.a.m.w.servlet.WebMvcmetricsFilter  : Filter 'webMvcmetricsFilter' configured for use
2020-09-23 14:54:51.945 DEBUG 14620 --- [  restartedMain] o.s.b.w.s.f.OrderedRequestContextFilter  : Filter 'requestContextFilter' configured for use
2020-09-23 14:54:51.945 DEBUG 14620 --- [  restartedMain] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2020-09-23 14:54:51.945 DEBUG 14620 --- [  restartedMain] o.s.b.w.s.f.OrderedFormContentFilter     : Filter 'formContentFilter' configured for use
DEBUG StatusLogger org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!
2020-09-23 14:54:52.109  INFO 14620 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-23 14:54:52.147  INFO 14620 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-09-23 14:54:52.200  INFO 14620 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-09-23 14:54:52.355  INFO 14620 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-09-23 14:54:52.361 DEBUG 14620 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @modelattribute,0 @InitBinder,1 RequestBodyAdvice,1 ResponseBodyAdvice
2020-09-23 14:54:52.415 DEBUG 14620 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 mappings in 'requestMappingHandlerMapping'
2020-09-23 14:54:52.446 DEBUG 14620 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/webjars/**,/**] in 'resourceHandlerMapping'
2020-09-23 14:54:52.459 DEBUG 14620 --- [  restartedMain] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler,1 ResponseBodyAdvice
2020-09-23 14:54:52.463  INFO 14620 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-09-23 14:54:53.132  INFO 14620 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-09-23 14:54:53.153  INFO 14620 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MysqL8Dialect
2020-09-23 14:54:53.364 DEBUG 14620 --- [  restartedMain] inMXBeanRegistrar$SpringApplicationAdmin : Application Admin MBean registered with name 'org.springframework.boot:type=Admin,name=SpringApplication'
2020-09-23 14:54:53.411 DEBUG 14620 --- [  restartedMain] o.s.b.d.livereload.LiveReloadServer      : Starting live reload server on port 35729
2020-09-23 14:54:53.413  INFO 14620 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-09-23 14:54:53.419  INFO 14620 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-09-23 14:54:53.479  INFO 14620 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-23 14:54:53.481  INFO 14620 --- [  restartedMain] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-09-23 14:54:53.482  INFO 14620 --- [  restartedMain] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-09-23 14:54:53.500  INFO 14620 --- [  restartedMain] c.p.InstaPalm.InstaPalmApplication       : Started InstaPalmApplication in 4.246 seconds (JVM running for 5.36)
2020-09-23 14:54:53.506 DEBUG 14620 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Creating new Restarter for thread Thread[main,5,main]
2020-09-23 14:54:53.506 DEBUG 14620 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Immediately restarting application
2020-09-23 14:54:53.506 DEBUG 14620 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1c3d316b
2020-09-23 14:54:53.506 DEBUG 14620 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Starting application com.palmieri.InstaPalm.InstaPalmApplication with URLs [file:/C:/Users/SI2001/IdeaProjects/InstaPalm/InstaPalm/target/classes/]
2020-09-23 14:54:54.064  INFO 14620 --- [ion(3)-10.8.2.7] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring dispatcherServlet 'dispatcherServlet'
2020-09-23 14:54:54.064  INFO 14620 --- [ion(3)-10.8.2.7] o.s.web.servlet.dispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-09-23 14:54:54.064 DEBUG 14620 --- [ion(3)-10.8.2.7] o.s.web.servlet.dispatcherServlet        : Detected StandardServletMultipartResolver
2020-09-23 14:54:54.071 DEBUG 14620 --- [ion(3)-10.8.2.7] o.s.web.servlet.dispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2020-09-23 14:54:54.072  INFO 14620 --- [ion(3)-10.8.2.7] o.s.web.servlet.dispatcherServlet        : Completed initialization in 8 ms

解决方法

createDatabaseIfNotExist=true添加到您的db.url

   spring.datasource.url = jdbc:mysql://sql2.freemysqlhosting.net:3306/XXX useSSL=false&createDatabaseIfNotExist=true
,

我认为您需要在application.properties/application.yml文件中使用以下配置:

<div id='boxA' class='box' :class="{boxRed : Aselected}" @click='change("A")'></div>

<div id='boxB' class='box' :class="{boxRed : Bselected}" @click='change("B")'></div>

<div id='boxC' class='box' :class="{boxRed : Cselected}" @click='change("C")'></div>
,

如果您希望Hibernate创建表,则应将application.property文件中的spring.jpa.hibernate.ddl-auto属性设置为无,以创建或更新。

__in
,

您可以通过添加此内容来实现自己的目的

spring.jpa.hibernate.ddl-auto = create

但是请记住,每次启动应用程序时,它将删除所有以前的表和数据。因此,为避免这种情况,只需将其更改为update

spring.jpa.hibernate.ddl-auto = update
,
  1. 如果MySQL服务器中没有可用的数据库,最好先使用SQL命令创建一个空数据库。

  2. 然后运行以下应用程序,并将以下属性添加到application.properties文件中。

    spring.jpa.hibernate.ddl-auto = update

因为update参数不会删除您的数据库并在每次重新启动时重新创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?