java 未知访问类型记录

kyxcudwk  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(97)

我正在尝试使用jakarta和jparepository将Weather对象添加到数据库。出现以下错误:未知的访问类型记录。最初,有javax而不是jakarta,但有一个错误,该对象是非托管的。我换到了雅加达,出现了一个新的错误。当用Crudrepository替换JpaRepository时,也没有什么变化。

package com.example.weather;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.weather.repository")
public class WeatherApplication {
    public static void main(String[] args) {
        SpringApplication.run(WeatherApplication.class, args);
    }
}
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> {

}
package com.example.weather.model;

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


import java.io.Serializable;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/weather")
@Transactional
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;
    }
}

错误代码:

2023-06-12T19:16:36.570+03:00  INFO 21364 --- [           main] com.example.weather.WeatherApplication   : Starting WeatherApplication using Java 20.0.1 with PID 21364 (C:\Users\Professional\IdeaProjects\weather\target\classes started by Professional in C:\Users\Professional\IdeaProjects\weather)
2023-06-12T19:16:36.572+03:00  INFO 21364 --- [           main] com.example.weather.WeatherApplication   : No active profile set, falling back to 1 default profile: "default"
2023-06-12T19:16:37.174+03:00  INFO 21364 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-06-12T19:16:37.236+03:00  INFO 21364 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 54 ms. Found 1 JPA repository interfaces.
2023-06-12T19:16:37.733+03:00  INFO 21364 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-06-12T19:16:37.743+03:00  INFO 21364 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-06-12T19:16:37.743+03:00  INFO 21364 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-06-12T19:16:37.835+03:00  INFO 21364 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-06-12T19:16:37.835+03:00  INFO 21364 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1207 ms
2023-06-12T19:16:37.987+03:00  INFO 21364 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-06-12T19:16:38.044+03:00  INFO 21364 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.2.2.Final
2023-06-12T19:16:38.046+03:00  INFO 21364 --- [           main] org.hibernate.cfg.Environment            : HHH000406: Using bytecode reflection optimizer
2023-06-12T19:16:38.095+03:00  INFO 21364 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-06-12T19:16:38.626+03:00  INFO 21364 --- [           main] o.h.b.i.BytecodeProviderInitiator        : HHH000021: Bytecode provider name : bytebuddy
2023-06-12T19:16:38.773+03:00  INFO 21364 --- [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2023-06-12T19:16:38.789+03:00  INFO 21364 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-06-12T19:16:38.977+03:00  INFO 21364 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@31e7afde
2023-06-12T19:16:38.978+03:00  INFO 21364 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-06-12T19:16:39.014+03:00  INFO 21364 --- [           main] org.hibernate.orm.dialect                : HHH035001: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect, version: 10.0
2023-06-12T19:16:39.015+03:00  WARN 21364 --- [           main] org.hibernate.orm.deprecation            : HHH90000026: PostgreSQL10Dialect has been deprecated; use org.hibernate.dialect.PostgreSQLDialect instead
2023-06-12T19:16:39.123+03:00  INFO 21364 --- [           main] o.h.b.i.BytecodeProviderInitiator        : HHH000021: Bytecode provider name : bytebuddy
2023-06-12T19:16:39.353+03:00  INFO 21364 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-06-12T19:16:39.359+03:00  INFO 21364 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-06-12T19:16:39.546+03:00  WARN 21364 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'weatherController' defined in file [C:\Users\Professional\IdeaProjects\weather\target\classes\com\example\weather\controller\WeatherController.class]: Unsatisfied dependency expressed through constructor parameter 2: Error creating bean with name 'weatherRepository' defined in com.example.weather.repository.WeatherRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.weather.model.Weather
2023-06-12T19:16:39.547+03:00  INFO 21364 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2023-06-12T19:16:39.548+03:00  INFO 21364 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-06-12T19:16:39.604+03:00  INFO 21364 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2023-06-12T19:16:39.606+03:00  INFO 21364 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-06-12T19:16:39.619+03:00  INFO 21364 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-06-12T19:16:39.636+03:00 ERROR 21364 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'weatherController' defined in file [C:\Users\Professional\IdeaProjects\weather\target\classes\com\example\weather\controller\WeatherController.class]: Unsatisfied dependency expressed through constructor parameter 2: Error creating bean with name 'weatherRepository' defined in com.example.weather.repository.WeatherRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.weather.model.Weather
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:245) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1189) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.9.jar:6.0.9]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.9.jar:6.0.9]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:733) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:435) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1305) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1294) ~[spring-boot-3.1.0.jar:3.1.0]
    at com.example.weather.WeatherApplication.main(WeatherApplication.java:10) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'weatherRepository' defined in com.example.weather.repository.WeatherRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.weather.model.Weather
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:888) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-6.0.9.jar:6.0.9]
    ... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.weather.model.Weather
    at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:192) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:465) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:97) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:80) ~[spring-data-jpa-3.1.0.jar:3.1.0]
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:69) ~[spring-data-jpa-3.1.0.jar:3.1.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:246) ~[spring-data-jpa-3.1.0.jar:3.1.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:211) ~[spring-data-jpa-3.1.0.jar:3.1.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:194) ~[spring-data-jpa-3.1.0.jar:3.1.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) ~[spring-data-jpa-3.1.0.jar:3.1.0]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317) ~[spring-data-commons-3.1.0.jar:3.1.0]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279) ~[spring-data-commons-3.1.0.jar:3.1.0]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:245) ~[spring-data-commons-3.1.0.jar:3.1.0]
    at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-3.1.0.jar:3.1.0]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285) ~[spring-data-commons-3.1.0.jar:3.1.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132) ~[spring-data-jpa-3.1.0.jar:3.1.0]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.9.jar:6.0.9]
    ... 30 common frames omitted
42fyovps

42fyovps1#

我在尝试使用hibernate-search-orm-mapper时遇到了同样的问题。问题是springboot-starter-data-jpa没有管理这个特定的依赖项。所以data-jpa尝试使用hibernate6.2.2jar,而search-orm-mapper尝试使用5.1.2。
我不得不检查哪个版本都使用相同的hibernate-core jar。对于我来说,springversion2.6.6和search-ormversion 6. 1. 3-Final现在都使用hibernate-core 5. 6. 7
类似的方式,你必须找到你的情况下..使用mvn dependency:tree查找不同版本的重复依赖项,您将在那里找到罪魁祸首,然后在maven central中检查这两个依赖项,并查看哪个版本编译依赖项匹配..

相关问题