Код: Выделить всё
pom.xml:Код: Выделить всё
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.4.3
com.test
testcodechallenge
0.0.1-SNAPSHOT
testcodechallenge
Demo project for Spring Boot
17
3.1.5
1.18.30
2.2.0
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-validation
javax.persistence
javax.persistence-api
2.2
com.h2database
h2
runtime
org.projectlombok
lombok
true
org.apache.maven.plugins
maven-compiler-plugin
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
< /code>
application.propertiesКод: Выделить всё
spring.application.name=testcodechallenge
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.sql.init.mode=always
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
< /code>
schema.sqlКод: Выделить всё
DROP TABLE IF EXISTS PRICES;
CREATE TABLE PRICES (
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
BRAND_ID INT NOT NULL,
START_DATE TIMESTAMP NOT NULL,
END_DATE TIMESTAMP NOT NULL,
PRICE_LIST INT NOT NULL,
PRODUCT_ID INT NOT NULL,
PRIORITY INT NOT NULL,
PRICE DECIMAL(10,2) NOT NULL,
CURR VARCHAR(3) NOT NULL
);
< /code>
data.sqlКод: Выделить всё
INSERT INTO PRICES (BRAND_ID, START_DATE, END_DATE, PRICE_LIST, PRODUCT_ID, PRIORITY, PRICE, CURR)
VALUES
(1, '2020-06-14 00:00:00', '2020-12-31 23:59:59', 1, 35455, 0, 35.50, 'EUR'),
(1, '2020-06-14 15:00:00', '2020-06-14 18:30:00', 2, 35455, 1, 25.45, 'EUR'),
(1, '2020-06-15 00:00:00', '2020-06-15 11:00:00', 3, 35455, 1, 30.50, 'EUR'),
(1, '2020-06-15 16:00:00', '2020-12-31 23:59:59', 4, 35455, 1, 38.95, 'EUR');
< /code>
TestcodechallengeApplication.java:Код: Выделить всё
package com.test.testcodechallenge;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestcodechallengeApplication {
public static void main(String[] args) {
SpringApplication.run(TestcodechallengeApplication.class, args);
}
}
< /code>
So far so good: if I run ./mvnw spring-boot:runОднако, если у меня был репозиторий JPA, приведенный выше запрос возвращается пустым: < /p>
Код: Выделить всё
PriceController.javaКод: Выделить всё
@RestController
public class PriceController {
@Autowired
private PriceService priceService;
@GetMapping("/prices")
public List
getAllPrices() {
return priceService.getAllPrices();
}
@GetMapping("/prices/{id}")
public Price getPriceById(@PathVariable Long id) {
return priceService.getPriceById(id);
}
}
< /code>
Price.javaКод: Выделить всё
@Data
@Entity
@Table(name = "PRICES")
public class Price {
@Id
private Long id;
private Long brandId;
private LocalDateTime startDate;
private LocalDateTime endDate;
private Long priceList;
private Long productId;
private BigDecimal price;
private String currency;
}
< /code>
PriceRepository.javaКод: Выделить всё
@Repository
public interface PriceRepository extends JpaRepository
{
// Custom query methods can be added here if needed
}
< /code>
PriceService.java@Service
public class PriceService {
@Autowired
private PriceRepository priceRepository;
public List
getAllPrices() {
return priceRepository.findAll();
}
public Price getPriceById(Long id) {
return priceRepository.findById(id).orElse(null);
}
}
< /code>
What is missed here?
Подробнее здесь: https://stackoverflow.com/questions/794 ... repository