Problemy w temacie Jpql

0

Witam serdecznie

Przerabiam materiał z tematu Jpql i napotkałem kilka problemów, z którymi nie mogę sobie poradzić.
Prosiłbym o wskazówki osób bardziej ogarniętych i zaznajomionych, które mogą mnie naprowadzić do rozwiązania zadania i wskazać błędy
Wrzucę poniżej cały kod

Postaram się po krótce opisać co robię, wstępem będzie przykład który przerabiam, a później to z czym mam problem.

Korzystam z Intellij idea i zaznajamiam się z jpql, ów przykład jest zrobiony na spring boot.
Klasa Car, gdzie jest model Car (tutaj znajdują się pola, encje, implementcja ),
jest interfejs CarDao tutaj są metody:
void save(Car car);
Car get(Long id);
List<Car> get(String customJpqlQuery);
List<Car> getAll();
int remove(String customDeleteFromCarCWhere);
int removeAll(),
jest też CarDaoImpl ( klasa implementuje metody z CarDao).

Stworzyłem scheme car_db, z której korzystam w przykładzie z samochodami.

Teraz to z czym mam problem.
Działając dalej na tym samym projekcie, który omawiany jest powyżej, jest schema word.
Na podstawie której, chcę stworzyć model city i customowe metody. Model city ma pola i również encje. Jest też CityDao i CityDaoImpl.
Pierwsza metoda pobiera listę wszystkich miast.
W klasie main próbuję sprawdzić i wywołać tą metodą, ale pojawia się błąd.

2021-08-15 10:59:49.603  WARN 9308 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1146, SQLState: 42S02
2021-08-15 10:59:49.603 ERROR 9308 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Table 'cars_db.city' doesn't exist
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet


Prosiłbym o wskazówki co robię źle. Gdzie popełniam błąd?
Może to banalny wątek, ale chcę się nauczyć.
Poniżej podsyłam kod, wygenerowany skrypt schemy word

application.properties:


#data base
spring.datasource.url=jdbc:mysql://localhost:3306/cars_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "city")
public class City implements Serializable {
    private static final long serialVersionUID = 1528284298896210101L;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Integer id;

    @Column(nullable = false)
    private Integer countryCode;

    @Column(nullable = false)
    private String district;

    @Column(nullable = false)
    private Integer population;

    public City() {
    }

    public City(int countryCode, String district, int population) {
        this.countryCode = countryCode;
        this.district = district;
        this.population = population;
    }

    @Override
    public String toString() {
        return "City{" +
                "id=" + id +
                ", countryCode=" + countryCode +
                ", district='" + district + '\'' +
                ", population=" + population +
                '}';
    }
}

import java.util.List;

public interface CityDao {

    List<City> getAll();
}

@Repository
@Transactional
public class CityDaoImpl implements CityDao{

    private EntityManager entityManager;

    public CityDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public List<City> getAll() {
        final String jpqlQuery = "SELECT c FROM City c";
        TypedQuery<City> tq = entityManager.createQuery(jpqlQuery, City.class);
        return tq.getResultList();
    }
}

create table country
(
    Code           char(3)                                                                                      default ''     not null
        primary key,
    Name           char(52)                                                                                     default ''     not null,
    Continent      enum ('Asia', 'Europe', 'North America', 'Africa', 'Oceania', 'Antarctica', 'South America') default 'Asia' not null,
    Region         char(26)                                                                                     default ''     not null,
    SurfaceArea    float(10, 2)                                                                                 default 0.00   not null,
    IndepYear      smallint                                                                                                    null,
    Population     int                                                                                          default 0      not null,
    LifeExpectancy float(3, 1)                                                                                                 null,
    GNP            float(10, 2)                                                                                                null,
    GNPOld         float(10, 2)                                                                                                null,
    LocalName      char(45)                                                                                     default ''     not null,
    GovernmentForm char(45)                                                                                     default ''     not null,
    HeadOfState    char(60)                                                                                                    null,
    Capital        int                                                                                                         null,
    Code2          char(2)                                                                                      default ''     not null
)
    charset = latin1;

create table city
(
    ID          int auto_increment
        primary key,
    Name        char(35) default '' not null,
    CountryCode char(3)  default '' not null,
    District    char(20) default '' not null,
    Population  int      default 0  not null,
    constraint city_ibfk_1
        foreign key (CountryCode) references country (Code)
)
    charset = latin1;

create index CountryCode
    on city (CountryCode);

create table countrylanguage
(
    CountryCode char(3)         default ''  not null,
    Language    char(30)        default ''  not null,
    IsOfficial  enum ('T', 'F') default 'F' not null,
    Percentage  float(4, 1)     default 0.0 not null,
    primary key (CountryCode, Language),
    constraint countryLanguage_ibfk_1
        foreign key (CountryCode) references country (Code)
)
    charset = latin1;

create index CountryCode
    on countrylanguage (CountryCode);

create table `world.employees`
(
    id        int          null,
    firstname varchar(100) null,
    lastname  varchar(100) null,
    position  varchar(100) null,
    salary    double       null,
    fired     tinyint(1)   null,
    `(id)`    int          not null
        primary key
);

@SpringBootApplication
public class JpqlLessonApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(JpqlLessonApplication.class, args);

        CarDao carDao = ctx.getBean(CarDao.class);
//        Car c1 = new Car("BMW", "M3cs", "3.0 R6 TwinTurbo", 2979, 460, 600);
//        carDao.save(c1);
//
//        Car c2 = new Car("Mercedes-Benz", "C63s", "4.0 V8 BiTurbo", 3982, 510, 700);
//        carDao.save(c2);
//
//        Car c3 = new Car("Alfa Romeo", "Giulia QV", "2.9 V6 TwinTurbo", 2891, 510, 600);
//        carDao.save(c3);
//
//        Car c4 = new Car("BMW", "330i", "2.0 R4 Turbo", 1998, 258, 400);
//        carDao.save(c4);

//        Car loadedCar = carDao.get(1L);
//        System.out.println(loadedCar);
//
//        List<Car> carList = carDao.get("SELECT c FROM Car c WHERE c.powerHp > 500");
//        carList.forEach(System.out::println);
//
//        List<Car> all = carDao.getAll();
//        all.forEach(System.out::println);
//        int removeCars = carDao.remove("c.powerHp > 500");
//
//        System.out.println(removeCars);

        int removeAllCars = carDao.removeAll();
        System.out.println(removeAllCars);

        System.out.println("==============");


        CityDao cityDao = ctx.getBean(CityDao.class);
        List<City> all = cityDao.getAll();
        all.forEach(System.out::println);

        ctx.close();
    }

}
0

Generalnie problem jest taki, że

Table 'cars_db.city' doesn't exist

:P Tylko teraz pytanie czemu. Może Ci się ten Twój plik sqlowy nie uruchamia? Uruchamiasz go ręcznie czy to jakiś Flywai / Liquibase? Jeśli ręcznie, to może nie zrobiłeś commita?

EDIT:

starskyHutch napisał(a):

Stworzyłem scheme car_db, z której korzystam w przykładzie z samochodami.

To w końcu car_db czy cars_db?

1 użytkowników online, w tym zalogowanych: 0, gości: 1