[英]JpaRepository findAll() returns empty result
JpaRepository
findAll()
method returns empty result. JpaRepository
findAll()
方法返回空结果。 I am trying to implement rest service by using Spring-boot, h2 database and jpa.我正在尝试使用 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.sql
file 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 City
entity : 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 JpaRepository
interface: 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 Contoller
class这是我的
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
You have a schema.sql
and data.sql
which are both executed after the DataSource
has been configured and is ready. 你有一个
schema.sql
和data.sql
这是后两者执行DataSource
已经配置并准备。 Next the EntityManagerFactory
is created and by default (See the reference guide ) this will create-drop
the database for embedded types (like H2). 接下来创建
EntityManagerFactory
,默认情况下(参见参考指南 ),这将create-drop
嵌入式类型的数据库(如H2)。
You can override this behavior by changing the spring.jpa.hibernate.ddl-auto
property to anything else then create
or create-drop
. 您可以通过将
spring.jpa.hibernate.ddl-auto
属性更改为其他任何内容然后create
或create-drop
来覆盖此行为。
Another solution is to rename your data.sql
to import.sql
which will be executed after Hibernate created the schema for you. 另一个解决方案是将
data.sql
重命名为import.sql
,这将在Hibernate为您创建架构后执行。 You can now obviously also remove the schema.sql
as Hibernate will create the schema. 您现在显然也可以删除
schema.sql
因为Hibernate将创建架构。
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. 如果这是出于学习目的,你应该没问题,如果你想在现场制作系统中使用它,我建议不要使用它来使用像Flyway这样的东西来管理你的架构。
As far as i understood, you want to execute sql scripts on application startup and after that use Hibernate? 据我所知,你想在应用程序启动时执行sql脚本,之后使用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. 好吧,你必须使用这里提到的选项之一,并设置
spring.jpa.hibernate.ddl-auto=none
。在那里给出解释。
Good luck 祝好运
在我的情况下,我将删除的列设置为 null 并在 JPA 实体上设置了 @Where(clause = "deleted='false'")
This is a sample code that works for me :这是一个对我有用的示例代码:
@DataJpaTest
@TestPropertySource(properties = {
"spring.datasource.schema=classpath:/sql/schema.sql",
"spring.datasource.data=classpath:/sql/data.sql",
"spring.jpa.hibernate.ddl-auto=none"
})
class CalculatorRepositoryTest implements WithAssertions {
@Autowired
private CalculatorRepository repository;
@Test
void testAutoConfiguration() {
assertThat(repository).isNotNull();
}
@Test
void testInsertResult() {
Result newResult = new Result();
newResult.setOperation(OperationType.DIVISION.name());
newResult.setValue(10);
Result insertedResult = repository.save(newResult);
assertAll(
() -> assertThat(insertedResult).isNotNull(),
() -> assertThat(insertedResult.getId()).isEqualTo(5L),
() -> assertThat(insertedResult.getOperation()).isEqualTo(OperationType.DIVISION.name()),
() -> assertThat(insertedResult.getValue()).isEqualTo(10)
);
}
@Test
void testSelectAllResults() {
List<Result> results = repository.findAll();
assertAll(
() -> assertThat(results).isNotEmpty(),
() -> assertThat(results.get(0).getOperation()).isEqualTo(OperationType.ADDITION.name()),
() -> assertThat(results.get(0).getValue()).isEqualTo(5)
);
}
}
schema.sql
CREATE TABLE result(
id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
operation VARCHAR(50) NOT NULL,
value INT
);
data.sql
INSERT INTO result(id, operation, value) VALUES(1, 'ADDITION', 5);
INSERT INTO result(id, operation, value) VALUES(2, 'SUBTRACTION', 14);
INSERT INTO result(id, operation, value) VALUES(3, 'MULTIPLICATION', 10);
INSERT INTO result(id, operation, value) VALUES(4, 'DIVISION', 3);
First fo all you don't need to override the findAll()
method.首先,您不需要覆盖
findAll()
方法。 It is already available in your interface.它已经在您的界面中可用。
While configuring your schema.sql
and data.sql
file, the first thing to do is to set this propertie spring.jpa.hibernate.ddl-auto
to none
, to avoid hibernate create the schema for you.在配置您的
schema.sql
和data.sql
文件,要做的第一件事就是设置这个propertie spring.jpa.hibernate.ddl-auto
到none
,避免休眠创建模式为您服务。
spring.datasource.schema=classpath:/sql/schema.sql
spring.datasource.data=classpath:/sql/data.sql
Here my sql
files are in this location : test/resources/sql
.我的
sql
文件在这个位置: test/resources/sql
。 Otherwise, by default, you don't need those configurations if you put your file in test/resources
否则,默认情况下,如果您将文件放在
test/resources
,则不需要这些配置
You can find very good details on this post .你可以在这篇文章中找到非常好的细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.