[英]Pageable parameter in my jpaRepository.findAll() returns an SqlSyntaxErrorException
[英]JpaRepository findAll() returns empty result
JpaRepository
findAll()
方法返回空結果。 我正在嘗試使用 Spring-boot、h2 數據庫和 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 doc
你有一個schema.sql
和data.sql
這是后兩者執行DataSource
已經配置並准備。 接下來創建EntityManagerFactory
,默認情況下(參見參考指南 ),這將create-drop
嵌入式類型的數據庫(如H2)。
您可以通過將spring.jpa.hibernate.ddl-auto
屬性更改為其他任何內容然后create
或create-drop
來覆蓋此行為。
另一個解決方案是將data.sql
重命名為import.sql
,這將在Hibernate為您創建架構后執行。 您現在顯然也可以刪除schema.sql
因為Hibernate將創建架構。
如果這是出於學習目的,你應該沒問題,如果你想在現場制作系統中使用它,我建議不要使用它來使用像Flyway這樣的東西來管理你的架構。
據我所知,你想在應用程序啟動時執行sql腳本,之后使用Hibernate? 好吧,你必須使用這里提到的選項之一,並設置spring.jpa.hibernate.ddl-auto=none
。在那里給出解釋。
祝好運
在我的情況下,我將刪除的列設置為 null 並在 JPA 實體上設置了 @Where(clause = "deleted='false'")
這是一個對我有用的示例代碼:
@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);
首先,您不需要覆蓋findAll()
方法。 它已經在您的界面中可用。
在配置您的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
我的sql
文件在這個位置: test/resources/sql
。 否則,默認情況下,如果您將文件放在test/resources
,則不需要這些配置
你可以在這篇文章中找到非常好的細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.