【问题标题】:Is there a way to create a database with the entity first aproach in spring boot?有没有办法在 Spring Boot 中使用实体优先方法创建数据库?
【发布时间】:2022-01-05 18:15:14
【问题描述】:

我想在 Spring Boot 中从对象创建 Microsoft SQL DB 的表。有没有办法做到这一点?如果是的话,有人能告诉我至少应该怎么做吗?

【问题讨论】:

标签: java sql spring-boot hibernate jpa


【解决方案1】:

是的,你可以。

首先,您需要从您的对象创建一个类,并将@Entity 注解传递给它,如下所示:

@Entity
public class DemoEntity {
    @Id
    private Long id;
    private String demoStringProperty;
    private Double demoDoubleProperty;
}

然后让 hibernate 知道它应该通过将其添加到 application.properties 文件中来从类中创建实体:

... database url, username, pass ...
spring.jpa.hibernate.ddl-auto=update

create-drop insead 或 update 在您的情况下会完成同样的事情,但您可以在community documentation 中阅读更多内容。

【讨论】:

  • 是的,我已经尝试过了,但是我遇到了以下问题。我创建了我的班级。在 application.properties 我有这些行: spring.datasource.url=jdbc:sqlserver://localhost:1433;database:BikeShopDB;integratedSecurity=true; spring.jpa.hibernate.ddl-auto=更新。问题是我收到一条错误消息,提示无法创建 entityManagerFactory。
  • 你有 spring-boot-starter-data-jpa 作为依赖吗?
  • 是的,我有它
  • 我已经在application.properties 中添加了这些:spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.jpa.show-sql=true spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2016Dialect spring.jpa.hibernate.ddl-auto = create-drop 现在我得到了这个Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  • 检查this question's answers是否可以帮助您。当数据库服务器关闭、使用错误的凭据、连接到错误的数据库服务器等时,可能会发生以下错误。
猜你喜欢
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2018-01-21
  • 2023-03-31
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多