java JpaRepository findAll() 返回空结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43135913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:07:45  来源:igfitidea点击:

JpaRepository findAll() returns empty result

javaspringspring-data-jpah2

提问by fiskra

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

JpaRepositoryfindAll()方法返回空结果。我正在尝试使用 Spring-boot、h2 数据库和 jpa 来实现休息服务。

Here is my schema.sql

这是我的 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`));

My data.sqlfile includes :

我的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');

The Cityentity :

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;
  }

}

The JpaRepositoryinterface:

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();
}

And here is my Contollerclass

这是我的Contoller班级

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

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

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

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

回答by M. Deinum

You have a schema.sqland data.sqlwhich are both executed after the DataSourcehas been configured and is ready. Next the EntityManagerFactoryis created and by default (See the reference guide) this will create-dropthe database for embedded types (like H2).

您有一个schema.sqldata.sql,它们都在DataSource配置完成并准备就绪后执行。接下来EntityManagerFactory创建,默认情况下(参见参考指南)这将create-drop是嵌入类型(如 H2)的数据库。

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

您可以通过将spring.jpa.hibernate.ddl-auto属性更改为其他任何内容 thencreate或来覆盖此行为create-drop

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

另一种解决方案是将您的重命名data.sqlimport.sql将在 Hibernate 为您创建架构后执行的。您现在显然也可以删除schema.sqlHibernate 将创建模式。

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 Flywayto manage your schema.

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

回答by Reborn

As far as i understood, you want to execute sql scripts on application startup and after that use Hibernate? Well, you have to use one of the options mentioned here, and set spring.jpa.hibernate.ddl-auto=none.The explanation is given there.

据我了解,您想在应用程序启动时执行 sql 脚本,然后再使用 Hibernate?好吧,你必须使用这里提到的选项之一,并设置spring.jpa.hibernate.ddl-auto=none。解释在那里给出。

Good luck

祝你好运

回答by Samuel Gwokuda

In my case I had the deleted column as null and had @Where(clause = "deleted='false'") on the JPA Entity

在我的情况下,我将删除的列设置为空,并在 JPA 实体上设置了 @Where(clause = "deleted='false'")