spring 无法向数据库添加对象,JPA

6jygbczu  于 2023-06-21  发布在  Spring
关注(0)|答案(1)|浏览(127)

我有一个Weather对象,我想使用JpaRepository添加到postgresql数据库。错误代码:不是托管类型:尽管在数据库中输入对象时存在所有依赖项。Service类是用@Service注解创建的,我试图通过它将对象输入到数据库中,但问题仍然存在。

package com.example.weather;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WeatherApplication {
    public static void main(String[] args) {
        SpringApplication.run(WeatherApplication.class, args);
    }

}
package com.example.weather.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "weather")
public class Weather implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "city")
    private String city;
    @Column(name = "temp")
    @JsonProperty("temp")
    private int temp;
    @Column(name = "wind_speed")
    @JsonProperty("wind_speed")
    private double wind_speed;
    @Column(name = "wind_degrees")
    @JsonProperty("wind_degrees")
    private double wind_degrees;
    @Column(name = "humidity")
    @JsonProperty("humidity")
    private int humidity;
    @Column(name = "sunset")
    @JsonProperty("sunset")
    private int sunset;
    @Column(name = "min_temp")
    @JsonProperty("min_temp")
    private int min_temp;
    @Column(name = "cloud_pct")
    @JsonProperty("cloud_pct")
    private int cloud_pct;
    @Column(name = "feels_like")
    @JsonProperty("feels_like")
    private int feels_like;
    @Column(name = "sunrise")
    @JsonProperty("sunrise")
    private int sunrise;
    @Column(name = "max_temp")
    @JsonProperty("max_temp")
    private int max_temp;

    public void Print(){
        System.out.println(String.format("id = %s\ncity = %s\ntemp = %s\nwind_speed = %s\nwind_degrees = %s\n" +
                "humidity = %s\nsunset = %s\nmin_temp = %s\ncloud_pct = %s\nfeels_like = %s\nsunrise = %s\nmax_temp = %s",
                id, city, temp, wind_speed, wind_degrees, humidity, sunset, min_temp, cloud_pct, feels_like, sunrise, max_temp));
    }
}
package com.example.weather.controller;

import com.example.weather.jms.WeatherListener;
import com.example.weather.jms.WeatherProducer;
import com.example.weather.model.Weather;
import com.example.weather.repository.WeatherRepository;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

@RestController
@RequestMapping("/weather")
public class WeatherController {

    private final WeatherProducer weatherProducer;
    private final WeatherListener weatherListener;
    private final WeatherRepository weatherRepository;

    @Autowired
    public WeatherController(WeatherProducer weatherProducer, WeatherListener weatherListener, WeatherRepository weatherRepository) {
        this.weatherProducer = weatherProducer;
        this.weatherListener = weatherListener;
        this.weatherRepository = weatherRepository;
    }

    @GetMapping("{city}")
    public int getWeather(@PathVariable String city) throws Exception{
        URL url = new URL("https://api.api-ninjas.com/v1/weather?city=" + city);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.addRequestProperty("X-Api-Key", "aCQf5mYIfKNIhy2etdORbQ==4lC2SUpMjyg9CTpq");
        InputStream responseStream = connection.getInputStream();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(responseStream);
        Weather weather = mapper.readValue(root.toString(), Weather.class);
        weather.setCity(city);

        //weatherProducer.sendWeather(weather);
        weatherRepository.save(weather);

        int currentTemperature = weather.getTemp();

        return currentTemperature;
    }
}
package com.example.weather.repository;

import com.example.weather.model.Weather;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface WeatherRepository extends JpaRepository<Weather, Long> {

}
spring.datasource.url=jdbc:postgresql://localhost:5432/weather
spring.datasource.username=postgres
spring.datasource.password=12345
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.database=postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

我添加了注解@EntityScan和@EnableJpaRepositories,但这并没有解决问题。

bfhwhh0e

bfhwhh0e1#

检查应用程序日志中是否有任何异常或错误消息,以便深入了解问题。查找任何与数据库相关的错误、事务失败或约束冲突。
如果使用现有的数据库模式,请确保表结构与实体类定义匹配。如果存在任何不一致,则可能需要更新实体类或执行数据库模式迁移。
也试试:

spring.jpa.hibernate.ddl-auto=create-drop

相关问题