0

I have a Weather object that I want to add to a postgresql database using JpaRepository. Error code: Not a managed type: class com.example.weather.model.Weather Although there are all dependencies for entering an object into the database. The Service class was created with the @Service annotation, through which I tried to enter the object into the database, but the problem remains the same.

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

I added annotations @EntityScan and @EnableJpaRepositories, but it didn't solve the problem.

  • 1
    Does this answer your question? [Spring boot - Not a managed type](https://stackoverflow.com/questions/28664064/spring-boot-not-a-managed-type) – juanlumn Jun 12 '23 at 12:42
  • Let me guess, you are "following" a tutorial for some Spring Boot 2.x version and decided to go with Spring Boot 3.x. Ran into issues with classes not being found, added the `persistence-api` dependency and now things don't work? Spring Boot 3 is for JakartaEE not JavaEE. Remove said dependency (and maybe some hibernate one you added in the mix) and use the `jakarta.persistence` annotation instead of `javax.persistence`. – M. Deinum Jun 12 '23 at 14:17
  • I changed jakarta.persistence to javax.persistence. But a new error has appeared: Unknown access type record – Просто Смешно Jun 12 '23 at 15:13

1 Answers1

0

Check if there are any exceptions or error messages in the application logs that can provide insight into the issue. Look for any database-related errors, transaction failures, or constraint violations.

If you're using an existing database schema, ensure that the table structure matches your entity class definitions. If there are any inconsistencies, you may need to update your entity class or perform database schema migrations.

also try:

spring.jpa.hibernate.ddl-auto=create-drop
SM. Hosseini
  • 171
  • 1
  • 13