Leere Tabelle Bei Verwendung von JParepository?Java

Java-Forum
Guest
 Leere Tabelle Bei Verwendung von JParepository?

Post by Guest »

Ich mache mein erstes Frühjahrsprojekt, in dem ich die Tabelle initialisiere: < /p>

Code: Select all

pom.xml:

Code: Select all

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
:

Code: Select all

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
:

Code: Select all

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
:

Code: Select all

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:

Code: Select all

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
und dann besuchen Wenn ich jedoch das JPA -Repository hatte, gibt die obige Abfrage leer zurück: < /p>

Code: Select all

PriceController.java
:

Code: Select all

package com.inditex.inditexcodechallenge.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.inditex.inditexcodechallenge.entity.Price;
import com.inditex.inditexcodechallenge.service.PriceService;

@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
:

Code: Select all

package com.inditex.inditexcodechallenge.entity;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

@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
:

Code: Select all

package com.inditex.inditexcodechallenge.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.inditex.inditexcodechallenge.entity.Price;

@Repository
public interface PriceRepository extends JpaRepository
 {
// Custom query methods can be added here if needed
}
< /code>
PriceService.java
:

Code: Select all

package com.inditex.inditexcodechallenge.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.inditex.inditexcodechallenge.entity.Price;
import com.inditex.inditexcodechallenge.repository.PriceRepository;

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

}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post