java - 在 Microsoft Azure 的 MSSQL 服务器中创建表时出错 : Spring Boot and Hibernate

标签 java spring-boot azure hibernate mssql-jdbc

我已经使用 Hibernate 创建了一个 Spring Boot Web 应用程序。我正在使用 Microsoft Azure 和 SQL 数据库来部署我的应用程序。我已经在 Azure 上和我的代码中配置了数据库。配置代码是-
1. application.properties 文件

#to  automatically create/update tables for any entities
spring.jpa.hibernate.ddl-auto=update
#to show the table operation query in the console
spring.jpa.show-sql=true

#change port
server.port=8085

#File related all configurations
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

project.image=images/

#get all the debugging logs for spring security
logging.level.org.springframework.security=DEBUG

<强>2。 application.yml 文件

spring:
  datasource:
    url: jdbc:sqlserver:<url>:1433;database=blogging-application-db;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
    username: <username>
    password: <password>
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      dialect : org.hibernate.dialect.SQLServer2012Dialect
    #   hibernate:
    #     '[globally_quoted_identifiers: true]'
    #   properties:
    #     dialect : org.hibernate.dialect.SQLServer2012Dialect

server:
  port: 8085

我正在数据库中创建多个表。
Image of tables in Microsoft Azure console

所有表都已创建,但需要为用户和角色之间的多对多关系自动创建的表尚未创建。该表被命名为user_role。我收到以下错误 -

Hibernate: create table user_role (user int not null, role int not null, primary key (user, role))  
2022-10-29 22:22:39.192  WARN 18348 --- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     : 
GenerationTarget encountered exception accepting command : 
Error executing DDL "create table user_role (user int not null, role int not null, primary key (user, role))" via JDBC Statement

Hibernate: alter table user_role add constraint FKlduspqw8rg0gbcpludbfadw6l foreign key (user) references users  
2022-10-29 22:22:39.852  WARN 18348 --- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     : 
GenerationTarget encountered exception accepting the command : 
Error executing DDL "alter table user_role add constraint FKlduspqw8rg0gbcpludbfadw6l foreign key (user) references users" via JDBC Statement

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'user'.

我无法弄清楚创建应该由 hibernate 自动创建的user_role表有什么问题。此外,当我使用 MySQL 与 localhost 时,代码运行良好。现在,当我尝试使用 Microsoft Azure 和 MSSQL 数据库时,出现错误。 请帮我解决这个问题!

最佳答案

  • 您也可以使用实体类创建这些表,其中您可以使用多对多注释创建连接表。

  • 多对多注释将有助于通过连接创建新表,您可以在其中选择哪些列将成为新表的一部分。

package com.example.demo;  

 import javax.persistence.*;  
 import java.util.HashSet;  
 import java.util.Set;  
  
@Entity  
@Table(name="t1")  
public class t1 {  
    @Id  
 @GeneratedValue(strategy = GenerationType.AUTO)  
    @Column(name = "id", nullable = false)  
    private Long id;  
  
 public Long getId() {  
        return id;  
  }  
  
    public void setId(Long id) {  
        this.id = id;  
  }  
  
    @Column(name = "name")  
    private String name ;  
  
  @ManyToMany(cascade = { CascadeType.ALL })  
    @JoinTable(  
            name = "t3",  
  joinColumns = { @JoinColumn(name = "id") },  
  inverseJoinColumns = { @JoinColumn(name = "newid") }  
    )  
    Set<t2> t1 = new HashSet<>();   
}
  • 这里多对多将连接两个表并创建一个名为 t3 的新表。这里我们还给出了所设置的表 t2 的引用。
package com.example.demo;  
  
import javax.persistence.*;  
import java.util.HashSet;  
import java.util.Set;  
  
@Entity  
@Table(name="t2")  
public class t2 {  
    @Id  
 @GeneratedValue(strategy = GenerationType.AUTO)  
    @Column(name = "newid", nullable = false)  
    private Long newid;  
  
 public Long getNewid() {  
        return newid;  
  }  
  
    public void setNewid(Long newid) {  
        this.newid = newid;  
  }  
  
    @Column(name = "newname")  
    private String newname ;  
  
  @ManyToMany(mappedBy = "t1")  
    Set<t1> t2 = new HashSet<>();  
}
  • 与 t1 类似,这里我们也将 t2 映射到 t1,并将 t1 的引用作为集合给出。

依赖项(pom.xml):

<dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-data-jpa</artifactId>  
</dependency>  
<dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-web</artifactId>  
</dependency>  
  
<dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-test</artifactId>  
 <scope>test</scope>  
</dependency>

现在运行您的项目,它将创建表。

enter image description here

引用此article Zeger Hendrikse 谈这个

关于java - 在 Microsoft Azure 的 MSSQL 服务器中创建表时出错 : Spring Boot and Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74247318/

相关文章:

azure - Terraform 没有按正确的顺序销毁 Frontdoor 资源。我怎样才能解决这个问题?

java - 如何将复杂的 JSON 解析为 ArrayAdapters

java - log4j2如何记录包?

java - 如何检查HttpClient中是否使用了代理以及如何关闭代理?

azure - Web API 身份验证最佳实践

Azure计时器触发器网络作业: Function is being recognised but not being triggered

java - AppEngine 连接的 Android 项目中的注释失败

Docker compose spring boot redis 连接问题

spring-boot - @Value 注释在 AbstractAuthenticationProcessingFilter 过滤器中返回空值

java - JPA批量插入并不能提高性能