【问题标题】:Spring boot H2 database applicationSpring boot H2 数据库应用
【发布时间】:2017-11-22 12:27:44
【问题描述】:

我使用以下代码创建了一个示例项目。即使我没有在 data.sql 中提供表创建语句,它也在创建表。如何阻止它。示例代码如下所示

你能告诉我我做错了什么吗?我已经删除了下面的导入语句,因为帖子不允许在这里放这么多代码。

package com.example.demo;
// Model class 
@Entity
@Table(name="reservation")
public class Reservation {
 @Id
 private Long id;
 @Column(name="user_id")
 private Long userId;    
 @Column(name="party_size")
 private int partySize;
 @Column(name="restaurant_id")
 private Long restaurantId;
 @Column(name="date")
 private LocalDateTime dt;
 public Reservation() {}
 public Reservation(Long id,  Long userId, int partySize) {
  this.id = id;
  this.userId = userId;
  this.partySize = partySize;
 }
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public Long getUserId() {
  return userId;
 }
 public void setUserId(Long userId) {
  this.userId = userId;
 }
 public int getPartySize() {
  return partySize;
 }
 public void setPartySize(int partySize) {
  this.partySize = partySize;
 }
  public Long getRestaurantId() {
  return restaurantId;
 }
 public void setRestaurantId(Long restaurantId) {
  this.restaurantId = restaurantId;
 }
 public LocalDateTime getDt() {
  return dt;
 }
 public void setDt(LocalDateTime dt) {
  this.dt = dt;
 }
}

package com.example.demo;

@SpringBootApplication
public class ReservationApp {
 public static void main(String[] args) {
  SpringApplication.run(ReservationApp.class, args);
 }
}

package com.example.demo;
@RestController
@RequestMapping("/v1")
public class ReservationController {

 @Autowired
 private ReservationService reservationService;

 // ------------ Retrieve all reservations ------------
 @RequestMapping(value = "/reservations", method = RequestMethod.GET)
 public List getAllReservations() {

  return reservationService.getAllReservations();

 }

package com.example.demo;
import org.springframework.data.repository.CrudRepository;
public interface ReservationRepository extends CrudRepository<Reservation,String> {
 }

 package com.example.demo;

@Service 
public class ReservationService {

 @Autowired
 private ReservationRepository reservationRepository;

 // Retrieve all rows from table and populate list with objects
 public List getAllReservations() {
  List reservations = new ArrayList<>();
  reservationRepository.findAll().forEach(reservations::add);
  return reservations;
 }
 }

【问题讨论】:

  • 每次启动应用程序时都会创建表吗?如果是这样,那是正常的,因为它是 H2 db
  • 但我不希望他们创建它们,我想为此添加 create 语句。

标签: spring spring-boot h2


【解决方案1】:

尝试删除spring boot hibernate配置

spring.jpa.hibernate.ddl-auto = update

能够从实体创建/更新数据库架构

【讨论】:

  • 对不起。我不关注这个问题。什么是多重配置文件。另一个问题 hibernate.configuration 文件在哪里。我已将 application.property 更改为 spring.jpa.hibernate.ddl-auto = none。但我认为它不是正确的地方
  • 您在资源文件夹中有一个名为 application.properties 的文件。你只有一个。属性文件,那么你没有配置文件
  • 您可以为每个环境创建多个配置文件一个配置文件。例如,您可以将一个 orofile 用于开发,另一个用于生产,并且每个文件都有其 .properties 文件
  • 这是 application.properties 不是 application.property
【解决方案2】:

要禁用自动 DDL 生成,请将 application.properties 中的以下属性设置为 false

spring.jpa.generate-ddl = false

更多信息和细粒度控制,请参阅documentation

【讨论】:

    【解决方案3】:

    在 application.properties 中将 ddl generation 设置为 none:

    spring.jpa.hibernate.ddl-auto=none
    

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 2016-05-15
      • 2023-02-08
      • 2017-02-20
      • 2018-08-23
      • 2018-06-13
      • 1970-01-01
      • 2014-08-27
      • 2018-11-29
      相关资源
      最近更新 更多