JpaRepository findAll()返回空结果 [英] JpaRepository findAll() returns empty result

查看:1067
本文介绍了JpaRepository findAll()返回空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JpaRepository findAll()方法返回空结果.我正在尝试通过使用Spring-boot,h2数据库和jpa来实现rest服务.

JpaRepository findAll() method returns empty result. I am trying to implement rest service by using Spring-boot, h2 database and jpa.

这是我的schema.sql

CREATE TABLE IF NOT EXISTS `City` (
  `city_id` bigint(20) NOT NULL auto_increment,
  `city_name` varchar(200) NOT NULL,
PRIMARY KEY (`city_id`));

我的data.sql文件包括:

INSERT INTO City (city_id,city_name) VALUES(1,'EDE');
INSERT INTO City (city_id,city_name) VALUES(2,'DRUTEN');
INSERT INTO City (city_id,city_name) VALUES(3,'DELFT');

City实体:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "City")
public class City {

  @Id
  @GeneratedValue
  @Column(name = "city_id")
  private Long cityId;

  @Column(name = "city_name")
  private String cityName;

  public Long getCityId() {
    return cityId;
  }

  public void setCityId(Long cityId) {
    this.cityId = cityId;
  }

  public String getCityName() {
    return cityName;
  }

  public void setCityName(String cityName) {
    this.cityName = cityName;
  }

}

JpaRepository界面:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CityRepository extends JpaRepository<City, Long> {

    @Override
    List<City> findAll();
}

这是我的Contoller

@RestController
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityRepository cityRepository;

    @RequestMapping(method = RequestMethod.GET, value = "/all")
    public List<City> getAllCityList(){
        return cityRepository.findAll();
    }
}

我在这里做错了什么?参考文档: Spring文档

What am I doing wrong here? The reference documentation : Spring doc

推荐答案

您有一个schema.sqldata.sql,它们都在配置DataSource并准备就绪后执行.接下来创建EntityManagerFactory并默认情况下(请参见

You have a schema.sql and data.sql which are both executed after the DataSource has been configured and is ready. Next the EntityManagerFactory is created and by default (See the reference guide) this will create-drop the database for embedded types (like H2).

您可以通过将spring.jpa.hibernate.ddl-auto属性更改为createcreate-drop以外的任何内容来覆盖此行为.

You can override this behavior by changing the spring.jpa.hibernate.ddl-auto property to anything else then create or create-drop.

另一种解决方案是将您的data.sql重命名为import.sql,这将在Hibernate为您创建模式之后执行.您现在显然也可以删除schema.sql,因为Hibernate将创建模式.

Another solution is to rename your data.sql to import.sql which will be executed after Hibernate created the schema for you. You can now obviously also remove the schema.sql as Hibernate will create the schema.

如果这是出于学习目的,那么应该没问题,如果您想在实时生产系统中使用它,我建议不要使用它来使用 Flyway 来管理您的架构.

If this is for learning purposes you should be fine, if you want to use this in a live production system I suggest instead of using this to use something like Flyway to manage your schema.

这篇关于JpaRepository findAll()返回空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆